Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How do I set a field to the difference between two dates?

laverne
Kilo Explorer

I am new to developing in ServiceNow and am struggling to set a variable 'Days To Event' to the number of days between today and the event 'start_date'.   This value needs to update every time the form is displayed or the record is queried.   The table name is 'Marketing Events'.   The date field names are 'current_date' and 'start_date'.   The variable that needs to be set is 'days_to_event'.

I have tried creating a business rule and while the syntax is correct, I get no value to display on the form.  

Any guidance you can provide as to how and where I set values to get this functionality to work would be GREATLY appreciated.   I am developing in a scoped application.

Thank You,

LaVerne Carrick

4 REPLIES 4

tony_barratt
ServiceNow Employee

Gurpreet07
Mega Sage

Code on following thread should do the job for you.


Re: How to get difference between two date time fields into time.


NOTE : Use onLoad client script instead of onChange.


Hi Gupreet;



I created the client script and the script include for AjaxDurCalc. However, when I display a record where the start_date and current_date are set, the Days 2 Event field is all zeros.



Can you please help me understand what I am doing wrong?



I appreciate your help.



Thank you…LaVerne


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



The client script is as follows:



function onLoad() {



//Type appropriate comment here, and begin script below


var strt = g_form.getValue('current_date'); //<-- This field is 'calculated' using this calculation --> GlideDateTime().getDisplayValue(); ?? is this causing a problem because it is also set on load? Should I be using strt = new GideDateTime() ???


var end = g_form.getValue('start_date');


var ajax = new GlideAjax('AjaxDurCalc');


ajax.addParam('sysparm_name','durCalc');


ajax.addParam('sysparm_strt',strt);


ajax.addParam('sysparm_end',end);


//ajax.getXMLWait(); //<-- NOTE: I am in a scoped application and this function is not allowed. ?? Not sure if this is part of the problem Is there a similar call I can use in a scoped application?


var answer = ajax.getAnswer();


g_form.setValue('days_2_event', answer); //<-- The days_2_event field TYPE = DURATION


}



The AjaxDurCalc script include is as follows: It is Active and Client Callable



var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor,


});






LaVerne Carrick


Global Inspections — Security Analyst


Policy, Risk and Oversight


PayPal Information Security



Phone: 480.862.7219



_30x123


9999 N. 90th Street


Scottsdale, AZ 85258


USA


droid
Kilo Explorer

Hey,



Try this onLoad script, this will change the field as a difference between two dates.



function onLoad() {


        var firstDate = g_form.getValue('current_date');


        var secondDate = g_form.getValue('start_date');


        g_form.setValue('days_to_event',gs.dateDiff(firstDate,secondDate, false));   // false will return in date format, true in string


   


}