Auto populate asset tag

josmad
Tera Contributor

Hi All, 

We have a service request, in that we need to auto populate Asset tag field(Lets say field name is asset_tag and type is single line text) based on selected user(lets say variable name is requested_for).   if user have more than one asset, then asset_tag field should show all the assets assigned to that user. and based on the selected asset os filed should auto populate(lets say os field name is OS).

Kindly help me to achieve this.

Thanks in advance. 

9 REPLIES 9

HarshTimes
Tera Guru

Hi Josmad

You need to write a onchange catalog client script on your catalog item. This script should do the following.

1.Run the script when the requested for changes

2.Query the asset table and fetch the corresponding records

3.Populated the variable on corresponding catalog item with the result.

 

Regards,

Harsh

 

 

 

Thanks for your response,

 

can you please provide us the script.

 

 

Try below script. I have not tested this but it should work after doing some tweak. You will be required to create one client script and one script include as stated below. The client script will make an ajax call to find the asset tags and then it will be set in your variable.


//Client script .Create a onchange client script of requested_for
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('AssetListAjax');
ga.addParam('sysparm_name','GetAssetList');
ga.addParam('sysparm_user_id',g_form.getValue('REQUESTED_FOR')); // use you variable name here
ga.getXML(setAssetTag);

function setAssetTag(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer!=''){
g_form.setValue('ASSET_TAG',answer); // Use your variable name here.
}
}
}


// Script include


Name of the script include - AssetListAjax and check the client callable checkbox

Copy the below function within the script include

GetAssetList: function()
{
var AstList = '';
var user_id = this.getParameter('sysparm_user_id');
var ast = new GlideRecord('alm_asset');
ast.addQuery('assigned_to',user_id);
ast.query();
While(ast.next())
{
AstList += ","+ast.asset_tag ;
}
return AstList ;
},

 

Thanks, will check this code and update you.