Trying to Calculate Days remaining
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 09:47 AM
All,
I'm trying to create a client script or business rule that calculates the difference of a Warranty Expiration Date and the current date to determine a "Number of Days remaining" on the warranty.
I found this script in community to modify, but I'm still missing something.
function onChange(control, oldValue, newValue, isLoading) {
var strt = g_form.getValue(warranty_expiration); //set this as current warranty date
var end = new Date(now.getFullYear(), now.getMonth(), no.getDate()); //This retrieves the current date
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_days_of_remaining_warranty', answer);
}
Below is an error that is received.
Any help is appreciated.
Jason
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 01:28 PM
Nope. Do you want to send me a screenshot of what your seeing as a comparison? I unselected the checkbox for query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 01:42 PM
Can you please send me the screenshots of all the sections of your BR. But i tried the exact same thing and works in my instance. One more question, the string field with name "u_days_of_remaining_warranty" is active correct??
Also confirm your BR runs on alm_hardware table
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 02:22 PM
Any luck??
Please mark my response as correct and helpful if it helped solved your question.
-Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 02:44 PM
Try this
(function executeRule(current, previous /*null when async*/) {
var start = new GlideDateTime();
var end = new GlideDateTime();
end.setValue(current.warranty_expiration);
// Duration in days
var dur = gs.dateDiff(start, end,false);
gs.addErrorMessage('This is the Duration:'+dur.split(' ')[0]);
current.u_days_of_remaining_warranty =dur.split(' ')[0];
})(current, previous);
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 10:13 PM
Hi,
you can try below logic:
Create "After Update" business rule.
When to run : 1) Warranty Expiration is Not Empty
2)Warranty expiration Changes
Script:
var warranty_expiration = current.warranty_expiration;
var today = new GlideDateTime();
today.getDate();
var diff = gs.dateDiff(warranty_expiration,today,false);
var convert =diff.split(' ');
var days = convert[0];
current.u_days_of_remaining_warranty = days;
current.update();
Thanks,
Ravi