The CreatorCon Call for Content is officially open! Get started here.

How to get Due Date and Current Date Difference using BR rule?

Vishal Kumar Sr
Tera Contributor

How to get Due Date and Current Date Difference using BR? I have a scenario where I have a need to get Difference of current date and due date and need to publish via report. I need to show difference in days only. Should I use client script instead of BR?

I tried this :

var date=new GlideDate();

var current=date;

gs.info(current)  //Showing me the current Date

var gr=new GlideRecord('table_name');

var dd=gr.getValue('due_date');  or var dd=gr.getDisplayValue('due_date');

var diff=dd-current;

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @Vishal Kumar Srivastava ,

this script will work in scoped app and give the difference in days

Note: If you wish to populate the difference in some field when BR runs; the BR should be Before and not After

var currentTime = new GlideDateTime();
var actualDatetime = new GlideDateTime(current.due_date);
var dur = new GlideDuration();
dur = GlideDateTime.subtract(currentTime, actualDatetime);

var days = dur.getDayPart();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Vishal,

Following business rule will set field "diff_days" to number of days between due date and today.

(function executeRule(current, previous /*null when async*/ ) {
    var gdtDueDate = new GlideDateTime();
    gdtDueDate.setDisplayValue(due_date);

    var today = gs.nowDateTime().split(' ')[0];
    var dueDate = current.due_date.getDisplayValue().split(' ')[0];
   
    var diff = gs.dateDiff(today, dueDate, false);
    var days = diff.split(' ')[0];
	current.diff_days = diff.split(' ')[0];
})(current, previous);

That said, may be better to create a Function field if the difference days field is only needed for reportings.

https://docs.servicenow.com/bundle/sandiego-platform-administration/page/build/platform-functions/co...

Hi Hitoshi,

I tried your code in my BR, but It says that gs.nowDateTime() is not supported.

Thanks,

Vishal Kumar Srivastava

 

Hello Vishal 

Try This below code

var date = new GlideDate();
var today = date.setDisplayValue(date, gs.getProperty('glide.sys.date_format'));

Please mark my answer "helpfull" or "Mark as Correct Answer" if it helped you in your query 

Reg

Hemant Kumar Ch

Tech Mahindra

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @Vishal Kumar Srivastava ,

this script will work in scoped app and give the difference in days

Note: If you wish to populate the difference in some field when BR runs; the BR should be Before and not After

var currentTime = new GlideDateTime();
var actualDatetime = new GlideDateTime(current.due_date);
var dur = new GlideDuration();
dur = GlideDateTime.subtract(currentTime, actualDatetime);

var days = dur.getDayPart();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

I tried this script in my scoped application and It gave me the expected result for parent task Due Date but I want this result for related task due date. In my case there are multiple task associated with one parent task and each child task(related-list) having a due date and I'm trying to get the difference of due date and current date for that child task on every page load. I have to show the difference for all child task on report and for every task i'm getting the due date on report and there is another column name Days left until due, where I have to get this Calculation, but currently i'm getting the result as 'empty'.

How I can get this calculation for every child task, which are added in related list?

I created a field on form level and set this result on that field. But it is showing me result on only form view and on list view it is showing empty. I want to use this calculated result on my report and after adding this field on my report it is giving me empty result for all task. Can u guide me how I can get this calculated result in list view as well?

 

Thanks & Regards,

Vishal Kumar Srivastava