What's the best way to place a time constraint on a catalog item?

patricklatella
Mega Sage

I need to place a time constraint on a catalog item.   For example, the item is available upon approved request, but only for a 10 day period...what's the best way to apply this constraint?

1 ACCEPTED SOLUTION

You do not need a script include to make sure that the end date is not before the start date, only the lines I added - checked and verified in my instance.


If you have all the script I provided, you should be covered.


Please make sure you added the lines to the correct script.


Also, reload the catalog item page after adding and the lines and saving the script.


If it still does not work, please share your script with the lines included.



harel


View solution in original post

41 REPLIES 41

awesome thank you.   I had used a different dot walking path, so thanks for clarifying.   Regarding the script you provided for the prevention of multiple simultaneous requests of this catalog item:



And finally, check that the user does not have already an open request:


Type: onLoad


Script:




  1. function onLoad() {  
  2.     //Type appropriate comment here, and begin script below  
  3.   var item = g_form.getUniqueValue(); //sys_id of the catalog item you are currently on  
  4.   var user = g_user.userID;// checks who the current user is  
  5.        
  6.   var open = new GlideRecord('sc_req_item'); //checks the requested items table  
  7.   open.addQuery('active', 'true');  
  8.   open.addQuery('cat_item', item);  
  9.   open.addQuery('requested_for', user);  
  10.   open.query();  
  11.   if(open.next()) {  
  12.   alert('You already have an open request. You cannot open a new one: ' + open.number);  
  13.   var url = 'google.com'; //redirect this to somewhere else in your instance  
  14.   OpenWindow = window.open(url, '_self'); //this will redirect the current window to the url you specified above  
  15.   //return false; //instead of redirecting to the URL, you can comment out the two above lines and enable this. The user will stay on the page.  
  16.   }  
  17. }  


What I actually need is for the system to prevent multiple requests for these catalog items in an overlapping time period.   I need it so someone can place multiple requests, but the time windows they want the items cannot overlap.   Does this make sense and do you have advice for this?   Your script does prevent multiple requests, but what is need is not overlapping time periods for the catalog item requests.   Your help is much appreciated!


Patrick,


We have 3 use cases:


1. Second request start date is between first request's start and end date - partial overlap.


2. Second request end date is between first request's start and end date - partial overlap.


3. Second request start date is before first request's start date and second request end date is after first request's end date - full overlap.


I would suggest the following:


Client script:


Name: Check dates overlap


Type: onChange


Variable name: end_date


Script:


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


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


          return;


  }



  //Type appropriate comment here, and begin script below


  var user = g_user.userID;// checks who the current user is


  var start = g_form.getValue('start_date');


  var end = g_form.getValue('end_date');


  var item = g_form.getUniqueValue(); //sys_id of the catalog item



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


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


  ga.addParam("sysparm_start", start); //sends start value to script


  ga.addParam("sysparm_end", end); //sends end value to script


  ga.addParam("sysparm_user", user);//sends the user


  ga.addParam("sysparm_item", item);//sends the catalog item



  ga.getXML(checkIt); //callback function


}


function checkIt(response) {


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



  if (answer == 'no') { //if the dates overlap


  alert("You have overlapping date requests.");


  g_form.setValue('start_date', ''); //remove value from start date field


  g_form.setValue('end_date', ''); //remove value from end date field


  return false;


  }


}



script include:


Name:checkOLDates


Client callable: true


Description: check overlapping dates


Script:


var checkOLDates = Class.create();


checkOLDates.prototype = Object.extendsObject(AbstractAjaxProcessor,{



  checkDate : function() {



  var start = this.getParameter('sysparm_start'); //start Date Field


  var end = this.getParameter('sysparm_end'); //end date


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


  var item = this.getParameter('sysparm_item'); //item



  var open = new GlideRecord('sc_req_item'); //checks the requested items table


  open.addActiveQuery();


  open.addQuery('cat_item', item);


  open.addQuery('requested_for', user);


  open.query();



  if(open.next()) {


  //>= means after   and <= means before


  if((start >= open.variables.start_date && start <= open.variables.end_date) || (end >= open.variables.start_date && end <= open.variables.end_date) || (start <= open.variables.start_date && end >= open.variables.end_date) ) { //between the dates or before AND after the dates


  return 'no';


  }


  return;


  }



  },


  type: 'checkOLDates'


});



I trust you to check it before moving to prod



harel


Thanks so much Harel,


the 3rd use case you list is actually not possible since the the 10 day limit already in place.   However I guess a 3rd use case is that both the start and end dates of the 2nd (and subsequent requests) match exactly the first (or existing) request start and end dates.   Does that change the code?   I am certainly testing everything and again, truly much appreciated.


I know the third case is not currently possible, but if you decide you need it, then you have it


Line 18 says //>= means after   and <= means before, while it should have said: //>= means on or after   and <= means on or before


So no, you don't need to change the code.



harel


fantastic Harel, works perfectly thanks so much!