Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Autopopulate user details onChange of user name in Catalog Item

Aju Sam
Tera Contributor

I have the following fields in my catalog item :

1. Name of creator(name_of_creator) - Reference from custom user table

2. Creator Mail id(creator_mail_id) - single line text

3. Process(process) - single line text

4. Supervisor mail id(supervisor_mail_id) - single line text

and want to auto populate Creator Mail id, Process and Supervisor mail id onChange of Name of creator.

I am relatively new to ServiceNow. Please explain the method to do this in a way a fresher can understand.

1 ACCEPTED SOLUTION

Mitsua
Tera Guru

Hello @Aju Sam 

 

You need to create a script include and catalog client script with type onChange.

You can use below script . (Tested on my PDI)

 

1)Catalog Client Script

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

    //fetch value of creator 
    var creator = g_form.getValue('name_of_creator');


    // call script include

    var userDetails = new GlideAjax('populateDetails');   // "populateDetails" name of script include
    userDetails.addParam('sysparm_name', 'details');  // "details" is the function name that present in script include
    userDetails.addParam('sysparm_userSelaected', creator);
    userDetails.getXMLAnswer(function(response) {
        var userObj = JSON.parse(response);


        g_form.setValue('creator_mail_id', userObj.grMail);
        g_form.setValue('supervisor_mail_id', userObj.grSupervisorMail);
        g_form.setValue('process', userObj.grProcess);

    });

}

Mitsua_0-1672763345003.png

 

 

 

2) Script Include

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

    details: function() { 
        var userObj = {};

	// fetch creator email , process and supervisor mail from user table(you need to use yout custome table name)
		
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_userSelaected'))) {
			
	
          // for below lines use backend names of email , and process fields of custom table
			
            userObj.grMail = gr.email.getDisplayValue(); 
            userObj.grProcess = gr.process.getDisplayValue();
            userObj.grSupervisorMail = gr.manager.email.getDisplayValue();

   }

        return JSON.stringify(userObj);
    },

    type: 'populateDetails'
});

 

@Aju Sam Let me know if any queries.

Please mark this as correct answer/accepted and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

 

Thanks!

View solution in original post

4 REPLIES 4

Mitsua
Tera Guru

Hello @Aju Sam 

 

You need to create a script include and catalog client script with type onChange.

You can use below script . (Tested on my PDI)

 

1)Catalog Client Script

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

    //fetch value of creator 
    var creator = g_form.getValue('name_of_creator');


    // call script include

    var userDetails = new GlideAjax('populateDetails');   // "populateDetails" name of script include
    userDetails.addParam('sysparm_name', 'details');  // "details" is the function name that present in script include
    userDetails.addParam('sysparm_userSelaected', creator);
    userDetails.getXMLAnswer(function(response) {
        var userObj = JSON.parse(response);


        g_form.setValue('creator_mail_id', userObj.grMail);
        g_form.setValue('supervisor_mail_id', userObj.grSupervisorMail);
        g_form.setValue('process', userObj.grProcess);

    });

}

Mitsua_0-1672763345003.png

 

 

 

2) Script Include

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

    details: function() { 
        var userObj = {};

	// fetch creator email , process and supervisor mail from user table(you need to use yout custome table name)
		
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_userSelaected'))) {
			
	
          // for below lines use backend names of email , and process fields of custom table
			
            userObj.grMail = gr.email.getDisplayValue(); 
            userObj.grProcess = gr.process.getDisplayValue();
            userObj.grSupervisorMail = gr.manager.email.getDisplayValue();

   }

        return JSON.stringify(userObj);
    },

    type: 'populateDetails'
});

 

@Aju Sam Let me know if any queries.

Please mark this as correct answer/accepted and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

 

Thanks!

nityabans27
Kilo Patron

 

🎯 Goal:

When a user selects a Name of creator, you want to automatically fill:

  • Creator Mail ID

  • Process

  • Supervisor Mail ID


🧱 Step-by-Step Guide:

Step 1: Identify Field Names

Make sure your variable names in the catalog item exactly match:

  • name_of_creator (reference to custom user table)

  • creator_mail_id (single line text)

  • process (single line text)

  • supervisor_mail_id (single line text)


Step 2: Add a Catalog Client Script

  1. Open your catalog item in Maintain Items (sc_cat_item).

  2. Scroll down to the "Catalog Client Scripts" related list.

  3. Click New to create a new client script.

Fill it out like this:

  • Name: Auto-populate fields from creator

  • Type: onChange

  • Variable name: name_of_creator

  • UI Type: All

Paste this into the script field:

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

  // Use GlideAjax to call a Script Include that fetches user details
  var ga = new GlideAjax('GetCreatorDetails');
  ga.addParam('sysparm_name', 'getDetails');
  ga.addParam('sysparm_user_sysid', newValue);
  ga.getXMLAnswer(function(response) {
    var result = JSON.parse(response);
    g_form.setValue('creator_mail_id', result.email);
    g_form.setValue('process', result.process);
    g_form.setValue('supervisor_mail_id', result.supervisor_email);
  });
}

Step 3: Create a Script Include to Fetch Data

  1. Go to System Definition → Script Includes.

  2. Click New, and configure it as:

  • Name: GetCreatorDetails

  • Client Callable: Checked

  • Accessible from: All Application Scopes

Paste this code:

var GetCreatorDetails = Class.create();
GetCreatorDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getDetails: function() {
    var userSysId = this.getParameter('sysparm_user_sysid');
    var userGR = new GlideRecord('your_custom_user_table'); // Replace with actual table name
    if (userGR.get(userSysId)) {
      var result = {
        email: userGR.getValue('email'),
        process: userGR.getValue('process'),
        supervisor_email: userGR.getValue('supervisor_email')
      };
      return JSON.stringify(result);
    }
    return JSON.stringify({});
  }
});

Important: Replace 'your_custom_user_table' with the actual name of your custom user table.


🔄 How It Works:

  • When user picks a name from the name_of_creator field…

  • The onChange client script sends a request to the Script Include with the selected user’s sys_id.

  • The Script Include looks up the user and returns the needed fields.

  • The script then fills those values into the catalog item fields.

 

@Aju Sam Please accept my solution and mark it as helpful, if it was found useful.

Thanks and regards

Nitya Bansal

@Aju Sam : 

I know this would be too late for your use, but can be used for future RPs, if this feature is not already known.

 

@nityabans27 : We should propose, low code / No code options as much as possible. I know we eat, breathe, sleep...code 🙂

 

I strongly recommend to use Auto populate feature which got introduced with Utah rather than going through scripting and reducing the technical debt.

 

BhaveshBijagar_0-1751436948774.png

 

There is this amazing article by @Mark Roethof  that explains this feature

 

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