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

seric
Kilo Contributor

Hi Matt;



This is great it works for Company but what If I am trying to populate lets say another field on that window i.e. Account. I have a reference field called Account on the user table. I also have it on the Incident form.   I have added it to the "new " user record form [reference list view] just like company, So, when I select Account on the incident form and i try to create a new caller from the incident form off that account. I want the account value to prepopluate just like the company above. How will the code be written. account will not be a dependent.


Sorry if this is a bit late. I actually came across an issue myself with having the filed set as a dependent value so came up with another solution. I think it should help you too. This solution also allows you to pass multiple values if needed.

 

1) create an onChange client script on the table you want to load the value(s) from which updates the reference picker onclick to included parameters with your value(s).

// override caller_id ref list icon onclick to add a reference to the selected company
// used to set correct company when teh new button is clicked
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  //get ref icon
  var refIco = gel('lookup.incident.caller_id');
  if (refIco) {
    // set new onclick
    refIco.onclick = function(e) {
      var url = reflistOpenUrl('incident.caller_id', 'incident.caller_id', 'caller_id', 'sys_user', null, false, 'session', '');
      url += '&sysparm_account_ref=' + newValue;
      // other params could be added here but this function would need to be defined elsewhere and
      // called from an onchange on each field you want to add
      popupOpenStandard(url, 'lookup');
      // stop original ref picker from loading
      e.stopPropagation();
      e.preventDefault();
    };
  }
}

 

2) create client script on the user table to load the value(s)

Type: onLoad
Global: false
View: sys_ref_list

// Set the account from sysparm_dependent or sysparm_account_ref 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 ref = /(sysparm_account_ref|sysparm_dependent)=([a-f0-9]{32})/i.exec(document.referrer);
    if (ref && ref[2]) {
      g_form.setValue('account', ref[2]);
      g_form.setReadOnly('account', true);
    }
  }
}