how to get list of all assets in a catalog variable

niharikayb
Kilo Contributor

Hi ,

I have a catalog item where we have 2 variables

requested for (reference field of user table)

serial number (reference field of asset table)

Based on the selection of 'requested for', i need to get all the assets assigned to him/her in the lookup field of 'serial number'

Please help me out.

Thanks in Advance

1 ACCEPTED SOLUTION

tanumoy
Tera Guru

Script Include:


Name: getAssignedTo


Client Callable: False


Code:



var getAssignedTo = Class.create();


getAssignedTo.prototype = {


  initialize: function() {


  },




  assigned:function() {


  var gp = ' ';


  var a = current.variables.requested_for;



  if(!a)


  return;



  var grp = new GlideRecord('alm_hardware');


  grp.addQuery('assigned_to', a);


  grp.query();


  while(grp.next()) {



  if (gp.length > 0) {


  gp += (',' + grp.sys_id);


  }


  else {


  gp = grp.sys_id;


  }


  }


 


  return 'sys_idIN' + gp;


  },



  }




Advance reference qualifier: javascript:new getAssignedTo().assigned()




Note: Please provide the correct variable names and table names.


View solution in original post

12 REPLIES 12

danielcuadros
Tera Contributor

Hi Niharika,



I think you can try to make a Script Inlcude, that makes a query to the table that you wanted, and return all the records that has the conditions that you want, and call that Script Include from the variable (the second one that need to has the values depending of the first variable). The way to call the Script Include from the variable is in the "Type Specifications" section:


Use reference qualifier -> Advance


Reference qual -> javascript: new nameOfScriptInclude().functionCreatedOnScriptInclude();



I hope this help you.



Regards,


Daniel.


hi daniel



thank you.


Please correct me in the script include



assets: function(){


  var user = new GlideRecord('sys_user');


  user.get(gs.getUserID());


  var answer = [];



  var g = new GlideRecord("alm_hardware");


  g.addQuery('assigned_to',user.sys_id);


  g.query();


  while( g.next()){


  answer.push('g.sys_id');


  return answer;


  }


  },


Hi Niharika,



I think you can make more simple the script:



assets: function(){


  var user = gs.getUserID();


  var answer = "";


  var cont = 0;



  var g = new GlideRecord("alm_hardware");


  g.addQuery('assigned_to',user.sys_id);


  g.query();


  while(g.next()){


      if(cont==0){


          answer = 'sys_id='+g.sys_id;


          cont = 1;


      } else {


          answer = '^ORsys_id='+g.sys_id;


      }


  }


  return answer;


  },



I'm "making" the query that is going to applied to that table, because in the reference qual, it's going to take the "query" to watch the table.



I did the code but didn't try it, I think it works but don't know 100% for sure.



Try it and let me know if it works.



Thank you.





Regards,


Daniel.



Hi Daniel ,



sorry.It is not working