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

Hi, can you pls help me

sheddyshiri
Tera Contributor

This question was asked in an interview :

we have a catalog form, where we have employee, email, company fields so, when employee field is something then email and company fields should auto populate 

9 REPLIES 9

GlideFather
Tera Patron

Hi @sheddyshiri,

 

it requires several steps - one thing is to have these fields upon loading the form > user loads the page, it's already pre-filled with their info, if some of the field changes it should also reflect the change.

 

Do you need both of these approaches?

 

To set the default value > go to the variable and find a field called Default, there is the place where you can add some values.

 

For the employee - is it a reference field? If so, you can use a simple line:

javascript: gs.getUserID();

and when you load the form it will be the currently logged in user.

 

For the email:

javascript: gs.getUser().getEmail();

 

For the company or any other details you might need to create an onLoad catalog client script calling Ajax. And the same for changes > onChange catalog client script.

 

Let me know if this helped you.

 

 

EDIT: there is automatic transcription of "javascript" and followed by ":"

 

It shall look like this:

GlideFather_0-1758788724209.png

 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Anand2799
Tera Guru

Hi @sheddyshiri ,

 

On variables record you have a Auto populate tab/section where you can set the fields which should auto populate this field based on other field (Reference).

for Eg. If you have Employee field of reference type and two other fields email and company which you want to populate whenever employee changes than you can set auto populate functionality on email and company variables.

**Make sure end users /catalog users have read access to the data, this functionality enforces ACLs.

Anand2799_0-1758795000679.png

 

If there are ACLs applied to the data and you don't want to create new ACLs, then you can create a client callable script include and call it in an onChange client script using GlideAjax.

 

If you want to set default value to Employee field when user opens the form then you can set default value to the Employee variable.

javascript: gs.getUserID();

 

Thanks

Anand

raviteja1600
Tera Guru

Hi @sheddyshiri ,

 

You can follow the below ways

 

1. Populate the Logged in user in the Employee field by setting the variable default values as follows

javascript:gs.getUserName() // for username
javascript:gs.getUserDisplayName() // for full name
javascript:gs.getUser().getEmail() // for email

raviteja1600_0-1758795847342.png

 

 

2. You can use the GlideAjax to populate the values of the Email and company based on th Employee field as follows below;

2.1 On Change Client Script

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

  // Get the selected employee's sys_id
  var employeeSysId = g_form.getValue('employee');

  // Use GlideAjax to call a Script Include to fetch email and company
  var ga = new GlideAjax('GetEmployeeDetails');
  ga.addParam('sysparm_name', 'getDetails');
  ga.addParam('sysparm_user_id', employeeSysId);
  ga.getXMLAnswer(function(response) {
    var result = JSON.parse(response);
    g_form.setValue('email', result.email);
    g_form.setValue('company', result.company);
  });
}

GlideAjax Script Include:

var GetEmployeeDetails = Class.create();
GetEmployeeDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getDetails: function() {
    var userId = this.getParameter('sysparm_user_id');
    var userGR = new GlideRecord('sys_user');
    if (userGR.get(userId)) {
      var result = {
        email: userGR.email,
        company: userGR.company.name
      };
      return JSON.stringify(result);
    }
    return JSON.stringify({ email: '', company: '' });
  }
});

 

Make sure the Script Inlcude should check the "Client callable/GlideAjax Enabled"

 

3. You can use the autopopulate function (OOB function) in the variable of the catalog item

raviteja1600_1-1758795871160.png

Proivde the dependent question and provide the reference field, it will populates and make sure there is no ACL restrictions on the table & table field.

 

If the above information is helpful to you, please mark it as helpful and accept the solution.

 

Regards,

Raviteja

Ankur Bawiskar
Tera Patron
Tera Patron

@sheddyshiri 

if employee variable is reference to sys_user and email (string), company (reference) then you can use auto populate feature and no scripting required

Auto-populate a variable based on a reference type variable (Utah) 

OR

You can use onChange + getReference callback

OR

You can use onChange + GlideAjax

If my response helped please mark it correct and close the thread so that it benefits future readers.

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