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

Autopopulate the variables in the catalog item based on the Requestor name field

Servicenow de11
Tera Contributor

Hello,

 

In the catalog item variables if the user selected the name i want to autopopulate the location and company  both are reference field in the sys_user table. Can anyone sugget the scriptinclude and client acript to autopulate.

 

Thanks in advance.

 

 

2 ACCEPTED SOLUTIONS

Tai Vu
Kilo Patron
Kilo Patron

Hi @Servicenow de11 

Try the below trick.

  1. Define your user variable. Sample below as "Requested for"
  2. Create the Location variable
  3. At the section "Auto-populate", set similar to the below screenshot.

TaiVu_0-1697007851589.png

 

You can do the same for the Company variable. 

 

Let me know if it works for you!

 

Cheers,

Tai Vu

View solution in original post

Hi @Servicenow de11 

 

As you mentioned. location & company fields are of type "Reference".

It should not populate sys_id.

 

Can you check , both variables on catalog item are of type "reference" or Single line text.

 

If its single line text then update script include below :

 

  if (grUser.next()) {
            /*4. If user present then store values in JSON object */

            result.location = grUser.location.name;  //updated
            result.company = grUser.company.name; //update
            
        }

		/*5. Stringify the object and then return */
        return JSON.stringify(result);

 

 

 

OR you can go with Auto-populate option suggested by @Tai Vu you just need to dot-walk on "Name" field.

like location.name  & company,name

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

8 REPLIES 8

Vishal Birajdar
Giga Sage

Hi @Servicenow de11 

 

Please find below :

 

1.Script include -

 

var userUtils = Class.create();
userUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getUserDetails: function() {

        /*1. Declare the JSON object to return the value to client script */
        var result = {
            location: '',
            company: '',
            };

        /*2. Get the user from client script */
        var user = this.getParameter('sysparm_user');

        /*3. Glide record on User table to get values */

        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', user);
        grUser.query();

        if (grUser.next()) {
            /*4. If user present then store values in JSON object */

            result.location = grUser.getValue('location');
            result.company = grUser.getValue('company');
            
        }

		/*5. Stringify the object and then return */
        return JSON.stringify(result);
    },

    type: 'userUtils'
});

 

2. onChange client script on User variable

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading) {
      return;
   }

   /* 1. Get user value */
   var user = g_form.getValue('u_user'); // use your "user"  variable value

   /* 2. Make a asynch ajax call 'userUtils' script include*/
   var ga = new GlideAjax('userUtils');   // Script incldue name
ga.addParam('sysparm_name','getUserDetails');
ga.addParam('sysparm_user',user);
ga.getXMLAnswer(callBackFun);
   
}

function callBackFun(answer){

var result = JSON.parse(answer);

/*3. Set the values on form */
g_form.setValue('location_variable',result.location);
g_form.setValue('company_variable',result.company);

}

 

Note : you can adjust the name & variables as per yours....!!

 

Hope this helps...!!

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi Vishal,

 

 Tried your script Sysid is populating in the variables

Hi @Servicenow de11 

 

As you mentioned. location & company fields are of type "Reference".

It should not populate sys_id.

 

Can you check , both variables on catalog item are of type "reference" or Single line text.

 

If its single line text then update script include below :

 

  if (grUser.next()) {
            /*4. If user present then store values in JSON object */

            result.location = grUser.location.name;  //updated
            result.company = grUser.company.name; //update
            
        }

		/*5. Stringify the object and then return */
        return JSON.stringify(result);

 

 

 

OR you can go with Auto-populate option suggested by @Tai Vu you just need to dot-walk on "Name" field.

like location.name  & company,name

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Tai Vu
Kilo Patron
Kilo Patron

Hi @Servicenow de11 

Try the below trick.

  1. Define your user variable. Sample below as "Requested for"
  2. Create the Location variable
  3. At the section "Auto-populate", set similar to the below screenshot.

TaiVu_0-1697007851589.png

 

You can do the same for the Company variable. 

 

Let me know if it works for you!

 

Cheers,

Tai Vu