How to limit the number of requests (not more than 2) for a particular catalog item based on dropdown variable select

robinpaul
Kilo Contributor

How to limit the number of requests (not more than 2) for a particular catalog item based on dropdown variable select

currently , there is a catalog item "Reserving laptops/Desktops" . There is a variable(select box) called "location" with the choices a) EMEA b) Apac c)Others

My Requirement is like this :

If the User selects the location as EMEA OR Apac ,then the particular user will be able to submit the 2 requests only for this catalog item.(user need not allow to submit the 3rd request for this item)

User will be   able only to submit the 3rd request,when the state of any one of the 2 requests(which are already submitted by user) is closed/cancelled.

If the User selects the location as "Other" ,then there is no restrictions to submit the requests for this catalog item

How can i acheive this one.

Any help would be appreciated.Thanks in Advance.

1 ACCEPTED SOLUTION

Hi Robin,


I think we have it.


Here is the script include:


var checkCount = Class.create();


checkCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  theCount : function() {



  var user = this.getParameter('sysparm_user');


  var catItem = this.getParameter('sysparm_item');


  var location = this.getParameter('sysparm_loc'); //this is your location field value received from the requested item


  var encQuery = 'sc_item_option.item_option_new.name=location^sc_item_option.value='+location; //where location is the name of your choice list



  var ri = new GlideRecord('sc_item_option_mtom'); //checking the variable table


  ri.addEncodedQuery(encQuery);


  ri.addQuery('request_item.request.requested_for', user); //checking for the user


  ri.addQuery('request_item.active', 'true'); //checking that the requested item is active


  ri.addQuery('request_item.cat_item', catItem); //checking the catalog item


  ri.query();



  var count = ri.getRowCount();


  return count;


},


  type: 'checkCount'


});


And in your catalog client script, add the following lines:


ga.addParam("sysparm_loc", v_location); //send value of location to the script include


change       if (answer > 2) {


to       if (answer > 3) {   or to whatever other number that suits your need.



Leave your conditions in the catalog client script as they are so that the script include checks against that specific choice.


harel


please mark as correct or helpful based on needs


View solution in original post

10 REPLIES 10

robinpaul
Kilo Contributor

Hi harel,
Thanks for giving quick reply.
location is the name of the variable on a catalog item.
location (select box variable)
EMEA and Apac are the choices of that variable.


Hi Robin,


I am a bit stuck here, because I am not sure on how to query a variable in a requested item from a script include.


Adding ctomasi, pradeepksharma and rfedoruk and actually anyone else who has an idea:


We need to check whether a certain set of open requested items (filtered by user and catalog item) have a specific value in one of the variables (location).


If the value exists more than 3 times, return the count etc.


We can send the value from the catalog item to the script include, but how do we use it to filter the answer in the script include?



harel


Thanks for the ping oharel. @Robin, please find the modified script as per your req. Please update the name of the location variables in the below script and try once. I've also modified the answer variable to return true or false. Hence you will have to modify the client script accordingly



Code :



var checkCount = Class.create();


checkCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  theCount : function() {


  var Loccount = 0;


  var answer = 'false';


  var user = this.getParameter('sysparm_userId');


  var catItem = this.getParameter('sysparm_item');


  var ri = new GlideRecord('sc_req_item');


  ri.addActiveQuery();


  ri.addQuery('request.requested_for', user);


  ri.addQuery('cat_item', 'f8cc750245f02340005bd2b6719220c997');



  ri.query();



  var Rowcount = ri.getRowCount();


  while(ri.next())


  {


  var loc = ri.variables.PASS LOCATION VARIABLE NAME HERE; //Update the name of the location variable


  if(loc == EMEA || loc == Apac) //Update the choice values accordingly


  {


  Loccount++;


  }


  }


  if(Loccount == 3 && Rowcount == 3)


  {


  answer = 'true';


  }



  return answer;




  },




  type: 'checkCount'


});



P.S: For some reason, I'm not able to paste the code in javascript editor


As long as you have the request item as an object you can dot walk through the variables property.



requested_item.variables.variable_name


Hi Robin,


I think we have it.


Here is the script include:


var checkCount = Class.create();


checkCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  theCount : function() {



  var user = this.getParameter('sysparm_user');


  var catItem = this.getParameter('sysparm_item');


  var location = this.getParameter('sysparm_loc'); //this is your location field value received from the requested item


  var encQuery = 'sc_item_option.item_option_new.name=location^sc_item_option.value='+location; //where location is the name of your choice list



  var ri = new GlideRecord('sc_item_option_mtom'); //checking the variable table


  ri.addEncodedQuery(encQuery);


  ri.addQuery('request_item.request.requested_for', user); //checking for the user


  ri.addQuery('request_item.active', 'true'); //checking that the requested item is active


  ri.addQuery('request_item.cat_item', catItem); //checking the catalog item


  ri.query();



  var count = ri.getRowCount();


  return count;


},


  type: 'checkCount'


});


And in your catalog client script, add the following lines:


ga.addParam("sysparm_loc", v_location); //send value of location to the script include


change       if (answer > 2) {


to       if (answer > 3) {   or to whatever other number that suits your need.



Leave your conditions in the catalog client script as they are so that the script include checks against that specific choice.


harel


please mark as correct or helpful based on needs