The CreatorCon Call for Content is officially open! Get started here.

How would I autofill a value on a new record that was generated off a reference field

Shant Cancik
Tera Contributor

I have a reference field on my form called Company. I have another reference field on my form called Point of Contact User. Both fields are reference fields to separate tables and Point of Contact User is a child table to the Company table (all Point of Contact User records have a Company field on them).

When a user selects a company on my form, the Point of Contact User field filters to just offer records that have that matching Company value in them. 

What I'm trying to do now is give users the ability to create new Point of Contact records for that company directly from that form. So if a user selected "Company 1" on the Company field , clicked on the magnifying glass on the Point of Contact field, and saw that there were no Point of Contact records for Company 1, I'd want to give them the ability to add a new record. 

The reference field list offers a "New" button but when clicked on the Company 1 value doesn't auto populate to the new record. I need to figure out a way to consider the Company value that was entered on the form and pass it to this reference field new record pop up screen.

 

3 REPLIES 3

Ruhi Jibhyenka1
Mega Guru

Hi Shant,

Try below example and make changes in code according to your requirements(eg- change table and fieldnames)-

You can create Display BR to capture the values from incident form in g_scratchpad and the use then in onLoad client script to auto populate the data when New Problem form loads.

Please try in this way:

Kindly TRY with below code in Display BR on Problem Table.

(function executeRule(current, previous /*null when async*/) {

 

  // Add your code here

 

  var gr = new GlideRecord('incident'); // Please enter the parent table name

 

  gr.get(parent.sys_id);

 

  g_scratchpad.value1 = gr.getValue('<name of the reference field from parent table>');

 

})(current, previous);

 

Client Script: on Problem Table.

function onLoad() {

  //Type appropriate comment here, and begin script below

  if(g_form.isNewRecord()){

  g_form.setValue('<name of the field on child table>', g_scratchpad.value1 );      

  }

}

Mark correct if my answer has solved your issue and/or helpful if my response is worthy.

Thanks,

Ruhi.

Ankur Bawiskar
Tera Patron
Tera Patron

@Shant Cancik 

Try checking what is shown in the url; that would help you

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Shant Cancik 

Any update on this thread

Regards
Ankur

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