How to pull asset warranty expiration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2019 05:49 AM
The script below should be pulling in the warranty expiration and comparing it to the current date but I continue to get undefined for the warranty expiration date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var serial = newValue;
var warranty = warrantyDate(serial);
var today = new Date();
var manufacturer = g_form.getDisplayValue('model_manufacturer');
var contact = g_form.getDisplayValue('company_phone');
var company = g_form.getDisplayValue('company');
alert(warranty);
if(today < warranty) {
alert("This item may be under warranty. Please contact " + company + " at " + contact+".");
g_form.clearValue('serial_no');
}
function warrantyDate(serial_no) {
var warranty;
var gr = new GlideRecord("alm_asset");
gr.addQuery("serial_number", serial_no);
gr.query();
while (gr.next()) {
warranty = gr.getDisplayValue('warranty_expiration');
}
return warranty;
}
}
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2019 07:05 AM
I agree with Sai. This is where I depend on a GlideAjax call to compare two date and return a negative, zero, or positive value to tell me if date1 is less than, equal, or greater than date2. I can get more control over what's happening with GlideDateTime().
Check out episodes 5, 6, and 33 of TechNow for more information on GlideAjax.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2019 07:55 AM
Thanks guys here is what I've gotten so far, please let me know if i am on the right track:
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var warranty;
var gr = new GlideRecord("alm_hardware");
gr.addQuery("serial_number", newValue);
gr.query();
while (gr.next()) {
warranty = gr.getDisplayValue('warranty_expiration');
}
var cdt = warranty; //Warranty Date
var dttype = 'day';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
var contact = g_form.getDisplayValue('company_phone');
var company = g_form.getDisplayValue('company');
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//verify if is less than 2 days and is not saturday or sunday
if ((answer < 1)){
alert("This item may be under warranty. Please contact " + company + " at " + contact+".");
//alert(answer);
}else if (answer < 0){
g_form.clearValue('serial_no');
}
}
}
Script include:
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2020 07:50 AM
Where do you enter this script?