- 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-28-2022 09:12 AM
After logging again to the instance, the Javascript error is gone 🙂
I've tried out various scenarios on the "look up label fields" value and even on the "look up value field", but the output is still the sysID.
Did you test it on native view or on portal?
Should this stay as a comment?
//assetList.push(assetGR.display_name.toString());

- 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-28-2022 01:39 PM
Yep, this is it! You're the man!
A big thank you!
I'll present it on tomorrow's project call and hope they won't come up with another crazy idea 🙂
Once again, thank you very much for your help!
Regards,
Dawid

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 01:54 PM
Great! you're welcome!
Using reference type for the model id would definitely be better than a lookup select box. This field type is quite unusual to use because it's read only and you're never using it to select a value.
Now that you have the go-ahead to change the field it might be worth doing it. You could take all the information on this thread and attempt to implement it yourself in a Personal Developer Instance which will surely reinforce your knowledge with scripting, field types, GlideRecord API, GlideAjax API, Catalog Client Scripts and Script includes. Once happy with it in a PDI, you could do the same on your work instance.
Good luck!