- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 12:37 AM
Hi All,
I want to auto populate "asset_tag" value from asset table(alm_hardware) for selected user "requested_for" in service request. Could you please provide me the sample code to achieve this.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2018 04:01 AM
Hi Josmad,
The field you are trying to set is a reference field right? or it is a string field.
If it is a reference field it can hold only 1 value
if it is a string field then do you want to populate all the asset tags with comma separated in that field.
If yes then update script as below
getDetails: function(){
var arr = [];
var sysId =this.getParameter('sysparm_sysId');
var gr = new GlideRecord('alm_hardware');
gr.addQuery('assigned_to', sysId);
gr.query();
while(gr.next()){
arr.push(gr.asset_tag.toString());
}
if(arr.length > 0)
return arr.toString();
else
return '';
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 06:55 AM
Hi Josmad,
Following is the sample script:
give proper column name for asset_tag on the form
onChange Client Script on requested_for field:
var ga = new GlideAjax('TestFilter1'); ga.addParam('sysparm_name', 'getDetails'); ga.addParam('sysparm_sysId', newValue); ga.getXML(methodCallBack);
function methodCallBack(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('asset_tagField',answer);
}
Script Include:
var TestFilter1 = Class.create();
TestFilter1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function(){
var sysId =this.getParameter('sysparm_sysId');
var gr = new GlideRecord('alm_hardware');
gr.addQuery('assigned_to', sysId);
gr.query();
if(gr.next()){
return gr.asset_tag;
}
return '';
},
type: 'TestFilter1'
});
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 08:59 AM
Hi Ankur,
This code is working as expected if user has one asset.If user has more than one asset assigned, then it's populating only one asset instead of all.
Could you please help me on this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2018 03:47 AM
Hi Ankur,
Any suggestions.
thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2018 04:01 AM
Hi Josmad,
The field you are trying to set is a reference field right? or it is a string field.
If it is a reference field it can hold only 1 value
if it is a string field then do you want to populate all the asset tags with comma separated in that field.
If yes then update script as below
getDetails: function(){
var arr = [];
var sysId =this.getParameter('sysparm_sysId');
var gr = new GlideRecord('alm_hardware');
gr.addQuery('assigned_to', sysId);
gr.query();
while(gr.next()){
arr.push(gr.asset_tag.toString());
}
if(arr.length > 0)
return arr.toString();
else
return '';
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2018 06:58 AM
Thanks Ankur, it's working fine 🙂 May I know the reason why it is not worling for the reference variable ?