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

Here we go:


start_date - the date the employee needs the car.


end_date - the date the employee returns the car, not more than start date +10


change the name of the fields to be consistent with what you need.


I tried being as explicit as possible with the explanation of what every line does, but feel free to wonder aloud



To control the end_date field, so that users do not populate it before the start date, create an on change script. The script will also check that the start date is not earlier than today.


field: start_date


script:


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


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


          return;


    }



    //Type appropriate comment here, and begin script below


  //empty the end_date upon changing start date


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


  if(newValue) {


  g_form.setValue('end_date', '');


  }



  //make sure they are not asking for a date in the past (before now)


  var ga = new GlideAjax("NowDateCalc"); //name of the called script include


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


  ga.getXML(getDate); //callback function


}



function getDate(response) {


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


  var rightNow = response.responseXML.documentElement.getAttribute("answer");



  if (start_date < rightNow) {


  alert("Start date cannot be earlier than today.");


  g_form.setValue('start_date', '');


  }


}



Script include to check NowDateCalc date:


Name: NowDateCalc


client callable: true


script:


var NowDateCalc = Class.create();


NowDateCalc.prototype = Object.extendsObject(AbstractAjaxProcessor,{



  getNow : function() {



  return gs.now();


  },


      type: 'NowDateCalc'


});



Now to check stuff in the end_date field:


Create an onChange catalog client script.


Variable name: end_date



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


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


          return;


    }



    //Type appropriate comment here, and begin script below


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


  var allowed_dayes = 10;//maximum days to add


  var addtype = 'day'; //The time type.



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


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


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


  ga.addParam('sysparm_addtime', allowed_dayes);   //send days to add value to script


  ga.addParam('sysparm_addtype', addtype);   //send the typf of time to the script, in this case - days


  ga.getXML(checkDate); //callback function


}



function checkDate(response) {


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


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



  if (answer < end_date) { //if the date received is more than 10 days from the start date


  alert("End date cannot be later than 10 days after start date.");


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


  return false;


  }


}



Script include:


Name: calcDate


Client callable: yes


Script:


var calcDate = Class.create();


calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor,{



  getDate : function() {



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


  var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day


  var addTIME = this.getParameter('sysparm_addtime'); //How much time to add


  var day = GlideDate();


  day.setValue(firstDT);



  if(addTYPE == 'day') {


  day.addDays(addTIME);


  }


  return day;


  },


  type: 'calcDate'


});


*based off of MB article Client Script Date/Time Functions



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


Type: onLoad


Script:


function onLoad() {


    //Type appropriate comment here, and begin script below


  var item = g_form.getUniqueValue(); //sys_id of the catalog item you are currently on


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


   


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


  open.addQuery('active', 'true');


  open.addQuery('cat_item', item);


  open.addQuery('requested_for', user);


  open.query();


  if(open.next()) {


  alert('You already have an open request. You cannot open a new one: ' + open.number);


  var url = 'google.com'; //redirect this to somewhere else in your instance


  OpenWindow = window.open(url, '_self'); //this will redirect the current window to the url you specified above


  //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.


  }


}




Hope this helps,


harel


Please mark as correct or helpful based on impact


fantastic!   However after your instructions I am able to enter an "End Date" that is prior to the "Start Date" chosen.   It does prevent me from entering an "End Date" more than 10 days after the "Start Date", but it does not prevent an "End Date" prior to the chosen "Start Date".   Does the first set of code you provided need a version of this bit of code below, but for the 'end_date'?



  1. //make sure they are not asking for a date in the past (before now)  
  2.   var ga = new GlideAjax("NowDateCalc"); //name of the called script include  
  3.   ga.addParam("sysparm_name", "getNow"); //name of the function in the script include  
  4.   ga.getXML(getDate); //callback function  
  5. }  
  6.  
  7. function getDate(response) {  
  8.   var start_date = g_form.getValue('start_date');  
  9.   var rightNow = response.responseXML.documentElement.getAttribute("answer");  
  10.  
  11.   if (start_date < rightNow) {  
  12.   alert("Start date cannot be earlier than today.");  
  13.   g_form.setValue('start_date', '');  

Hi Patrick,


NowDateCalc is the name of the script that checks that the start_date is not before today.


And yes, I missed a couple of lines in one of the scripts (copied it to the answer too early).


So here goes - see in bold the lines you should add to the onChange catalog client script for the end_date


(Also, I see I had a spelling mistake: allowed_dayes should be allowed_days)



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


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


          return;


  }



  //Type appropriate comment here, and begin script below


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


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


  var allowed_dayes = 10;//maximum days to add


  var addtype = 'day'; //The time type.



//check if end_date is before start_date


  if(start_date > end_date) {


  alert('You cannot return a car before taking it. Please change either the start or end date');


g_form.setValue('end_date', '');


  }



  //check maximum 10 days


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


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


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


  ga.addParam('sysparm_addtime', allowed_dayes);   //send days to add value to script


  ga.addParam('sysparm_addtype', addtype);   //send the typf of time to the script, in this case - days


  ga.getXML(checkDate); //callback function


}




function checkDate(response) {


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


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



  if (answer < end_date) { //if the date received is more than 10 days from the start date


  alert("End date cannot be later than 10 days after start date.");


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


  return false;


  }


}



harel


Please mark my answer as correct or helpful based on impact


Hi Harel,


that didn't work...do I also need to maybe a new script include for the "end_date"?   You write above,


NowDateCalc is the name of the script that checks that the start_date is not before today.



do I also therefore need a separate script that checks the the end_date is not before the "start_date", or should your additional code do that all on its own?


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