- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 01:39 PM
Dear Community Members,
I've been working on a form, where the model name and warranty end date fields are auto populated based on the chosen user and his/hers asset: This has been achieved by using reference qualifiers only, because I'm not good in scripting.
Unfortunately, this approach has been rejected by the Project Lead. It has been said that it should be done via a script.
This is the point where my problem starts, because I'm new to ServiceNow and my scripting skills are less than zero.
I've tried to find something on the forum, but it all is pure black magic to me.
Could you please give me a hand with this and suggest any examples or something I could start with, which won't make me confused?
My variable set name is: "pc_replacement"
My variables are:
- "damaged_pc" (table: "alm_hardware", field name: "display_name")
- "model_of_pc" (table: "alm_hardware", field name: "model")
- "warranty_end_date" (table: "alm_hardware", field name: "warranty_expiration")
If there is additional information needed, please let me know!
Thank you in advance!
Regards,
Dawid
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 11:38 AM
Hi DT,
Hopefully this is the fix.
Clear the label field values on Model of PC field
Replace scripts:
Client script: populateAssetDetails
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var glide = new GlideAjax('populateAssetInfo');
glide.addParam('sysparm_name', 'populateAssetDetails');
glide.addParam('sysparm_damagedpc', g_form.getValue('damaged_pc'));
glide.getXML(updateFields);
}
function updateFields(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = JSON.parse(answer);
g_form.clearOptions('model_of_pc');
g_form.addOption('model_of_pc', returneddata.display_name, returneddata.display_name);
g_form.setValue('warranty_end_date', returneddata.warranty);
} else {
g_form.setValue('warranty_end_date', clearvalue);
}
}
Script include: populateAssetInfo
var populateAssetInfo = Class.create();
populateAssetInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
populateAssetDetails: function(){ //function being called asynchronously from client script
var damagedpc = this.getParameter('sysparm_damagedpc'); //store the forms damaged pc sys id value
var assetGR = new GlideRecord('alm_hardware'); //open a search on the alm_hardware table
assetGR.addQuery('sys_id', damagedpc); //filter for a record with sys id of the sys id from before
assetGR.query(); //look if theres a match
if(assetGR.next()){ //if there is a match for the sys id
gs.log('[test] found a match');
var json = new JSON();
var assetDetails = { //create a new object to store information
"model" : assetGR.model.toString(), //store the model of the asset
"warranty" : assetGR.warranty_expiration.toString(), //store the warranty expiration date
"display_name" : assetGR.model.display_name.toString()
};
gs.log('[test] model: ' + assetGR.model + ' / warranty ' + assetGR.warranty_expiration + ' / display_name: ' + assetGR.model.display_name + ' / ' + assetDetails.display_name);
return JSON.stringify(assetDetails); //return the object (encoded)
}else{
return null; //if we didnt find a record, return empty
}
},
loadAssetsForUser: function(){
var impacteduser = this.getParameter('sysparm_impacteduser');
var assetGR = new GlideRecord('alm_hardware');
assetGR.addQuery('assigned_to', impacteduser);
assetGR.query();
var assetList = [];
while(assetGR.next()){
var assetObj = {
"display_name":assetGR.display_name.toString(),
"sys_id":assetGR.sys_id.toString()
};
assetList.push(assetObj);
gs.log('[test] pushing assetGR.display_name: ' + assetGR.display_name);
}
return JSON.stringify(assetList);
},
type: 'populateAssetInfo'
});
and theres no need to change the other client script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 01:43 PM
Can you share what your reference qualifiers look like? Then i'll probably be able to write the script for you and comment it so that you can read it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 01:58 PM
Hi Dan,
sure, here they are:
- for "Choose the damaged PC":
Lookup value: display_name
Reference qualifier: javascript: 'assigned_to.name='+current.variables.impacted_user;
Variable attributes: ref_qual_elements=current.variables.impacted_user
- for "Model of the Damaged PC":
Lookup value: model
Reference qualifier: javascript: 'display_name='+current.variables.damaged_pc;
Variable attributes: ref_qual_elements=current.variables.damaged_pc
- for "Warranty End Date":
Lookup value: warranty_expiration
Reference qualifier: javascript: 'display_name='+current.variables.damaged_pc;
Variable attributes: ref_qual_elements=current.variables.damaged_pc

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2022 06:20 PM
Hi DT,
I have tested this in my PDI and it is working.
You need to create two records.
A script include
Name: populateAssetInfo
Client callable: true
Script:
var populateAssetInfo = Class.create();
populateAssetInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
populateAssetDetails: function(){ //function being called asynchronously from client script
var damagedpc = this.getParameter('sysparm_damagedpc'); //store the forms damaged pc sys id value
var assetGR = new GlideRecord('alm_hardware'); //open a search on the alm_hardware table
assetGR.addQuery('sys_id', damagedpc); //filter for a record with sys id of the sys id from before
assetGR.query(); //look if theres a match
if(assetGR.next()){ //if there is a match for the sys id
var json = new JSON();
var assetDetails = { //create a new object to store information
"model" : assetGR.model.toString(), //store the model of the asset
"warranty" : assetGR.warranty_expiration.toString() //store the warranty expiration date
};
return JSON.stringify(assetDetails); //return the object (encoded)
}else{
return null; //if we didnt find a record, return empty
}
},
type: 'populateAssetInfo'
});
Image for reference:
A client script - open your variable set and in the menu, click configure -> Client scripts -> New
name: populateAssetDetails
Applies to: A variable set
Type: onChange
Variable set: pc_replacement
variable name: damaged_pc
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var glide = new GlideAjax('populateAssetInfo'); //glide is a instantiated object for class populateAssetInfo (which is the Script include name)
glide.addParam('sysparm_name', 'populateAssetDetails'); //name of the function we will call in the script include (populateAssetDetails)
glide.addParam('sysparm_damagedpc', g_form.getValue('damaged_pc')); //pass the script include the value for damaged_pc that is set on the form
glide.getXML(updateFields); //updateFields is the function below
}
function updateFields(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); //response is the asynchronous response from the script include function and answer is storing it
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = JSON.parse(answer); //returned data is now the holding the object and its properties from the script include
g_form.setValue('model_of_pc', returneddata.model); //set form field model of pc
g_form.setValue('warranty_end_date', returneddata.warranty); //set form field warranty
} else {
g_form.setValue('model_of_pc', clearvalue); //set to empty if there was an issue
g_form.setValue('warranty_end_date', clearvalue); //set to empty if there was an issue
}
}
Image for reference:
Lastly, you do need to keep one reference qualifier. The one that is currently being used on the following:
- for "Choose the damaged PC":
Lookup value: display_name
Reference qualifier: javascript: 'assigned_to.name='+current.variables.impacted_user;
Variable attributes: ref_qual_elements=current.variables.impacted_user
Let me know if any issues
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2022 03:15 AM
Hi,
I've created the script include and client script as instructed, removed the reference qualifiers and variable attributes from "Model ..." and "Warranty ..." and kept the settings for "Choose the damaged PC" as they are. Unfortunately it doesn't work for me, it shows the "none" option:
Maybe it is my bad I didn't mention that choosing the impacted user is the trigger. Not sure if this part is important. The scenario is that after choosing the impacted user, the field "choose the damaged pc" is limited only to the assets that are assigned to the particular user. The user is chosen from the sys_user table. The one I chose for testing purposes has two assets assigned, Dell and HP.
Hope I didn't make it more confusing with this explanation.