How to update variables using a script

johannes5
Giga Expert

Hi ServiceNow Community Developers,

Is there a way to update a variable on the sc_req_item using something like a batch script. I do this all the time with the incident table going after fields but it looks like its different when you are going after variables. For instance I have the following batch script

gs.log("running it");

var items = new GlideRecord('sc_req_item');

items.addQuery('number', 'RITM0011306');

items.query();

while (items.next()) {

  gs.log ("data found " + items.cat_item.name);

  gs.log ("data found " + items.variables.asset_name);

  items.variables.asset_name = 'No Asset';

  items.update();

}

Now asset_name is a variable and thats why my script does not work, if asset_name was a field on this table I am pretty sure it will work. Both my gs.log statements work fine, they give me the data I want, it's the update part that is not working and I think its because maybe I need to code it differently since I am dealing with a variable.

Would you please advise as to what do I need to do in order to get the desired functionality here.

Thanks,

Johannes

5 REPLIES 5

Carpenter
Tera Contributor

Hi Johannes,



Is your "asset_name" variable a reference variable?



If so, you need to replace:


items.variables.asset_name = 'No Asset';


with:


items.variables.asset_name.setDisplayValue('No Asset');


(assuming you have a record with that name).


Hi Michael,



Thanks for your response.



No my asset_name variable is not a reference variable, just free text.



Johannes


This has worked for me



var items = new GlideRecord('sc_req_item');


  items.addQuery('number', 'RITM0010001');


  items.query();


  if (items.next()) {


  gs.addInfoMessage("test");


  items.variables.original = 'No Asset';


  items.update();


  }


manikorada
ServiceNow Employee
ServiceNow Employee

Johannes,



I will try to keep this in a function and see if it works. It would be something like:


myfunction();



function myfunction()


{


gs.log("running it");


var items = new GlideRecord('sc_req_item');


items.addQuery('number', 'RITM0011306');


items.query();


while (items.next()) {


  gs.log ("data found " + items.cat_item.name);


  gs.log ("data found " + items.variables.asset_name);


  items.variables.asset_name = 'No Asset';


  items.update();


}


}