Trying to Calculate Days remaining

jasonjones
Giga Contributor

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.

find_real_file.png

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.

find_real_file.png

Any help is appreciated.

Jason

41 REPLIES 41

vinothkumar
Tera Guru

Hi,

I am using below code in our scheduled job to calculate the date difference between opened and current date.

You can try this,it will work

calculateAge();

function calculateAge(){
var gr = new GlideRecord('incident');
gr.addEncodedQuery("active=true^stateNOT IN6,7"); // Active is True and State not in Resolved or Closed
gr.query();
while(gr.next())
{
var start = new GlideDateTime(gr.opened_at);
var end = new GlideDateTime(gs.nowDateTime());
var dur = GlideDateTime.subtract(start, end);
gr.u_incident_age= dur.getRoundedDayPart();
gr.setWorkflow(false); //Do not run business rules
gr.autoSysFields(false); //Do not update system fields
gr.update();
}
}

Sigval Bergesen
Tera Contributor

From Business rule you'll have more options based on available APIs functions to solve this.
Compared to client script where you'll be more limited to the basic java script supported by your end users current preferred browser.

 

From business rule this could be done using this simple one liner: 

var remainingDays = parseInt(Math.floor(gs.dateDiff(new GlideDateTime(current.getValue('warranty_expiration')),new GlideDateTime(),true))/86400);

 

I would suggest making this a business rule triggered on display, and then store that remainingDays variable in the scratchpad which you could then refer to in your client script 🙂 😉 

 

g_scratchpad.remainingDays  = remainingDays; 
Then in client script you may refer to this by simply calling:
g_scratchpad.remainingDays