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 10:02 AM
warranty_expiration needs to be in quotes like "warranty_expiration"
var strt = g_form.getValue("warranty_expiration"); //set this as current warranty date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 10:14 AM
The error is as Mike mentioned: getValue() requires the field name to be specified as a string.
However, I would caution against trying to manipulate dates client-side, given that the display format is under user control so making assumptions the figures appear in specific locations is a flawed one.
Definitely BR is the way I would go - possibly triggered on a afterQuery so that the value can be calculated then displayed to the user when the form loads.
You may also want to look at a calculated field (or function field) to achieve the same outcome.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 10:34 AM
All,
I am still getting an error. I also tried Duration and the following attribute string.
u_days_of_remaining_warranty = GlideDateTime.subtract(warranty_expiration.getDisplayValue(), gs.nowDate())
Which shows.
I also tried Calculated Value, but not having luck there either. I'm sure it is syntax again.
Jason
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 10:45 AM