- 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
05-19-2025 08:01 AM
check this and share the logs if values are coming or not
If logs came fine then it should work
Also I moved the entire logic within the IF condition
var gr = fd_data._1__look_up_records.records;
var count = fd_data._1__look_up_records.count;
var serialNumberList = [];
if (count > 0 & !gr.hasNext()) {
gs.info('table name' + gr.getTableName());
gs.info('encoded query' + gr.getEncodedQuery());
gr = new GlideRecord(gr.getTableName());
gr.addEncodedQuery(gr.getEncodedQuery());
gr.query();
gs.info('row count' + gr.getRowCount());
while (gr.next()) {
serialNumberList.push(gr.getValue('u_serial_number'));
}
var serialNumberList_array = serialNumberList.join();
return "serialNumberListIN" + serialNumberList_array + "";
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 08:15 AM - edited 05-19-2025 08:27 AM
Hi @Ankur Bawiskar thank you for your prompt response!
I tried to use your updated script, and I am not getting any logs. Please see my attached screenshot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 09:43 AM
update as this
we should not use ! operator
since we used ! operator it didn't enter
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 10:15 AM - edited 05-19-2025 10:16 AM
Hi @Ankur Bawiskar I tried executing the flow after removing the "!" and it still isn't yielding any results. I think the issue lies with my look up if no data is returning. Could you help me with my dilemma? Let me give you some more background info on my flow
Step 1 - Look up records into table [cmn_location] ~ Identify the locations that have computers
Step 2 - For Each Item ~ For each facility
Step 3 - Look up records into table [u_computers] ~ Look up which computer matches the location in step 1
Step 4 - For Each item ~ For each computer serial number
Step 5 - Look up records into table [u_request] ~ Look up if someone has already submitted a renewal request for the serial number last month
Step 6 - IF Look up records > Count = 0 ~ Meaning, no one has submitted any requests, so all serial numbers need to be added to the list for renewal
Step 7 - Set Flow Variables 'serialNumberList' [Scripted (Expand to edit)]
--Step 7 is where I inserted your suggested code, but I don't think I have the correct table or the correct query conditions.
I basically need to compare the list of serial numbers in step 3 and see if any of the serial numbers have been requested in step 5 and for any serial number that has not been recently requested, then add it to my comma separated list.
For example, if there are 10 computers in Location A, only 2 computers have been requested for renewal, I need the remaining 8 computer serial numbers populated in a comma separated list into my Flow Variable, so that I can pass it as Parm 2 to my email notification.