How to assign a field value from a variable

robertlankford
Kilo Expert

I have a custom field that I have added to the sc_req_item table.   I also have a variable on an Order Guide that get's populated when the user fills the request out.

In workflow, I'd like to copy the variable value that the user filled out to the requested item's custom user field.

var gr_item = new GlideRecord("sc_req_item");

gr_item.addQuery("sys_id",current.sys_id);

gr_item.query();

while(gr_item.next()) {

  gs.log("name:requestedItem:" + gr_item.cat_item.name);

  gs.log("name:reqItem.AssProcCat:" + gr_item.getValue('u_assigned_procurement_category'));

  gs.log("name:CurrentCat:" + current.variables.procurement_cats);

 

  //gr_item.setValue('u_assigned_procurement_category', current.variables.procurement_cats);

  gr_item.setValue('u_assigned_procurement_category', 'it');

}

The value is hard-coded above to 'it'.   I would like to use something like current.variables.procurement_cats instead.   But I can't even see where it gets hard-coded to 'it' correctly.

What am I missing here?

3 REPLIES 3

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You don't need a script to accomplish this.   If you set the Name value of the variable to match the column name of a field, by default that field will be filled in with the value from the variable.   So based on your code, set the name of the variable to u_assigned_procurement_category and it should automatically set the field for you.


Thank you for the response.   Unfortunately, I'm actually trying to keep track of two different values.



The variable on the order guide is what the user selects when they fill out the request.



The field added to the requested item table should start out with that value (and that's what I'm trying to get my script to do).   But it can be altered later on as folks work the tasks created by the workflow.   In this case, I want to be able to reference the value that the user entered along with the new value that workers may specify.


Actually my recommendation still stands.   The value of the variable will get set into your sc_req_item field upon the creation of the record, but the value of that field and the variable can have different values over time.   They won't continue to sync.