- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 02:11 AM
Hi All,
I have a requirement to display a field in the catalog form, only when the difference between two Date type fields are calculated and the condition appears as if like below:
The 1st field (Launch Date) Date is less than 4 days away from the ticket creation date/time, ie., Created (date/time) field.
Here is my onchange client script:
*******************************************
function onChange(control, oldValue, newValue)
{
alert('you changed Launch Date from ' + oldValue + ' to ' + newValue);
if(newValue != '')
{
var start = g_form.getValue('request_datetime').split(' ')[0];
var end = g_form.getValue('launch_date');
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_start',start);
ajax.addParam('sysparm_end',end);
ajax.getXML();
var answer = ajax.getAnswer();
alert(ajax.getAnswer());
if(ajax.getAnswer()<4)
{
alert("if");
g_form.setDisplay('warning', true);
}
else
{
alert("else");
g_form.setDisplay('warning', false);
}
}
}
My script include is as below:
*******************************************
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
//return gs.dateDiff(this.getParameter('sysparm_start').getDisplayValue(), this.getParameter('sysparm_end').getDisplayValue(), false);
var sd = new GlideDate(this.getParameter('sysparm_start'));
var ed = new GlideDate(this.getParameter('sysparm_end'));
var diffSeconds = gs.subtract(sd,ed);
var converttodays = diffSeconds/(60*60*24);
return converttodays;
}
});
This script is not working properly. Null value is resulted for "ajax.getAnswer()"
Could any1 pls help?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 03:44 AM
Its straightforward. Date variables internally have milliseconds itself. You can subtract them and convert into days. I put additional alert statements. Remove them, once you get the correct difference.
var one_day=1000*60*60*24;
var start = g_form.getValue('request_datetime').split(' ')[0];
var end = g_form.getValue('launch_date');
var date1_ms = new Date(start);
var date2_ms = new Date(end);
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
alert("Difference in days");
alert(Math.round(difference_ms/one_day));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 03:26 AM
Hi Ram,
There is no particlar reason to make a server call rather than that I believed this would be more efficient and easy. This script is also not returning any value.
If possibe, please suggest this issue to be solved using the client scipt itself. Anything would helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 03:44 AM
Its straightforward. Date variables internally have milliseconds itself. You can subtract them and convert into days. I put additional alert statements. Remove them, once you get the correct difference.
var one_day=1000*60*60*24;
var start = g_form.getValue('request_datetime').split(' ')[0];
var end = g_form.getValue('launch_date');
var date1_ms = new Date(start);
var date2_ms = new Date(end);
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
alert("Difference in days");
alert(Math.round(difference_ms/one_day));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 04:06 AM
Hi Ram,
Thanks for the script. I was struggling with this since long.
It worked perfectly.
Thanks,
Rathika.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 04:40 AM
Hi Ram,
One additional requirement is to add a condition in the script, such that the user should not be allowed to enter any date in launch_date field prior to the Created date.
This is required as the prior date to creation is populating negative value for difference_ms, which is not working properly in if else condition.
Thanks,
Rathika
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 05:15 AM
Hi,
var difference_ms = date2_ms - date1_ms;
Yes, you can add the following condition before the above line.
if(start > end)
{
alert("End date can't before be start date");
return false;
}