Record Producer to update location records

galavodasal
Giga Expert

Trying to develop a catalog item for end users to use to update our districts (cmn_location).

I started making a record producer with one reference field to look up the district. A client script then enters all of the existing district information into various string fields.

I want the user to be able to update any field which would then update the respective district CI. I don't mind if all of the fields are updated on the location record upon submission, regardless of whether or not the user actually changed the field on the record producer.

So I think I just need to create a simple workflow with a script action. The script is what I have questions on. I haven't tested it out yet, just figured I'd run it by the community first. Maybe there's a better way to handle this. Thanks in advance.

var gr = new GlideRecord('cmn_location');

\\not sure what to query here -- gr.addQuery("name", district) -- with "name" being the Name field on the cmn_location table and "district" being the variable name on the record producer?

or can I use something like:

gr.addQuery("name", current.variables.district);

  gr.u_mailing_address = current.variables.mailing_address;

  gr.u_mailing_city = current.variables.mailing_city;

  gr.u_mailing_state = current.variables.mailing_state;

  gr.u_district_manager = current.variables.district_manager;

  gr.update();

3 REPLIES 3

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Alexander, it is best practice not to use 'gr' for variable names and to instead come up with something unique for your code like "locationRec".   I have had a situation a few times where I was lazy and used 'gr' and then found that out of the box code also used 'gr' and my code failed since the object was overwritten.



I assume your district reference is to the location (cmn_location) so that will be storing the sys_id of the location.   The following code should work for you.



var locationRec = new GlideRecord('cmn_location');


locationRec.get(current.variables.district);


locationRec.u_mailing_address = current.variables.mailing_address;


locationRec.u_mailing_city = current.variables.mailing_city;


locationRec.u_mailing_state = current.variables.mailing_state;


locationRec.u_district_manager = current.variables.district_manager;


locationRec.update();


tanumoy
Tera Guru

var name = g_form.getValue('<district variable name>');



var gr = new GlideRecord('cmn_location');  


gr.addQuery('sys_id', name);


gr.query();


Thanks guys, this is very helpful. For some reason the workflow isn't even running, and I get a blank screen after hitting Submit on the record producer -- but that's another issue. Thank you, I'll let you know if I run into any issues once I get the workflow to run