Inline Scripting in Workflow Studio

neil_b
Tera Guru

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:

flow.png

1 ACCEPTED SOLUTION

@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.

View solution in original post

21 REPLIES 21

@John Gilmore thank you so much for the updated script. This worked beautifully. I truly appreciate all of your help, patience, and determination!

 

If anyone needs this script, I have included it below as there were additional syntax errors from John's suggestion that have since been fixed.

var currentSN = fd_data._9__for_each.item.u_asset_serial_number;
var gr = new GlideRecord ('asset_report_table');
gr.addEncodedQuery('sys_created_onBETWEENjavascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()^u_asset_serial_number=' + currentSN);
gr.query();
 if(!gr.next()){
       var snList = fd_data.flow_var.assetList;
            if (!gs.nil(snList)) {
            var fullList = fd_data.flow_var.assetList= snList + ', ' + currentSN;
            } else {
            fullList = fd_data.flow_var.assetList= currentSN;
      }
 }

 return fullList;

 

 

Chaitanya ILCR
Kilo Patron

Hi @neil_b ,

 

var gr = fd_data._1__look_up_records.records;
var count = fd_data._1__look_up_records.count;
var serialNumberList = [];
if (count > 0 & !gr.next()) {
    gr = new GlideRecord(gr.getTableName()); //this won't work as gr won't enter this if count of recors is zero or records doesn't exist and at this point of time gr is this line
    gr.addEncodedQuery(gr.getEncodedQuery());//this too won't run better give pass a value from lookup record condition
    gr.query();
}
while (gr.next()) {
    serialNumberList.push(tbGr.getValue('u_serial_number'));
}

I have added my comments next to the lines

line 5 and 6 don't work here and I don't why you have to go down this path too as you are using the same filter as lookup records and same table it's not going to give you any results if the lookup records don't give you any

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

@Chaitanya ILCR thank you for your input. Can you let me know about the following syntax? I'm unfamiliar with what you wrote on the 2nd to last line 

serialNumberList.push(tbGr.getValue('u_serial_number'));

 What is 'tbGr' coming from? I'm not passing that as a variable so I would just like to know what that is and how it's used so that I can better understand how to read the script. 

Hi @neil_b ,

apologies

My Bad

var gr = fd_data._1__look_up_records.records;
var count = fd_data._1__look_up_records.count;
var serialNumberList = [];
if (count > 0 & !gr.next()) {
    gr = new GlideRecord(gr.getTableName()); //this won't work as gr won't enter this if count of recors is zero or records doesn't exist and at this point of time gr is this line
    gr.addEncodedQuery(gr.getEncodedQuery());//this too won't run better give pass a value from lookup record condition
    gr.query();
}
while (gr.next()) {
    serialNumberList.push(gr.getValue('u_serial_number'));
}

return "serialNumberListIN" + serialNumberList.join();

what I'm trying to say is from if to while (4th line to 9th line) it doesn't make any difference in keeping it or removing it

and also in the the case of 5th and 6th line gr.getTableName() and gr.getEncodedQuery() won't work as you are initializing a new glideRecord as gr and the value you are intending to get is from look up records action

 

you can simply use below script

 

check if there any records at all in the table with condition added in the look up records action

var gr = fd_data._1__look_up_records.records;
var count = fd_data._1__look_up_records.count;
var serialNumberList = [];
while (gr.next()) {
    serialNumberList.push(gr.getValue('u_serial_number'));
}

return "serialNumberListIN" + serialNumberList.join();

or

no code approach

you can create a flow variable and in the for each action add the u_serial_number to flow variable

 

just check if 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

 

The output from the lookup records step is a wrapper and not a glide record. Therefor aren't these lines required to convert the output to a glide record in order to use gr.next?

count > 0 & !gr.hasNext()

"count > 0" verifies that there are records within the wrapper and then !gr.hasnext() verifies if its a glide record and not a wrapper. If it contains records and is not a glide record then it will regerate the query to retrieve the proper glide record so that gr.next() can be used.