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
Kilo Sage

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
Kilo Sage

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!

@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.

 

@NityaB161013953 : 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)