Script include and Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 11:09 PM
I am currently working on a requirement where I need to populate the manager's name when a user is selected in the 'Assigned to' field on the Incident table. I have created a 'Manager' field as read-only, and I've developed a Script Include along with an OnChange Client Script to achieve this functionality. However, I'm facing issues with fetching and displaying the manager's name upon selecting the user in the 'Assigned to' field.
Could someone please assist me with the correct implementation? Additionally, I would like to know if this is the best approach. Specifically, I am interested in knowing which method would require less coding while maintaining efficiency.
Thank you in advance for your assistance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 11:19 PM
Hi @Ashish Khairnar ,
The best approach would be a catalog lookup definition, so you don't need any coding at all.
Have a look at the docs: https://docs.servicenow.com/bundle/xanadu-servicenow-platform/page/product/service-catalog-managemen...
Or the article of Mark: https://www.servicenow.com/community/developer-articles/catalog-data-lookup-definition-on-any-table-...
If for some reason you do want to go for scripting, please share both script include and client script to get support.
Regards,
Hayo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 11:27 PM
Thank you @Hayo Lubbers Can you please Provide me the Code for Script include and onload Client Script thats the need now 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 01:02 AM
@Ashish Khairnar Apart from a script include + client script combination you can also chose to use g_form.getReference() method to populate the manager field. Here is an example.
function onChange(control, oldValue, newValue, isLoading) {
// Avoid running the script during form load
if (isLoading || newValue === '') {
return;
}
// Use g_form.getReference to get details of 'assigned_to'
g_form.getReference('assigned_to', function(assignedTo) {
if (assignedTo) {
// Check if the assignedTo record has a manager
if (assignedTo.manager) {
// Set the 'manager' field with the manager sys_id
g_form.setValue('manager', assignedTo.manager);
} else {
// Clear the manager field if no manager found
g_form.clearValue('manager');
}
}
});
}
Apart from this you can also use Auto Populate feature to auto populate the field in case if you do not wish to use scripts.
For more information please refer to https://rubenferrero.com/servicenow/auto-populate-variable-reference/
Please mark my response helpful and accepted solution if it addresses your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2024 01:26 AM
Hi @Ashish Khairnar ,
Please don't use getReference due to performance reasons. See e.g. https://www.servicenow.com/community/developer-forum/which-approach-is-best-getreference-or-glideaja...
I created a simple generic function so you can reuse it for every getValue retrieval via GlideAjax.
The script include is called "ajaxGetFieldValues" and the function "getValues", which is accepting 3 params (tableName, fieldNames end sysId).
It will retrieve the fieldValues for the given table/sys_id and returns it as an object. You can retrieve multiple values at once. It returns both value and displayValue.
var ajaxGetFieldValues = Class.create();
ajaxGetFieldValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/*
Get the values of the specified fields for the specified table
Input :
- tableName
name : sysparm_tableName
type : string
example : "incident"
- fieldNames
name : sysparm_fieldNames
type : string -> comma seperated list
example : "number, short_description"
- sysId
name : sysparm_sysId
type : string
example : "800b174138d089c868d09de320f9833b"
*/
getValues : function(tableName, fields, sysId){
tableName = this.getParameter("sysparm_tableName") || tableName;
fields = this.getParameter("sysparm_fieldNames") || fields;
sysId = this.getParameter("sysparm_sysId") || sysId;
var returnObj = [];
try{
var fieldsObj = fields.split(",");
var gr = new GlideRecord(tableName);
//Validate the existence of the table
if(gr.isValid()){
//Retrieve the record
if(gr.get(sysId)){
for(var i = 0; i < fieldsObj.length; i++){
//Validate the existence of the field
if(gr.isValidField(fieldsObj[i])){
returnObj.push({"fieldName" : fieldsObj[i], "value" : gr.getValue(fieldsObj[i]), "displayValue" : gr.getDisplayValue(fieldsObj[i])});
}
}
return JSON.stringify(returnObj);
}
}
return "[]"
}catch(e){
return "[]";
}
},
type: 'ajaxGetFieldValues'
});
Example client script, where I retrieve only the manager field of the user you've pushed into the assigned_to field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('global.ajaxGetFieldValues');
ga.addParam('sysparm_name', 'getValues'); //name of your function
ga.addParam('sysparm_tableName', "sys_user");
ga.addParam('sysparm_fieldNames', "manager"); //the fieldnames, comma separated
ga.addParam('sysparm_sysId', newValue); //the sysId of the record you want to retrieve
ga.getXMLAnswer(function(answer) {
var result = JSON.parse(answer);
var resultLength = result.length;
debugger;
if(result && resultLength > 0){
for(var i = 0; i < resultLength; i++){
//your logic to push the values into the right fields
//e.g. the manager should be set into the description field
if(result[i].fieldName == 'manager'){
g_form.setValue('description', "The value of the manager is : " + result[i].value + ", and the displayValue is " + result[i].displayValue);
}
}
}
});
}
I also attached the examples as XML, so you can import them in your PDI to check them out.
Of course you can simplify it even more, by just retrieving the manager, but that would give you work every time you need a value via GlideAjax.
Regards,
Hayo
Please hit like and mark my response as correct if it solved your issue