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

dgarad
Giga Sage

Hi @kranthi2 

 

Please refer below link 

https://www.servicenow.com/community/developer-forum/autopopulate-user-details-onchange-of-user-name...

 

Regards,

Dharmaraj

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

View solution in original post

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


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

View solution in original post

6 REPLIES 6

Hi @kranthi2 ,

Sorry for the late response 

You can select the user or system admin role that calls the script.

 

Thanks.

nityabans27
Mega Guru

You can auto-populate user details in Virtual Agent by using a Lookup Utility to select a user and then a Script block to fetch fields like location, department, and manager from the sys_user table using GlideRecord. Store them in "vaVars" to use later in the flow.