- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2017 11:19 AM
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?
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2017 10:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2017 01:02 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2017 09:26 AM
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'?
- //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', '');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2017 09:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2017 10:16 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2017 10:26 AM
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