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

Just a guess, but "Title" is probably not the actual field name you need, you need the actual name of the field on the table, which at a minimum would be all lower-case "title". On the form you can right-click and select "show" on the field to see the field name. 

Just make sure the values are an exact match between the title field and value (not label) of the variable. 

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!

Yup, that was it, was using the label, lowercase t fixed it right up.

Thanks for your help on this.  Still trying to wrap my head around scripting, learning a bit at a time...though I feel like the community is doing all my work for me!

Hey Michael, another question for you.  Is there a way to modify this script so it only matches the first 80 characters of the value?

To be clear, are you saying the value for producer.u_var_strategic_alignment is only the first 80 characters of the title of the record you are trying to retrieve? Do those 80 characters comprise a unique value, or would there potentially be multiple matches on the table? If it is unique you could do something like this: 

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

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

if(sa.next()) {
//If we find a match set strategic alignment to match reference table option
current.u_strategic_alignment = sa.getUniqueValue();

}

 

I hope this helps!

Michael Jones - Proud member of the GlideFast Consulting Team!

 

 

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

Some of the records in the reference table are longer than 80 characters, but the max characters in the value field on the record producer variable is 80 characters, so if it tries to match the entire string it won't work since some of them are longer than the 80 character max it would be trying to match.

Hopefully that makes sense.

So my idea was to limit the match set to the first 80 characters so it would ensure it always finds the match.  The first 80 characters of each variable is unique.

I'll give that code a shot, thanks!