Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Glide Record Query in Workflow

williamskri
Kilo Contributor

Hi Everyone,

 

I am not sure why this is not working and require some help with this query. I am trying to query name records on a table that are equal to a name field entered by the user on a service catalog item in a Run Script on a workflow.

 

My script starts as follows which is the part that is not working;

var gr = new GlideRecord('u_contractor_app');

gr.addQuery('u_name', current.variables.u_contractor_name);

gr.query();

 

gs.log("gr.u_name: " + gr.u_name + "gr.sys_id: " + gr.sys_id + "     current.variables.u_contractor_name: " + current.variables.u_contractor_name);

 

When looking at the script log statements gr.u_name and gr.sys_id returns undefined and current.variables.u_contractor_name returns the sys_id of the contractor.

Any help is much appreciated.

 

Thanks,

Kris

1 ACCEPTED SOLUTION

AnishSasidharan
Mega Expert

As Michael mentioned before, you have to include 'while' or 'if' on your query as like this



var gr = new GlideRecord('u_contractor_app');


gr.addQuery('u_name', current.variables.u_contractor_name);


gr.query();


if(gr.next()){


gs.log("gr.u_name: " + gr.u_name + " gr.sys_id: " + gr.sys_id + "     current.variables.u_contractor_name: " + current.variables.u_contractor_name);


}



Thanks,


Anish


View solution in original post

3 REPLIES 3

michaelhade
Tera Contributor

you need to get the next record after you query the records.



gr.query();


while(gr.next()) {


gs.log("gr.u_name: " + gr.u_name + "gr.sys_id: " + gr.sys_id + "     current.variables.u_contractor_name: " + current.variables.u_contractor_name);

}


AnishSasidharan
Mega Expert

As Michael mentioned before, you have to include 'while' or 'if' on your query as like this



var gr = new GlideRecord('u_contractor_app');


gr.addQuery('u_name', current.variables.u_contractor_name);


gr.query();


if(gr.next()){


gs.log("gr.u_name: " + gr.u_name + " gr.sys_id: " + gr.sys_id + "     current.variables.u_contractor_name: " + current.variables.u_contractor_name);


}



Thanks,


Anish


Anish



Using an if to fetch the next record will only grab the first record.   You need to use a while to grab all records where the contractor name matches the name.



Thanks


Mike