- 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-27-2022 12:09 PM
Hi Dan,
sorry for the delayed response. When I switch between the assets on the form, I get the alert "client script is working".
However when selecting the user, a pop-up appears "There is a JavaScript error in your browser console".
Not sure if this is the log entry, you were asking for?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2022 12:42 PM
Thanks DT, no problem.
The alerts/logs are showing that both the client script and the script include are being used which is good.
Did you also get a log starting with "[test] model:"?
I'm guessing not because the script will have failed at this point.
There are some issues due to the fields. Typically the damaged PC and model fields would be a reference field to fulfill this requirement.
I will do some testing in my PDI and reply back
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2022 09:54 AM
Hi
I can assist you here, but for that you need to confirm on below things:
1) Can you explain your requirement in brief here again as the above explanation is not clear at all to me.
2) Is the requirement is to autopopulate Model Name and Warranty expiration based on Damaged PC variable?
Or the requirement is different and you need to auto populate Model Name and warranty based on certain user selection? Is it a value you are selecting in the variable "Choose the impacted user" and then you need to query the Asset table and check if there is any asset assigned to that user and then fetch the details?
If yes, then follow the steps below:
1) Create a Client callable Script include and use the script as below:
var getAssetInfo = Class.create();
getAssetInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo : function(){
var selectedUser = this.getParameter('sysparm_user');
var gr = new GlideRecord('alm_hardware');
gr.addQuery('assigned_to',selectedUser);
gr.query();
if(gr.next()){
var obj = {};
obj.Model = gr.model.toString();
obj.Warranty = gr.getDisplayValue('warranty_expiration').toString();
}
return JSON.stringify(obj);
},
type: 'getAssetInfo'
});
Now create a On Change Catalog Client script on the variable "Choose the Impacted User" and use the script as below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue){
var ga = new GlideAjax('getAssetInfo');
ga.addParam('sysparm_name','getInfo');
ga.addParam('sysparm_user',newValue);
ga.getXMLAnswer(getDetails);
}
function getDetails(response){
var answer = response;
var parsedAnswer = JSON.parse(answer);
g_form.getValue('Variable Name',parsedAnswer.Model); // Replace "Variable Name" with Model Variable
g_form.getValue('Variable Name',parsedAnswer.Warranty); // Replace "Variable Name" with Warranty Expiration Variable
}
//Type appropriate comment here, and begin script below
}
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2022 10:58 AM
Hi Shloke,
the idea is to create a form to report a damaged asset. So basically it all depends on the chosen user and his or hers assigned assets. My test user has two assets, Dell and HP, but there might be other users that have more or less assets.
In case of my test user, if the selection will be the Dell asset, it should auto populate the model name and warranty of this Dell asset. If the selection will be HP, it should auto populate the model and warranty for this HP.
I've tested your script (replaced the variables names) but I get "There is a JavaScript error in your browser console" and the auto populate doesn't work.
If any questions, please let me know, thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 02:30 AM
Not an issue. I got your requirement and will assist you here to achieve the same.
1) Breaking your requirement into two parts here for better understanding on what we are doing:
First Part:
1) Based on Selected User in the Variable "Choose the Impacted User", we will first load the variable which is named as "Choose the Damaged PC" with the list of assets which are available for the selected user which can be of any number say 1 or 2 or anything.
So for this, Please write a Normal Script Include and not client callable one and use the script below:
Script Include:
var getAssets = Class.create();
getAssets.prototype = {
initialize: function() {
},
getUserAssets : function(userID){
var arr = [];
var gr = new GlideRecord('alm_hardware'); // Replace Hardware with any other table against which you want to check
gr.addQuery('assigned_to',userID);
gr.query();
while(gr.next()){
arr.push(gr.sys_id.toString());
}
return 'sys_idIN' + arr.toString();
},
type: 'getAssets'
};
Attaching screenshot of Script Include for reference as well below:
Now Open the variable named as "Choose the Damaged PC" and update the Reference Qualifier of this variable as below:
javascript: new ScriptInclude Name().Function Name(current.variables.Variable Name of User);
So it will be based on my Script Include Name as below:
javascript : new getAssets().getUserAssets(current.variables.VariableName); // Replace Variable Name correctly for the User here.
Screenshot attached as well:
Now Second part to your query is when Damaged Asset is selected , you need to populate the Model and Warranty details, so please write a On Change Catalog Client script and use the script as below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue){
var getDamagedAsset = g_form.getReference('Variable Name of Damaged Asset selected here', getDetails);
}
function getDetails(getDamagedAsset){
g_form.setValue('Variable Name of Model',getDamagedAsset.model);
g_form.setValue('Variable Name of Warranty Expiration',getDamagedAsset.warranty_expiration);
}
//Type appropriate comment here, and begin script below
}
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke