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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Josmad,

What relationship exist between user and alm_hardware table?

Is there a field on alm_hardware which refers to user table based on which you can fetch the asset tag?

Also will there be only 1 record in alm_hardware table for a user?

Regards

Ankur

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

"assigned_to" is the field name in "alm_hardware" which refers to the user table. 

Hi Josmad,

Write onChange on requested_for call a script include through GlideAjax and send the requested_for value

Query alm_hardware table with this against assigned_to value and send asset_tag from script include function, then set the value.

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

Can you please provide me code if you have any sample codes. It could be helpful to me.