The CreatorCon Call for Content is officially open! Get started here.

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,

Reference variable is defined to store only 1 reference record so it can hold only single value i.e. sys_id

Regards

Ankur

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

@Ankur Bawiskar how can i put on form onload ? 

 

getting error  :

 

chercm_0-1702258914057.png

 

Hi @chercm 

 

Use Auto populate 

 

LearnNGrowAtul_0-1702301074803.png

 

LearnNGrowAtul_1-1702301074986.png

 

Actually there's not much more to write on this, the Auto-populate functionality is just so straightforward! Have a Variable of type reference on your Catalog Item, create a new Variable that you want to Auto-populate and fill in the three fields under the new Auto-populate section, and that's it.

LearnNGrowAtul_2-1702301093247.png

 

 

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc....

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

@Dr Atul G- LNG  if the user have more than one asset in the alm_hardware table , based on your method does it show all of them and it is not for the catalog item but instead for interaction for walkup experience 

 

chercm_0-1702342930163.png

 

Hi @chercm 

 

I never tried but seems answer is NO.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************