Populate duration when state is in review state

maithrimh
Tera Contributor

Hello, I am trying populate the value of the duration when the state is in review state using a business rule. I tried debugging this its entering the if part inside else if but the duration value is not getting populated.Please help me resolve this issue.

(function executeRule(current, previous /*null when async*/) {

    if (current.state == 10) {     //In draft state
        current.work_start = new GlideDateTime();
    }
    else if (current.state == 100) {         //In review state
        current.work_end = new GlideDateTime();
        if(current.work_start && current.work_end){
            var duration = GlideDateTime.subtract(current.work_start,current.work_end);
            current.calendar_duration = duration.getDurationValue();
        }
    }

})(current, previous);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@maithrimh 

your business rule should be Before update

try these changes

1) both start and end should be of date/time type

2) calendar_duration field should be of duration type

3) also use start and end as GlideDateTime object and not string

(function executeRule(current, previous /*null when async*/) {

    if (current.state == 10) { // In draft state
        current.work_start = new GlideDateTime();
    }
    else if (current.state == 100) { // In review state
        current.work_end = new GlideDateTime();
        if (current.work_start && current.work_end) {
            // Convert to GlideDateTime objects
            var start = new GlideDateTime(current.work_start);
            var end = new GlideDateTime(current.work_end);
            var duration = GlideDateTime.subtract(start, end);
            current.calendar_duration = duration;
        }
    }

})(current, previous);

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

6 REPLIES 6

Arun_Manoj
Mega Sage

Hi @maithrimh ,

 

(function executeRule(current, previous /*null when async*/) {

if (current.state == 10 && previous.state != 10) {
// Entered Draft State
current.work_start = new GlideDateTime();
}
else if (current.state == 100 && previous.state != 100) {
// Entered In Review State
current.work_end = new GlideDateTime();

if (current.work_start && current.work_end) {
var gdStart = current.work_start.getGlideObject();
var gdEnd = current.work_end.getGlideObject();
var duration = gdEnd.subtract(gdStart); // Returns GlideDuration
current.calendar_duration = duration.getDurationValue();
}
}

})(current, previous);

 

 

ensure your Business Rule is set to run:

  • When: Before update

  • Condition: current.state == 100 (or check in script)

  • Fields to watch: state

Please check with this script,

 

If the solution is helpful. Please mark it as Helpful

 

Thanks

Arun

var duration = gdEnd.subtract(gdStart);

Subtract function is not working. It is going inside if but is not displaying the value of duration after subtract.

Hi @maithrimh ,

try to debug values in each variable

Hello @Arun_Manoj ,

Is there any alternative for subtract function inside business rule? That is where it is getting stuck.