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

Hi @Vishal Kumar Srivastava ,

Glad to know that my script worked.

I believe I have given you the right direction to proceed which you can enhance further as per your requirement. I have shared solution to your original question asked.

Please mark my response as correct and close the thread.

Regards
Ankur

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

Hi Ankur,

This code is giving me the proper output with calculated field, but calculated field is not doing the calculation on daily bases.

I) Is there any way to run this dictionary(calculated field ) on every page load. Can I use a schedule job  for this if yes then how?

II)This calculated result is coming right in list view and list report as well, but in Multiple Pivot this result is coming as empty for all.

III)function field is also not supporting my requirement. At past I raised a ServiceNow Hi  ticket for this why calculated field data is coming in multiple pivot report. Is this a limitation?

 They suggested me to use function field instead of calculated field but that is also not giving  me  the right result and they told me this is new finding function field is not supporting your requirement and they will improve this in upcoming releases.

IV) How I can completed this requirement. Should I use business rule with schedule job b/s calculation should be change on daily bases can not be constant?

I need to fulfil this requirement asap. Please suggest me the best approach to fulfill this requirement.