Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

List of all assets matching the current logged in users location

pennieturner
Mega Guru

I want to create a link from left hand pane called My Assets which will show All Assets with the same location as the current logged in user.


I know from Dublin you can download and install something called Simple Separation but we are on Calgary so this won't work for us.

I think I need a business rule like the one for getMyApprovals, but, i'm struggling with the code as I only know a little Javascript

Has anyone been able to write anything like that or can help?

Thank you in advance

1 ACCEPTED SOLUTION

pennieturner
Mega Guru

I managed to get this working by using the following script include:



function getlocationList() {


  var user = new GlideRecord('sys_user');


  user.get(gs.getUserID());


  var answer = new Array();


  var i = 0;


  answer[i++] = new String(user);



  var g = new GlideRecord("cmn_location");


  g.addQuery("country", user.location.country);


  g.query();


  while( g.next())


  answer[i++] = new String(g.sys_id);


  return answer;


}


View solution in original post

19 REPLIES 19

I also put another info msg in to find out what gr.sys_id was and it was a sys ID relating to a random address


function getlocationList(){  


  var user = new GlideRecord('sys_user');  


  user.get(gs.getUserID());  


 


  var ret = 'sys_idIN';  


 


  var gr = new GlideRecord( 'cmn_location' );  


  gr.addQuery( 'country', user.location.country );  


  gr.query();  


  while( gr.next() ){  


  ret += gr.sys_id+',';  


  }  


 


  return ret;  


}






Some correction.


make sure the location have a valid country name.


It still brings back a random address.



Could it be because the country field is a free text field?




  var user = new GlideRecord('sys_user');    


  user.get(gs.getUserID());      


  gs.log(user.sys_id.toString());  


  var ret = 'sys_idIN';      


     


  var gr = new GlideRecord( 'cmn_location' );      


  gr.addQuery( 'country', user.location.country );      


  gr.query();      


gs.log('Count:'+gr.getRowCount());


  while( gr.next() ){      


  ret += gr.sys_id.toString()+',';      


  }      


      gs.log('return:'+ret);





Run this in background script and send me the result.


to use background script, you should be security admin(Elevated role)


This brought back a Sys_id for Me Correct


count 430 (this is correct there are 430 locations with the country as united Kingdom (my country location)



then listed the sys_ids relating to the country I can gather



I clicked the link, but, nothing appears in the list and the query string shows location as blank



Getting there though:)