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

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

Hello @Ankur Bawiskar ,
Yeah it worked. Thank you!