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.

How to map a reference variable to a work order reference field

amy_rodriguez
Tera Contributor

Hi!

 

I have a record producer I am working on where a user can select the building name (common_building_name | reference field for table A) of their choosing.  After the user submits their form, this building name is supposed to map to the "location" field on the work order created (location | which is a different reference field for table B).  

 

Since they are both different tables, I checked each one and saw both tables had the 'name' field with the same building names so I tried using: 

1. current.location = producer.common_building_name 

- this did not work, I thought maybe since the variable name was inside a Variable Set that I had to add that before the variable name...

2. current.location = producer.ritm_common_building_set.common_building_name 

- this also didn't work. I had some other values that needed to map from the same variable set and found you did not need the variable set infront of the variable name for it to work, so #1 should have worked but it doesnt. 

3. current.location = 'sys_id of common building name' 

- didn't work 

4. current.location = producer.common_building_name.getDisplayValue() 

- also did not work. 

 

At this point I am stuck. Is there a way to successfully do this within the record producer script? or do I have to create a function? and if I do, can anyone point me to resources on how to get this started? 

 

I provided pictures below. Please note that address' are also the same for both tables, but I have blurred them out. 

 

Thanks. 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Amy,

A reference field or variable stores the sys_id of the selected record.  Since the reference variable is referencing the Building table, your attempts with the simple assignment logic have been trying to use that sys_id to find the same sys_id on the Location table, which will not happen.  You can do a quick GlideRecord inside this script to find the Location record where the Name matches the Name of the selected building:

var loc = new GlideRecord('cmn_location');
loc.addQuery('name', producer.common_building_name.name);
loc.query();
if (loc.next()) {
	current.location = loc.sys_id;
}

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hi Amy,

A reference field or variable stores the sys_id of the selected record.  Since the reference variable is referencing the Building table, your attempts with the simple assignment logic have been trying to use that sys_id to find the same sys_id on the Location table, which will not happen.  You can do a quick GlideRecord inside this script to find the Location record where the Name matches the Name of the selected building:

var loc = new GlideRecord('cmn_location');
loc.addQuery('name', producer.common_building_name.name);
loc.query();
if (loc.next()) {
	current.location = loc.sys_id;
}

Thank you! this worked for me 🙂 

You are welcome!

 

 

Connect with me https://www.linkedin.com/in/brad-bowman-321b1567/