- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 07:44 AM - edited 06-03-2025 04:51 AM
Hi,
I am in the workflow studio, and I have a Look Up Records step. I want to retrieve the data from the Look Up Records step and pass it into my flow variable (set up as type: String) as a comma separated list. Can someone help me with my inline script and let me know why my script isn't working? I want the comma separated list because I am trying to get a list of serial numbers separated by commas to include on a notification that gets triggered using an event to a user group for machines with expired licenses. Once I am able to populate my flow variable with a comma separated list, I intend to pass it to my event as parm 2.
var gr = fd_data._10__look_up_records.records;
var count = fd_data._10__look_up_records.count;
var serialNumberList = [];
if(count > 0 & !gr.hasNext()){
gr = new GlideRecord(gr.getTableName());
gr.addEncodedQuery(gr.getEncodedQuery());
gr.query();
}
while(gr.next()){
serialNumberList.push(gr.getValue('u_serial_number'));
}
var serialNumberList_array = serialNumberList.join();
return "serialNumberListIN"+serialNumberList_array+"";
Here is my flow:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2025 11:12 AM - edited 06-05-2025 11:22 AM
@neil_b
Sorry the error you are getting now is because I'm crossing up traditional workflows and Workflow Designer. Because we are in workflow designer you don't have scratchpad because it has Flow Variables. We can use dot walking to access the flow variable directly, this is also why current.u_serial_number didn't work.
Try this script:
var currentSN = fd_data._9__for_each.item.u_serial_number;
var gr = new GlideRecord('asset_report_table');
gr.addEncodedQuery('sys_created_onBETWEENjavascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()^asset_report_table.u_serial_number='+currentSN);
gr.query();
if(!gr.hasNext()) {
var snList = fd_data.flowVariables.variableName; //replace variableName with the name of your flow variable
if (!gs.nil(snList)) {
fd_data.flowVariables.variableName = snList +"," + currentSN;
} else {
fd_data.flowVariables.variableName = currentSN;
}
}
Given that we are now setting the flow variable as part of the script your Set Flow Variable step is no longer necessary.
With regard to how you are sending emails it might be easiest to actually create a grouped array of the serial numbers. Then you could perform a for each loop on the groups and the serial numbers under the group in the array would already be aligned with the proper user. I haven't specifically used this method with a flow variable.
You could try this:
Make sure you flow variable is of type JSON then try this script:
var snListArray = {};
var currentSN = fd_data._9__for_each.item.u_serial_number;
var gr = new GlideRecord('asset_report_table');
gr.addEncodedQuery('sys_created_onBETWEENjavascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()^asset_report_table.u_serial_number='+currentSN);
gr.query();
while (gr.next()) {
var responsibleUser = fd_data._9__for_each.item.getDisplayValue('responsibleUser'); // update this to reference the correct attribute, it may be something like fd_data._9__for_each.item.location.getDisplayValue('responsibleUser') if the responsible user isn't directly associated with the record in the loop
if (!snListArray[responsibleUser]) {
snListArray[responsibleUser] = [];
}
snListArray[responsibleUser].push(currentSN);
}
fd_data.flowVariables.variableName = snListArray; // I haven't done this specifically before so you may have to use JSON.stringify(snListArray)
This would then set your flow variable to a JSON array which you could then use as a grouped array where the responsible user is the group name. Again I've never applied this method in the way you are using it I've only used it inside scripts. If you are able to figure out how to use the grouped array in a for each item I'd appreciate sharing how (might work better if you use a flow variable type of array.object but not sure if the script would change in that case).
Editing to add an additional thought: If you used the responsible users email address as the group name rather than the display value as in my example script that would allow you to reference it directly when generating the email which might save you the complication of having to look it up in that step. If you need more than one attribute of the responsible user such as using their name for a Hello Name! start to the email then using the responsible users sys_id could also simplify the process of retrieving the full user record as you could use it in a .get() or lookup record step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 12:26 PM - edited 06-03-2025 12:27 PM
Hmm okay. I apologize in advanced as it can be difficult to fully understand someone's issue through text and my reading comprehension isn't always the best 😅. That being said, based on my understanding so far this is what you're trying to achieve:
- Loop through location records on the Location [cmn_location] table.
- For each location, loop through all asset records where the Facility Location matches.
- For each asset, check if an Asset Report was created in the last 30 days.
- If no Asset Reports are found, capture the serial number for the current asset.
If that is correct, than I would think you don't need to perform any look up in the script. You're already looping through all asset records in your flow, so you have all the information you need. Adding a look up would cause a lot of unnecessary overhead, as your flow is going to execute that script multiple times. I would update the script as follows (step 12):
// First we pull the current flow variable into a local variable, and then the
// current asset serial number from our "For Each" Loop (Step 4 in my example,
// but would be step 9 in your flow).
//
var assetSerialNums = fd_data.flow_var.asset_serial_numbers,
currentAssetSn = fd_data._4__for_each.item.serial_number.getValue();
// Since we are looping through assets outside of this script, we need to check
// if the Flow Variable already contains values (if other assets were already
// found), and if so split it. If the string is empty, then we return an empty
// array instead (otherwise if we try to split an empty string we would have
// an expected comma in our output).
//
// If you are unfamiliar with the below syntax, look up "ternary operator."
//
var snNumArr = assetSerialNums ? assetSerialNums.split(",") : [];
// Add the new serial number
//
snNumArr.push(currentAssetSn);
// Return the newly formated string
//
return snNumArr.join()
Note that you will need to update your variable names with what is in your flow. This lets the flow do the work and only appends the new serial number to the flow variable.
Hope that helps! Let me know if you have any questions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 11:35 AM
So I did some testing, obviously I don't have your same tables but the following script worked for me:
var gr = fd_data._1__look_up_records.records;
var snListArray = [];
while (gr.next()) {
snListArray.push(gr.getValue('u_serial_number'));
}
var snList = snListArray.join();
return snList;
This returns a string with comma separated values of any valid attribute for the records output from the lookup records step. This is when used as an inline script in the "Set Flow Variables" action following the Look Up Records step which I assume is where this is going.
If you are using the script and getting an empty output than there are 2 possible reasons:
1 - The attribute name provided in the gr.getValue('attribute name') isn't valid for the records being returned.
2 - There are no records being returned by the look up records step.
You can easily verify if the look up records step is returning records by viewing the runtime value of the output in the execution record. If you are getting valid records output from the look up records step then check that you are using the proper name (not label) for the column/attribute you are attempting to retrieve the value of within gr.GetValue().