I want to save my query results in an array and later use them in the next activity in a workflow. The array only saves the first result a couple of times. I want each result to be saved in the array. Thanks

Hilberto
Kilo Contributor

var targetPrioTwo = new GlideRecord('x_327210_project_consultants') ;
targetPrioTwo.addQuery('currently_available',true);
targetPrioTwo.query();

while (targetPrioTwo.next()) {
targetPrioTwo.addQuery('qualification','=',current.required_requirement);
targetPrioTwo.query()
while (targetPrioTwo.next()){
workflow.scratchpad.prioTwo.push(target.name);
}

1 ACCEPTED SOLUTION

Raf1
Tera Guru

Hi Hilberto,

You can add an array variable and pass the values of it as scratchpad value.

Try this updated code.

 

var test_array = [];
var targetPrioTwo = new GlideRecord('x_327210_project_consultants') ;
targetPrioTwo.addQuery('currently_available',true);
targetPrioTwo.query();

while (targetPrioTwo.next()) {
test_array.push(target.name);
}


workflow.scratchpad.new_array = test_array;

Regards,

Raf

View solution in original post

4 REPLIES 4

SanjivMeher
Kilo Patron
Kilo Patron

Please fix your query to

workflow.scratchpad.prioTwo = [];

var targetPrioTwo = new GlideRecord('x_327210_project_consultants') ;
targetPrioTwo.addQuery('currently_available',true);

targetPrioTwo.addQuery('qualification',current.required_requirement);
targetPrioTwo.query();

while (targetPrioTwo.next()) {
workflow.scratchpad.prioTwo.push(targetPrioTwo.name+'');
}


Please mark this response as correct or helpful if it assisted you with your question.

The SN Nerd
Giga Sage
Giga Sage

Where is your target variable declared?


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Raf1
Tera Guru

Hi Hilberto,

You can add an array variable and pass the values of it as scratchpad value.

Try this updated code.

 

var test_array = [];
var targetPrioTwo = new GlideRecord('x_327210_project_consultants') ;
targetPrioTwo.addQuery('currently_available',true);
targetPrioTwo.query();

while (targetPrioTwo.next()) {
test_array.push(target.name);
}


workflow.scratchpad.new_array = test_array;

Regards,

Raf

AbhishekGardade
Giga Sage

Hello Hilberto,

you have written a perfect code. But you need to use toString if you are passing anything to array.push:

var targetPrioTwo = new GlideRecord('x_327210_project_consultants') ;
targetPrioTwo.addQuery('currently_available',true);
targetPrioTwo.query();

while (targetPrioTwo.next()) {
targetPrioTwo.addQuery('qualification','=',current.required_requirement);
targetPrioTwo.query()
while (targetPrioTwo.next()){
workflow.scratchpad.prioTwo.push(target.name.toString());
}

NOTE:

Whenever you're looping and pushing data directly from a query, you need tostring() it.

array.push(gr.sys_id.toString); OR array.push(gr.getValue('sys_id').toString);

And see if that helps. Whatever is causing this problem I've seen it hundreds of times. If you're looping and you're using an object, if you don't string it you may end up with a list of the same thing (or worse- if you're doing an update on this field, you'll find yourself updating the same record numerous times). I always make sure to toString() or format the object so ServiceNow treats the data separately.

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade