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.

Accessing variables through the Background Scripts

Puru2
Tera Contributor

Dear All,

I am trying to modify a variable *Reason for order*   of an existing RITM record through background script , but unable to access the variable (script throws an error).

Below is the code snippet :

var obj = new GlideRecord('sc_req_item');

obj.addQuery('sys_id','dc60c6eddb14430020a656f0cf961988');

obj.query();

gs.log("Record number is :"+obj.getRowCount());     //got the Row Count as 1

if(obj.next())

{

    obj.reason = 'New Phone Request';

    obj.update();     //trying to update the variable of RITM record

}

RITM_Variables.JPG

However, whenever I am running this script, variable *Reason for Order* remains the same as its previous value --> damaged device received.

Please let me any appropriate script modification.

Best Regards,

Puru

1 ACCEPTED SOLUTION

Chay2
Mega Guru

Hi Pururava,



you can write something like



obj.variables.variable_name;



so your script should be like



var obj = new GlideRecord('sc_req_item');


obj.addQuery('sys_id','dc60c6eddb14430020a656f0cf961988');


obj.query();


gs.log("Record number is :"+obj.getRowCount());     //got the Row Count as 1


if(obj.next())


{


    obj.reason = 'New Phone Request';


    obj.variables.variable_name = 'name';


    obj.update();     //trying to update the variable of RITM record


}


View solution in original post

3 REPLIES 3

Chay2
Mega Guru

Hi Pururava,



you can write something like



obj.variables.variable_name;



so your script should be like



var obj = new GlideRecord('sc_req_item');


obj.addQuery('sys_id','dc60c6eddb14430020a656f0cf961988');


obj.query();


gs.log("Record number is :"+obj.getRowCount());     //got the Row Count as 1


if(obj.next())


{


    obj.reason = 'New Phone Request';


    obj.variables.variable_name = 'name';


    obj.update();     //trying to update the variable of RITM record


}


Chuck Tomasi
Tera Patron

Variables are access through the variables object.



You need to set this like ths



obj.variables.reason = 'Your new reason';


Sharique Azim
Mega Sage

Hi Puruva,




If the workflow is on the RITM table itself, you can simply use the line current.variables.variable_name = 'New Phone Request';; // if a variable



else current.variable_pool.variable_name = 'New Phone Request';; //if variable a part of variable set.