Flow designer inline script issue for updating user criteria

M_iA
Kilo Sage

Hi, wondering if someone can help me out.

I am building a flow that when triggered, captures a company record and a user criteria record and pushes the sys_ids into their corresponding flow variables.

I then have an update action where i want it to assign the company to the user criteria record.

As the companies field in the user criteria record may already contain a company, I have added what i believe to be the right in line script into the update action.

The logs accurate show that it gets the sys_ids. But on update, it is just clearing the value!

Could anyone point me in the right direction?

 

(function execute(inputs, outputs) {
  var userCriteriaSysId = fd_data.flow_var.user_criteria_sys_id;
  var customerSysId = fd_data.flow_var.customer_sys_id;

  gs.info("MTTA1: userCriteriaSysId: " + userCriteriaSysId);
  gs.info("MTTA2: customerSysId: " + customerSysId);

  var grUserCriteria = new GlideRecord('user_criteria');
  if (grUserCriteria.get(userCriteriaSysId)) {
    gs.info("MTTA3: User criteria record found.");
    var currentCompanies = grUserCriteria.company.toString();
    gs.info("MTTA4: currentCompanies: " + currentCompanies);

    if (currentCompanies === '') {
      gs.info("MTTA5: Companies list is empty. Returning: " + customerSysId);
      return customerSysId; // List is empty, just add the ID
    } else {
      // Check if the customerSysId is already in the companies list
      var companiesArray = currentCompanies.split(',');
      if (companiesArray.indexOf(customerSysId) === -1) {
          gs.info("MTTA6: Adding customerSysId to list. Returning: " + currentCompanies + ',' + customerSysId);
          return currentCompanies + ',' + customerSysId; // List has values, add the new ID
      } else {
          gs.info("MTTA7: customerSysId already in list. Returning original list: " + currentCompanies);
          return currentCompanies; // customerSysId already in the list, return original list.
      }
    }
  } else {
    gs.info("MTTA8: User criteria record NOT found.");
    return ''; // Exit if record not found
  }
})(inputs, outputs);
1 ACCEPTED SOLUTION

Thanks for spending the time, but I actually found the issue!

 

I mistakingly wrapped it all up in a function with inputs and outputs! I removed it from the function and just did the simple returns. Its now updating as expected

View solution in original post

9 REPLIES 9

Hi @M_iA and @KKM,

 

I am trying to follow this to have a flow update the collaborators field (a glide list field similar to the companies field on the user criteria record) on a demand record. I have a step in the flow (#3) to look up the sys_id of the requested_for variable. I have an update record step 4, that uses this inline script to update the collaborators field.

return fd_data._3__look_up_requested_for.record.sys_id + '';

This is working as designed and is updating the collaborators field as expected.

Step 5, I am doing a check to see if we need to create an approval. If we do, then I need to update the demand record's collaborator field (step 6) by adding (appending) the requested_for.manager. 
I have tried to use your code as a sample to build this:

(function execute(inputs, outputs) {
  // Get the demand record from trigger
  var demandSysId = fd_data.trigger.sys_id;
  var managerSysId = fd_data._3__look_up_requested_for.record.manager.sys_id + '';

  gs.info("COLLAB1: Demand sys_id: " + demandSysId);
  gs.info("COLLAB2: Manager sys_id: " + managerSysId);

  var demandGr = new GlideRecord('dmn_demand');
  if (!demandGr.get(demandSysId)) {
    gs.info("COLLAB3: Demand record not found.");
    return [];
  }

  // Get existing collaborators
  var currentCollaborators = demandGr.getValue('collaborators') || '';
  gs.info("COLLAB4: Current collaborators: " + currentCollaborators);

  var collabArray = currentCollaborators ? currentCollaborators.split(',') : [];

  // Add manager to the list (no check)
  collabArray.push(managerSysId);

  gs.info("COLLAB5: Final collaborator array: " + JSON.stringify(collabArray));
  return collabArray;
})(inputs, outputs);

However, instead of adding the manager, it is clearing out the field. Any suggestions on how to get it to work?

 

Thanks!

Jeremy

Hi @JeremyHoffman - Did you notice my solution to this? It looks like your script is also in function with inputs and outputs. By removing this, I fixed the issue where it was clearing the value

@M_iA Thanks, I did over look that very important tid bit of information. I did take that into account and updated my script as follows:

var demandSysId = fd_data.flow_var.demand_sys_id;
//var requestedForSysId = fd_data.flow_var.requested_for_sys_id;
var managerSysId = fd_data.flow_var.manager_sys_id;

gs.info("COLLAB1: Demand sys_id: " + demandSysId);
gs.info("COLLAB2: Manager sys_id: " + managerSysId);

var grDemand = new GlideRecord('dmn_demand');
if (grDemand.get(demandSysId)) {
  gs.info("COLLAB3: Demand record found.");
  
  var currentCollaborators = grDemand.getValue ('collaborators'); //Get existing Collaborators list
  gs.info("COLLAB4: currentCollaborators: " + currentCollaborators);

  var collaboratorsArray = currentCollaborators ? currentCollaborators.split(',') : [];
  
  //Check is managerSysId is already in the list
  if (!collaboratorsArray.includes(managerSysId)) {
    gs.info("COLLAB5: Adding managerSysId to list.");
    collaboratorsArray.push(managerSysId);
  } else {
    gs.info("COLLAB6: managerSysId already exists.");
  }
  gs.info("COLLAB7: REturning updatd list: " + JSON.stringify(collaboratorsArray));
  return collaboratorsArray;
} else {
  gs.info("COLLAB8: Demand record NOT found.");
  return[];
}

What I am seeing happen now is the Collaborators field is being updated with "non clickable" sys_id's. These are the sys_id's of the users who should be added to the list, but they are not resolving to the name of the user... 

JeremyHoffman_0-1747157346634.png

Thoughts?

 

Jeremy

 

Hi @JeremyHoffman ,

 

I believe you may need to provide a comma-separated string of sys_ids to the field via the array. It looks like its interpretating your sys_ids as a string rather that references.

 

So, instead of:

return collaboratorsArray;

You should try explicitly joining the array into a comma-separated string:

return collaboratorsArray.join(',');

 

So you will end up with something along the lines of:

// ... (rest of the script remains the same up to the point of preparing collaboratorsArray) ...

  //Check is managerSysId is already in the list
  if (!collaboratorsArray.includes(managerSysId)) {
    gs.info("COLLAB5: Adding managerSysId to list.");
    collaboratorsArray.push(managerSysId);
  } else {
    gs.info("COLLAB6: managerSysId already exists.");
  }
  
  var newCollaboratorsString = collaboratorsArray.join(',');
  gs.info("COLLAB7: Returning updated list as string: " + newCollaboratorsString);
  return newCollaboratorsString; // Return a comma-separated string

} else {
  gs.info("COLLAB8: Demand record NOT found.");
  // Decide what to return if record not found, perhaps an empty string for a list field
  return ''; // Or null, depending on how Flow Designer handles it for list fields
}

 

Hope this helps

Hi @M_iA Thank you for the response. I was able to get it to work with this change:

Change:

return collaboratorsArray;

to:

return collaboratorsArray.toString();

 

Thanks again for your help!

Jeremy