Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Business Rule script to populate a reference field from a string field

Kev McLeod
Kilo Contributor

Hi all

 

I have a String field "u_wrnumber" that is populated from the email body using a business rule

 

i need to populate a Reference field "Parent" from the u_wonumber fiild and have tried the following script in a business rule that is not working

These are the two fields on the form

find_real_file.png

 current.parent=current.u_wrnumber;

find_real_file.png

i can manually put the WR Number in the Parent field as it exists in the task table

could anyone please assist with how i populate the parent field

 

Thankyou

 

9 REPLIES 9

Ankur Bawiskar
Tera Patron

Hi Kev,

So you are sure that value exists in the table which is referred by Parent; then try this; also that value should be display value 

 current.setDisplayValue('parent',current.u_wrnumber);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Kev McLeod
Kilo Contributor

 

Cheers Ankur

the script still doesnt populate the parent field

This is how the form appears when i open it

find_real_file.png

if i start yping in the number, it appears and saves succesfully

find_real_file.png

The script looks like this

find_real_file.png

Hi Kev,

Just remove line number 4 and keep only line 5 and try once

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Not applicable

Hi Kev,

 

Your script is not working as Reference Field takes sys_id and not value.

Moreover, as the BR will run before Insert or Update and parent is a mandatory field, it should cause an error during form submission 

As an alternative You could use an onChange Client Script for the WR Number Field:

 

var gr = new GlideRecord('parent field table name');
gr.addQuery('name of field',g_form.getValue('u_wrnumber'));
gr.query();
if(gr.next())
g_form.setValue('parent',gr.sys_id);

 

For your current solution remove

 current.parent=current.u_wrnumber; and only keep the second line of the code.