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

@Dr Atul G- LNG  i tried to use this but it is not populating 

 

// Client Script on the incident table
function onLoad() {
   // Check if the 'opened_by' field has a value
   var openedBy = g_form.getValue('opened_by');

   // If 'opened_by' has a value, perform further actions
   if (openedBy) {
       // Query the alm_hardware table to find the user asset (replace 'alm_hardware' with your actual table name)
       var hardware = new GlideRecord('alm_hardware');
       hardware.addQuery('user', openedBy);
       hardware.query();

       // If a user asset is found, set the value of the 'userasset' field on the form
       if (hardware.next()) {
           g_form.setValue('userasset', hardware.userasset);
       }
   }

@chercm 

 

Kindly check this link i have similar requirement and i achieved it with the help of scripting. If user has more than 2 asset then i have populate the serial number with comma , seperated.

 

https://www.servicenow.com/community/developer-forum/populate-the-multiple-serial-number-in-a-single...

 

regards 

shubham

@shubhamdubey thanks for the solution, i tried to convert it to onload for the walkup experience 

 

chercm_0-1702595698297.png

 

function onLoad() {
g_form.addLoadCompleteCallback(function () {
var assetField = g_form.getControl('your_asset_field'); // Replace 'your_asset_field' with the actual name of your asset field
var serialNumberField = g_form.getControl('serial_number'); // Replace 'serial_number' with the actual name of your serial number field

if (assetField && serialNumberField) {
var assetValue = assetField.value;
if (assetValue) {
g_form.clearValue('serial_number'); // Clear the serial number field initially
var assetList = assetValue.split(',');
var temp = '';

function processAsset(index) {
if (index < assetList.length) {
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'getSerialNumber');
ga.addParam('sysparm_ast', assetList[index]);

ga.getXMLAnswer(function (answer) {
if (answer !== ' ') {
temp += answer + ',';
serialNumberField.value = temp;
}

// Process the next asset
processAsset(index + 1);
});
}
}

// Start processing assets from the first index
processAsset(0);
}
}
});
}

 

Hi Ankur,

 

If i want to fetch the user name in other custom field "user_to_be_released", then how i will fetch the value of the field using client script. Can you please help me since i am new learner of scripting.

 

thanks,

Aruna

@Aruna13 

Can you post a new question and tag me there as this is an old thread?

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