How can I set a default value of the requesters company on a record producer?

Daniel R2
Kilo Sage

Hi,


I am hoping I can get some help here. 

I have created a Company variable on a record producer with a reference field type.

 

How can I set a default value in the company field (variable) to show the requesters company? on the variable within employee centre. 

5 REPLIES 5

Mike_R
Kilo Patron
Kilo Patron

Set this as the default value for the variable

 

Mike_R_0-1666971781589.png

 

This is what we ended up using.

 

javascript: var company; var rec = new GlideRecord('sys_user'); rec.get(gs.getUserID()); company = rec.company; company;

Thank you for your reply and help.

Brad Bowman
Kilo Patron
Kilo Patron

Hi Daniel,

Not a default value per se, but it will look the same.  This example assumes you have a reference variable for requestor (named v_requestor).  The advantage to having the Requestor variable is it gives you the ability to allow someone to submit on behalf of someone else.  Then you can also have a similar script onChange of Requestor, so that Company is updated when the Requestor changes.  If you want to create the requestor variable, you can have it default to the current user by putting javascript: gs.getUserID() in the Default value field, and map it to Contact or whatever.  Next create an onLoad Catalog Client Script that will populate the Company variable (named v_company in this example) based on the company of the requestor variable.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

     var usr = g_form.getReference('v_requestor', userLookup);
    function userLookup(user) {
        g_form.setValue('v_company', usr.company);
    }
}

If you don't want a Requestor variable, your onLoad script would need to call a Script Include via GlideAjax, passing in the current user for the SI to return the Company.  Not necessarily more difficult, just more lines of code and another script.