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

For this Requirement , I tried the below scripts but no use.



On Change Catalog client script:



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }




    //Type appropriate comment here, and begin script below


  var v_location = g_form.getValue('location');


    if(v_location == "EMEA" || v_location == "Apac" ) {    


  var user = g_user.userID;  


  var ga = new GlideAjax("checkCount"); //name of script include  


  ga.addParam("sysparm_name", "theCount"); //name of function in script include  


  ga.addParam("sysparm_user", user); //send user value to script  


  ga.getXML(myCount); //callback function  


  }  


    function myCount(response) {  


  var answer = response.responseXML.documentElement.getAttribute("answer"); //the response from the script  


  if (answer > 2) {  


      alert('You already have '+ answer + ' requests opened.');


    document.getElementById("oi_order_now_button").style.display='none';


  return false;  


  }  


}  


}





script include:



var checkCount = Class.create();  


checkCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


 


  theCount : function() {  


 


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


  var ri = new GlideRecord('sc_req_item');  


  ri.addActiveQuery();  


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


  ri.query();  


 


  var count = ri.getRowCount();  


  return count;  


  },  


 


 


      type: 'checkCount'  


});  




Any help would be appreciated.Thanks in advance.


Hi Robin,



I checked it on my instance, and it seems to be working. It is counting ALL requested items, not just for a specific catalog item. If you need it to check for a specific item, you can add in the catalog client script:


var catItem = g_form.getUniqueValue(); //as a variable that will get the catalog item


and in the ajax-part:


ga.addParam("sysparm_item", catItem); //send value of catalog item



In your script include, add:


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


and in the query-part:


ri.addQuery('cat_item', catItem);



If it is still not working, please explain what is not working - are you not getting the alert? or the number is incorrect?


Make sure that you are giving the right values from the location field. To check, add under if(v_location == "EMEA" || v_location == "Apac" ) {  


alert('locaiton is ' + v_location) and see the value that you're getting.



harel


Please mark as helpful or correct based on impact.



Edit:


In my script (on Helsinki), I also changed


document.getElementById("oi_order_now_button").style.display='none';


to


document.getElementById("order_now").style.display='none';


robinpaul
Kilo Contributor

Hi harel,



Sorry for the delay and Thanks for the reply.


I just modified the scripts as per you suggested and below are the scripts


SCRIPT INCLUDE:


var checkCount = Class.create();


checkCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {



theCount : function() {


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 count = ri.getRowCount();


return count;




},




type: 'checkCount'


});




Instead of Disabling the OrderNow button in the On change script,I just preferred the Onsubmit catalog client script



OnSubmit catalog client script:



function onSubmit() {


var catItem = g_form.getUniqueValue();


var v_location = g_form.getValue('location');



if(v_location == "EMEA" || v_location == "Apac" ) {



var user = g_user.userID;



var ga = new GlideAjax('checkCount');


ga.addParam("sysparm_name", "theCount"); //name of function in script include


// ga.addParam("sysparm_userId", user); //send user value to script


ga.addParam("sysparm_item", catItem); //send value of catalog item


ga.addParam('sysparm_userId',g_user.userID);



ga.getXMLWait();


if (ga.getAnswer() > 2){


alert('This contains already 3 Active RITMs');


return false;


}


}


}



Both the scripts work very well.Now i have little bit change in my Requirement


when the user selects the location as "EMEA" while submitting the request, user will be able to submit the 3 requests for the location "EMEA'


and if the user selects the location as "Apac" while submitting the request, user will be able to submit the 3 more requests for the location "Apac".



so totally 6 requests will be allow to submit for this catalog item.


User will not be allow to submit the requests At the time of 7th ,8th requests(4th request for "EMEA" and 4th request of "Apac") till the any one of the previous requests was closed complete.



How can i acheive this one.Any help would be appreciated.Thanks in Advance.


Hi Robin,



I am glad it is working. As for your other requirements:


So the user can submit a maximum of 3 requests for EMEA and 3 for APAC; this means that we need to check the location as well, not just the item and the user. Cool.


A question: is location a variable in your request or is it   a location field on the request?