User Details auto population in Problem Management

johnjosephjose
Tera Contributor

Hi, 

 

I'm trying to auto populate some user details in the Problem Management Module with 'User' field as the main field, then populates the rest. 

I am new to scripting so I am not sure how the approach will be. 

 

johnjosephjose_0-1696414711656.png

 

Thanks! 

 

5 REPLIES 5

Omkar Kumbhar
Mega Sage
Mega Sage

Hello @johnjosephjose ,

Please find the below client script and script include.

 

1)On change Client Script

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

    //fetch value of user
    var creator = g_form.getValue('field value name of user');


    // 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('email', userObj.grMail);
        g_form.setValue('department', userObj.department);
        g_form.setValue('user_id', userObj.userID);

    });

}

 

Client callable 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.department= gr.department.getDisplayValue();
            userObj.userID= gr.user_id.getDisplayValue();

   }

        return JSON.stringify(userObj);
    },

    type: 'populateDetails'
});

 

Please change the backened name of fields according.

 

Thank you,

Omkar

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

praneeth7
Tera Guru

Hi @johnjosephjose 

you can write a on change Client script on desired table

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

    var user = g_form.getReference('caller_id', details); //instead of 'caller_id' pass 'user' field name here

    function details(user) {
        var email = user.email;
        var userID = user.user_name;
        var dept = user.department;
        g_from.setValue('email', email);
        g_from.setValue('user_name', userID);
        g_form.setValue('department', dept);
    }

}

 

if you find the useful please mark my answer as Helpful and Accepted solution,

Thank you

Vishal Birajdar
Giga Sage

Hi @johnjosephjose 

 

You can follow the below steps :

 

Step 1 : Create Client callable script include 
Name : userUtils
function name : getUserDetails

 

VishalBirajdar_0-1696417961752.png

 

 

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 = {
            userId: '',
            email: '',
            department: ''
        };

        /*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.userId = grUser.getValue('user_name');
            result.email = grUser.getValue('email');
            result.department = grUser.getValue('department');  // hoping department field is reference field 
        }

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

    type: 'userUtils'
});

 

 

Step 2 : Write onChange Client script on 'User' field

 

VishalBirajdar_1-1696418081157.png

 

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

   /* 1. Get user value */
   var user = g_form.getValue('u_user'); // or can use 'newValue'

   /* 2. Make a asynch ajax call 'userUtils' script include*/
   var ga = new GlideAjax('userUtils'); 
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('u_user_id',result.userId);  //user your form fields backend name
g_form.setValue('u_email',result.email); //user your form fields backend name
g_form.setValue('u_department',result.department); //user your form fields backend name


}

 

 

Output :

 

VishalBirajdar_2-1696418220001.png

 

 

 

Vishal Birajdar
ServiceNow Developer

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

Jens Damhoej
Tera Contributor

My advise is to use FlowDesigner.

In that way you do not need to worry about upgrades