- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 08:21 AM
We have a request to configure date fields on a catalog item where users cannot enter an End Date that seven days greater than the Start Date.
We had to configure a date validation client script recently, so we've used that as a base but are having issues setting the Start Date and End Date within the script include.
Script include:
var CheckValidToOSS = Class.create();
CheckValidToOSS.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate: function(){
var gdt = new GlideDateTime();
gdt2.setDisplayValue(this.getParameter('start_date'));
gdt.addDaysLocalTime(7);
var gdt2 = new GlideDateTime();
gdt2.setDisplayValue(this.getParameter('end_date'));
if(gdt2.getDate()>=gdt.getDate()){
return true;
}
else{
return false;
}
},
type: 'CheckValidToOSS'
});
Can we set the parameters to catalog item variables this way, or is there another way to do it?
Catalog client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('CheckValidToOSS');
ga.addParam('start_date',g_form.getValue('start_date'));
ga.addParam('end_date',g_form.getValue('end_date'));
ga.getXML(callBack);
function callBack(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer=="false"){
alert("End Date cannot be greater than 7 days from the Start Date");
g_form.setValue('end_date','');
}
}
}
Thanks in advance
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 02:38 PM
Hi Alexander,
try this as well, this code will work irrespective of the date format
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 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_end", newValue);
ga.getXML(checkDate); //callback function
}
function checkDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); //the response from the script
if (answer > 7) { //if the date received is more than 7 days from the start date
alert("End date cannot be later than 7 days after start date.");
g_form.setValue('end_date', ''); //remove value from end date field
return false;
}
}
script include code-
var calcDate = Class.create();
calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor,{
getDate : function() {
var startDT = new GlideDate();
startDT.setDisplayValue(this.getParameter('sysparm_start'));
var endDT = new GlideDate();
endDT.setDisplayValue(this.getParameter('sysparm_end'));
var duration = new GlideDuration();
duration= GlideDate.subtract(startDT, endDT);
return duration.getDayPart();
},
type: 'calcDate'
});
Regards,
Lavanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 10:42 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 10:54 AM
If this is an onChange catalog client script for the variable end_date, then let's add some log statements to the script include and see what (if any) data it is getting:
In the line above if(addTYPE == 'day') { , add
gs.log('Alex, start date is ' + firstDT + ' and days allowed is ' + addTIME);
and save. Then look for log entries starting with "Alex"
Once that's in, change the end_date in the item and check the logs.
If you are not getting anything, it means that nothing is being sent, or something is sent, but the script include cannot see what it is.
Let me know how it goes,
harel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 11:00 AM
Thanks Harel,
It looks like we're getting the values, here's the log entry: "Alex, start date is 04/13/2017 and days allowed is 7"
I also noticed this log error occur:
Unparseable date: "04/13/2017": java.text.ParseException: Unparseable date: "04/13/2017": java.text.DateFormat.parse(DateFormat.java:366)
com.glide.util.SimpleDateFormatEx.parse(SimpleDateFormatEx.java:96)
com.glide.glideobject.GlideDate.setValue(GlideDate.java:106)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138)
org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:292)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2577)
org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
org.mozilla.javascript.gen.sys_script_include_765b6eb50f4e36805b3dabf8b1050edd_script_5404._c_anonymous_1(sys_script_include.765b6eb50f4e36805b3dabf8b1050edd.script:10)
org.mozilla.javascript.gen.sys_script_include_765b6eb50f4e36805b3dabf8b1050edd_script_5404.call(sys_script_include.765b6eb50f4e36805b3dabf8b1050edd.script)
org.mozilla.javascript.ScriptRuntime.doCall2(ScriptRuntime.java:2645)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2582)
org.mozilla.javascript.ScriptRuntime.applyOrCall(ScriptRuntime.java:2517)
org.mozilla.javascript.BaseFunction.execIdCall(BaseFunction.java:296)
org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:102)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2580)
org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
org.mozilla.javascript.gen.sys_script_include_d65f78c40a0a0b6900196656f35913d3_script_285._c_anonymous_2(sys_script_include.d65f78c40a0a0b6900196656f35913d3.script:34)
org.mozilla.javascript.gen.sys_script_include_d65f78c40a0a0b6900196656f35913d3_script_285.call(sys_script_include.d65f78c40a0a0b6900196656f35913d3.script)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:563)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3439)
org.mozilla.javascript.gen.sys_script_include_d65f78c40a0a0b6900196656f35913d3_script_285.call(sys_script_include.d65f78c40a0a0b6900196656f35913d3.script)
org.mozilla.javascript.ScriptRuntime.doCall2(ScriptRuntime.java:2645)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2582)
org.mozilla.javascript.ScriptableObject.callMethod(ScriptableObject.java:2662)
org.mozilla.javascript.ScriptableObject.callMethod(ScriptableObject.java:2634)
com.glide.script.RhinoObject.callFunction(RhinoObject.java:167)
com.glide.script.RhinoObject.callFunction(RhinoObject.java:163)
com.glide.processors.xmlhttp.AJAXEvaluator.evalScriptInclude(AJAXEvaluator.java:167)
com.glide.processors.xmlhttp.AJAXEvaluator.evaluateAjax(AJAXEvaluator.java:84)
com.glide.processors.xmlhttp.AJAXEvaluator.process(AJAXEvaluator.java:62)
com.glide.processors.XMLHttpProcessor.processJavaAJAX(XMLHttpProcessor.java:143)
com.glide.processors.XMLHttpProcessor.process(XMLHttpProcessor.java:100)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:424)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:195)
com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:178)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:167)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:49)
com.glide.sys.Transaction.run(Transaction.java:1976)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 11:30 AM
Try adding in your script include above day.setValue(firstDT);
var firstDate = firstDT.toString();
and then change day.setValue(firstDT); to
day.setValue(firstDate);
Hopefully, this will convert the date to a string that the script include can parse.
Let me know what you see.
Harel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 11:37 AM
Good idea, still getting the same parsing error though: Unparseable date: "04/30/2017": java.text.ParseException: Unparseable date: "04/30/2017":
And it's still pulling the data ok: Alex, start date is 04/30/2017 and days allowed is 7