How to add an if/else in Script Include to filter out asset based on location?

Dan Loza
Tera Contributor

Hello, I've created this Script include to be used as an asset filter for reference qualifiers. So, for example, when I use this script include in a reference qualifier like: javascript:new AssetUtil().getAssetFilter(current.request_item.cat_item.model,current.variables.requested_for.location), it filters the asset based on model and the requester's location.

 

Now, I need to modify this such that if the location STARTSWITH US, then it goes to one USxx location. For all other users outside US, it still gets the requester's location. I can't seem to make the IF statement work.

 

var AssetUtil = Class.create();
AssetUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserdetails: function(){
var phone='';
var num = 0;

var user = this.getParameter('sysparm_user');
var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id',user);
gr.query();
if(gr.next())
{
phone = gr.phone;
}
var ast = new GlideAggregate('alm_hardware');
ast.addAggregate('COUNT');
ast.addQuery('install_status','1');
ast.addQuery('model_category.display_value','Computer');
ast.addQuery('assigned_to',user);
ast.query();
if(ast.next()){
num = ast.getAggregate('COUNT');
}
return phone+','+num;
},

getAssetFilter: function(modl,usrloc){
var asstsysid = "";
var locast = new GlideRecord('u_m2m_locations_stockrooms');
locast.addQuery('u_location',usrloc);
locast.query();
while(locast.next()){
var astloc = new GlideRecord('alm_asset');
astloc.addQuery('stockroom',locast.u_stockroom); 
ast.addQuery('location',usrloc);
ast.addQuery('location.active',true);
astloc.addQuery('install_status','6');
astloc.addQuery('substatus','available');
astloc.addQuery('model',modl);
astloc.query();
while (astloc.next()){
if(asstsysid == "")
asstsysid = astloc.sys_id;
asstsysid = asstsysid + "," + astloc.sys_id;
gs.info('Asset name:'+ asstsysid);
}
}

var ast = new GlideRecord('alm_asset');
ast.addQuery('stockroom.location',usrloc);
ast.addQuery('location.active',true);
ast.addQuery('install_status','6');
ast.addQuery('substatus','available');
ast.addQuery('model',modl);
ast.query();
while (ast.next()){
if(asstsysid == "")
asstsysid = ast.sys_id;
asstsysid = asstsysid + "," + ast.sys_id;
gs.info('Asset name:'+ asstsysid);
}

return 'sys_idIN' + asstsysid;
},

type: 'AssetUtil'
});

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Hi Dan,

Is the u_location field on your custom stockroom table a reference to the location table, so usrloc is a sys_id?  The ast GlideRecord seems redundant - did you add this for the latest requirement?  If usrloc is a sysid you would have to query the location table to retrieve that record to be able to direct or filter the stockroom or asset GR based on the Name field of the retrieved record. 

Hi, Brad. Yes, the u_location field is a reference to the location so it's a sys_id.