Business Rule Client Script to calculate duration field on a custom application

Scott29
Kilo Sage

I am trying to calculate a duration field "legal_review_duration" using 2 date/time fields "legal_hold_start" and "legal_hold_end". I created a business rule with the following script.

 

-------------------------------------------

 

Scott29_0-1749612004291.png

----------------------------------------------

 

When I try to test it, I am getting this error: 

Function dateDiff is not allowed in scope x_hoen_legal. Use GlideDateTime.subtract() instead
 
How can I rebuild this script to work in a custom scope?
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Scott29 

you should use GlideDateTime.subtract() in scoped app as dateDiff() is not supported

something like this in before update business rule

var start = new GlideDateTime(current.legal_hold_start);
var stop = new GlideDateTime(current.legal_hold_end);
var duration = GlideDateTime.subtract(start, stop);
current.legal_review_duration = duration;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Scott29 

you should use GlideDateTime.subtract() in scoped app as dateDiff() is not supported

something like this in before update business rule

var start = new GlideDateTime(current.legal_hold_start);
var stop = new GlideDateTime(current.legal_hold_end);
var duration = GlideDateTime.subtract(start, stop);
current.legal_review_duration = duration;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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