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

How to auto populate a reference field?

mfamphlett
Mega Expert

On our incident form we have the company and caller fields:

inc-company-caller.png

If a user clicks the reference icon:

inc-caller-popup.png

And then clicks the new button:

inc-caller-new.png

I would like to have the company field auto populated based on the company selected in the incident.

I have considered modifying the onclick of the reference picker so it inserts the sys id of the company into the url of the reference picker popup as a sysparm. Then using a client script on the sys_ref_list view of the user form to retrieve the sys id from the referring url. However, this involves dom manipulation in order to replace the onclick which I would like to avoid. Also we have a number of other similar situations in our system and the same would need to be implemented for all of them.

I was thinking there has to be a simpler way to achieve this but was unable to find a solution online. Does anybody have a better idea?

Thanks,

Matt

1 ACCEPTED SOLUTION

Okay so i've implemented this solution in a test system and it seems to be working nicely. I've posted it here for the benefit of others and for some constructive criticism.



1) Set the the the value of "Dependant" in the dictionary record of the user field on the incident form to "company".



2) Create the following client script on the user table:


Type: onLoad


Global: false


View: sys_ref_list


// Set the company from sysparm_dependent of the referrer if it's a new record and the view is sys_ref_list


// author: matt a



function onLoad() {


  if (g_form.isNewRecord() && typeof document !== 'undefined' && 'referrer' in document) {


      var referrer = document.referrer.toString();


      var i = referrer.indexOf('sysparm_dependent=');


      if (i !== -1) {


          var sysid = referrer.substr(i + 18, 32);


          if (/^[a-f0-9]{32}$/i.test(sysid)) {


              g_form.setValue('company', sysid);


              g_form.setReadOnly('company', true);


          }


      }


  }


}



This should work for any similarly related fields on other tables. I will actually be using this on account and contact tables within our prod instance. The use of the company and user was just an example to illustrate my issue.


View solution in original post

16 REPLIES 16

Harsh Vardhan
Giga Patron

Hi Matt,



Check this link. i hope you will get your answer.



Auto populate company name from reference field in user table




Regards,


Harshvardhan


No sorry that doesn't help. That's a completely different issue.



Thanks for the suggestion though.


Hi Matt,



Sorry i thought your requirement is just to populate value on same form.


Now i got it. i am really excited to know how to solve this.



goranlundqvist , i Hope you know the answer




Regards,


Harshvardhan


Hi,



Problem is just like you write, having the company name follow with you the whole time. I would have a a macro to put next to the caller field. pressing it would take you to the "new user" form and then it's easier to have the company to follow through with the sysparm_query. Like you are on a list and throw in a few filters, those filters comes with you if you press the new button.



By having a macro, it will be 1 less click as well for the user since they can press "new" directly from the form and don't need to go through the reference icon to get to new.