Mapping second field in a record producer

Staxed
Giga Guru

I am working on a record producer.  At the moment I am using the default map to field option on a variable to correctly map it to a field.  However, I need to map that same variable to another field on the form as well.  The second field is a reference field that is pulling it's options from a table (the table records match exactly the options that are available in the record producer variable)

I'm trying to do it as simply as possible, so am currently trying to use the script field on the record producer to do something like:

current.u_strategic_alignment = producer.u_var_strategic_alignment;

The u_strategic_alignment variable is the one that is a reference to a table with the options.  Not sure what I'm doing wrong, or what the best way is to accomplish this.

Whenever I submit the catalog item record producer, it is not mapping the variable to that second field.

Not sure if that made sense, please let me know whatever clarification you need and I will provide.

1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

On your record producer variable, is the value a string or is it the sys_id of the matching record on the reference table? (My guess is that you have string values coded in your variable). 

If this is the case then you are trying to set the value of a reference field on the record, to the string value on the record producer, which won't match. The value of a reference field would need to be the sys_id of the record that matches your string value. So, you will probably need to do an intermediate lookup. Without knowing the details of your reference table I can't give you an exact example, but let's assume on your table you have a field called display_name, which is the value contained in your record producer variable, then something like this: 

//Initialize a new GR to your custom table
var sa = new GlideRecord('<enter_your_table_name');

//We want to get the record that has the display_name selected
sa.get('display_value', producer.u_var_strategic_alignment)

current.u_strategic_alignment = sa.getUniqueValue();

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

9 REPLIES 9

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi, Try to print "producer.u_var_strategic_alignment" and see what it returns.   Since it's a reference field it should return the sys_id of the record it is referring to. Also, Try the below script. Please make sure to adjust the table name and field column name.

var strat = producer.u_var_strategic_alignment; 
var parentGR = new GlideRecord('PASS TABLE NAME HERE'); 
parentGR.addQuery('name',strat); 
parentGR.query(); if (parentGR.next()) 
current.u_strategic_alignment = parentGR.getValue('sys_id'); 

 

producer.u_var_strategic_alignment is a string that is set with a Catalog Item choice list, not a sys_id.  Your code works to get an option added to the correct field in the form, but it always just chooses the last option in the list...because I'm assuming it's trying to match a sys_id to a string.

I tried using the code in Michael's response but although it should be matching the string to an appropriate sys_id option from the table, it still has the same result of just selecting the last item in the list every time instead of actually choosing the correct option.

Michael Jones -
Giga Sage

On your record producer variable, is the value a string or is it the sys_id of the matching record on the reference table? (My guess is that you have string values coded in your variable). 

If this is the case then you are trying to set the value of a reference field on the record, to the string value on the record producer, which won't match. The value of a reference field would need to be the sys_id of the record that matches your string value. So, you will probably need to do an intermediate lookup. Without knowing the details of your reference table I can't give you an exact example, but let's assume on your table you have a field called display_name, which is the value contained in your record producer variable, then something like this: 

//Initialize a new GR to your custom table
var sa = new GlideRecord('<enter_your_table_name');

//We want to get the record that has the display_name selected
sa.get('display_value', producer.u_var_strategic_alignment)

current.u_strategic_alignment = sa.getUniqueValue();

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Thank you, this is a huge leap in the right direction!  However, it is now partially working. It is selecting an option on the form for the Strategic Objective, though it seems to just be pulling the last item in the list of options in the Table, it isn't actually matching it to the correct one.  Any ideas?

 

//Initialize a new GR to reference table
var sa = new GlideRecord('strategic_objective');

//Get record that matches strategic alignment
sa.get('Title', producer.u_var_strategic_alignment);

//Set strategic alignment to match reference table option
current.u_strategic_alignment = sa.getUniqueValue();