Auto populate assets assigned to user

josmad
Tera Contributor

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. 

 

1 ACCEPTED SOLUTION

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

21 REPLIES 21

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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. 

josmad
Tera Contributor

Hi Ankur,

Any suggestions. 

thanks!

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks Ankur, it's working fine 🙂 May I know the reason why it is not worling for the reference variable ?