I want to auto populate the user details based on the user selection

kranthi2
Tera Expert

Hi,

I want to auto populate the user details based on the user selection in the Opened for:

 

kranthi2_0-1706013585358.png

 

Please help me out i want to use the Scriptinclude for the same.

 

Kindly help me out onChange and Scriptinclude for the same.

 

Thanks,  

2 ACCEPTED SOLUTIONS

Shaqeel
Mega Sage

Hi @kranthi2 

 

To auto-populate user details based on the user selection in the 'Opened for' field, you can use a client script in ServiceNow.

Here are the steps:

 

1. Navigate to System Definition > Client Scripts in ServiceNow.

2. Click on New to create a new client script.

3. Fill in the necessary fields:

- Name: Give a name to your client script.

- Table: Select the table where you want this script to run.

- Type: Select onChange.

- Field name: Select the 'Opened for' field.

4. In the Script field, write a script to fetch the user details.

Here is a sample script:

 

javascript

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

//Call a GlideAjax

var ga = new GlideAjax('GetUserDetails');

ga.addParam('sysparm_name', 'getUserDetails');

ga.addParam('sysparm_user_id', newValue);

ga.getXML(Answer);

}

 

function Answer(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

var userObj = JSON.parse(answer);

//Set the user details to the respective fields g_form.setValue('field_name', userObj.field_value);

}

 

5. In the above script, 'GetUserDetails' is a Script Include which has a function 'getUserDetails' that fetches the user details based on the user id.

6. Click on Submit to save the client script.

 

Remember to replace 'field_name' and 'field_value' with the actual field names and values you want to set.

This script will run every time a user is selected in the 'Opened for' field and auto-populate the user details in the respective fields.

 

Regards

Shaqeel

View solution in original post

6 REPLIES 6

Shaqeel
Mega Sage

Hi @kranthi2 

 

To auto-populate user details based on the user selection in the 'Opened for' field, you can use a client script in ServiceNow.

Here are the steps:

 

1. Navigate to System Definition > Client Scripts in ServiceNow.

2. Click on New to create a new client script.

3. Fill in the necessary fields:

- Name: Give a name to your client script.

- Table: Select the table where you want this script to run.

- Type: Select onChange.

- Field name: Select the 'Opened for' field.

4. In the Script field, write a script to fetch the user details.

Here is a sample script:

 

javascript

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

//Call a GlideAjax

var ga = new GlideAjax('GetUserDetails');

ga.addParam('sysparm_name', 'getUserDetails');

ga.addParam('sysparm_user_id', newValue);

ga.getXML(Answer);

}

 

function Answer(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

var userObj = JSON.parse(answer);

//Set the user details to the respective fields g_form.setValue('field_name', userObj.field_value);

}

 

5. In the above script, 'GetUserDetails' is a Script Include which has a function 'getUserDetails' that fetches the user details based on the user id.

6. Click on Submit to save the client script.

 

Remember to replace 'field_name' and 'field_value' with the actual field names and values you want to set.

This script will run every time a user is selected in the 'Opened for' field and auto-populate the user details in the respective fields.

 

Regards

Shaqeel

Pravershi
Tera Contributor

Hi @kranthi2 

Can you please try this in your requirement-

 

var  autoPop= Class.create();

autoPop.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

getFields: function() {

var sysId = this.getParameter('sysparm_user_sysid');

var userGR = new GlideRecord('sys_user');

if (userGR.get(sysId)) {

var userDetails = {};

userDetails.Contact_no. = userGR.getValue('preferred_contact_number');//use field backend name

userDetails.Issue = userGR.getValue('issue');

userDetails.CCID = userGR.getValue('ccid');

return new JSON().encode(userDetails);

} return null;

},

type: 'autoPop'

});

 

for onChange-

 

var ga = new GlideAjax('autoPop');

ga.addParam('sysparm_name', 'getFields');

ga.addParam('sysparm_user_sysid', g_form.getValue('user'));

ga.getXMLAnswer(function(answer) { var userDetails = new JSON().decode(answer);

g_form.setValue('preferred_contact_number', userDetails.Contact_no);

g_form.setValue('issue', userDetails.Issue);

g_form.setValue('ccid', userDetails.CCID);

}); }  

Hope this works!!

 

 

Hi,

kranthi2_0-1706019530620.png

 

While i am creating the script include i am getting this message.

Thanks,