- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 04:40 AM
Hi,
I want to auto populate the user details based on the user selection in the Opened for:
Please help me out i want to use the Scriptinclude for the same.
Kindly help me out onChange and Scriptinclude for the same.
Thanks,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 04:50 AM
Hi @kranthi2
Please refer below link
Regards,
Dharmaraj
Thanks
dgarad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 05:18 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 04:50 AM
Hi @kranthi2
Please refer below link
Regards,
Dharmaraj
Thanks
dgarad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 05:18 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 05:20 AM
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!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 06:19 AM
Hi,
While i am creating the script include i am getting this message.
Thanks,