Business Rule script to populate the date/time on field incident table level.

LaraReddy
Tera Guru

HI,

When we set the state to on hold and on hold reason field to any one option, then we want to populate the date/time on "On Release" field level.

During the process, when we change the on hold reason field to any other options without changing the state from on hold then we don't want to change the populated date/time.

And the populated the time should change only when we change the state from on hold.

We wrote the below business rule to run when state changes to on hold and it's working fine.

Now the requirement is we don't want to populate date/time when the "on hold reason" field option is either A or C.

Can anyone please help us to adjust the below script to skip the date/time.

(function executeRule(current, previous /*null when async*/ ) {
//functionality configurations
var tz = "America/New_York"; 
var hours = 48;
var rightNow = new GlideDateTime();
var dur = new GlideDuration(3600000 * hours); 

var schedule = new GlideSchedule('021479371bdb99d04649c845604bcb58', tz);
var releaseDT = schedule.add(rightNow, dur);
current.u_hold_release = releaseDT;

})(current, previous);


Thanks,
LARA REDDY




1 ACCEPTED SOLUTION

HI Lara,
Hope the below script helps you.



(function executeRule(current, previous /*null when async*/ ) {
    //functionality configurations
    var tz = "America/New_York";
    var hours = 48;
    var rightNow = new GlideDateTime();
    var dur = new GlideDuration(3600000 * hours);


    var popsTime = current.u_capture_time; // getting the pre populated data

    // if the hold reason is problem or change , clearing the populated data

    if ((current.hold_reason == '3' || current.hold_reason == '5') && (!previous.u_hold_release.nil())) {
        gs.addErrorMessage('three');
        current.u_hold_release = '';
    }
    // state is still on hold and on hold field changes to some other options then populating the existing date/time.
    else if ((current.state == previous.state) && (current.hold_reason == '1' || current.hold_reason == '2' || current.hold_reason == '4') && (!current.u_capture_time.nil())) {
        gs.addErrorMessage('one');
        current.u_hold_release = popsTime;
    }

    // state is on hold and on hold is any one option we are setting the future date/time in on release field level.
    else if ((current.state == '3') && (current.hold_reason == '1' || current.hold_reason == '2' || current.hold_reason == '4') && (current.u_capture_time.nil())) {
        gs.addErrorMessage('two');
        var schedule = new GlideSchedule('021479371bdb99d04649c845604bcb58', tz);
        var releaseDT = schedule.add(rightNow, dur);
        current.u_hold_release = releaseDT;
        current.u_capture_time = releaseDT;
    }
})(current, previous);

 
Thanks,
AR

View solution in original post

10 REPLIES 10

@LaraReddy 

you need to ensure your BR doesn't run for the case you wish to skip it

Handle this using BR conditions

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

Thanks for the response Ankur.

We are able to skip to populating the date/time on On Reason field level.

We're facing only one below issue:

Example:
1. State is On hold && on hold reason is B =     2023-11-08 04:35:25  [date/time is populated]
2. State is still same && on hold reason is changed to A ==  On Reason field populated date/time is cleared
3. And now still state is same and on hold reason is changes to B or D == We need to set the first populated date/time [2023-11-08 04:35:25] again because we're not changed the state from on hold.


I hope you got my requirement/issue.

@LaraReddy 

unless you track in some field when was the 1st time the value was populated and when it got changed, you won't be able to know

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

Hi Ankur,
We tried using to capture the populated date/time in one variable and using the same date/time to populate again when on on hold reason is changes to B or D from A.

The only issue we are facing now is 

1. State is on hold and on hold reason is A ---> date/time will not populate. [It's working fine]
2. state is same and on hold reason B or D ---> the new date/time is not populating [As per the process it should populate new date/time but it's not populating and it's goes under error :one ].

Please check the below script once:

 

(function executeRule(current, previous /*null when async*/ ) {
    //functionality configurations
    var tz = "America/New_York";
    var hours = 48;
    var rightNow = new GlideDateTime();
    var dur = new GlideDuration(3600000 * hours);

    var popsTime = current.u_capture_time;

    if ((current.hold_reason == '3' || current.hold_reason == '5') && (!previous.u_hold_release.nil())) {
        gs.addErrorMessage('three');
        current.u_hold_release = '';
    }
 
    else if ((current.state == previous.state) && (current.hold_reason == '1' || current.hold_reason == '2' || current.hold_reason == '4')) {
        gs.addErrorMessage('one');   // this alert is coming but the value is not popualting becuase earlier there is no value.
        current.u_hold_release = popsTime;
    }
 
    else if ((current.state != previous.state) && (current.hold_reason == '1' || current.hold_reason == '2' || current.hold_reason == '4')) {
        gs.addErrorMessage('two');
        var schedule = new GlideSchedule('021479371bdb99d04649c845604bcb58', tz);
        var releaseDT = schedule.add(rightNow, dur);
        current.u_hold_release = releaseDT;
        current.u_capture_time = releaseDT;
    }

   


})(current, previous);


Advance thanks.


HI Lara,
Hope the below script helps you.



(function executeRule(current, previous /*null when async*/ ) {
    //functionality configurations
    var tz = "America/New_York";
    var hours = 48;
    var rightNow = new GlideDateTime();
    var dur = new GlideDuration(3600000 * hours);


    var popsTime = current.u_capture_time; // getting the pre populated data

    // if the hold reason is problem or change , clearing the populated data

    if ((current.hold_reason == '3' || current.hold_reason == '5') && (!previous.u_hold_release.nil())) {
        gs.addErrorMessage('three');
        current.u_hold_release = '';
    }
    // state is still on hold and on hold field changes to some other options then populating the existing date/time.
    else if ((current.state == previous.state) && (current.hold_reason == '1' || current.hold_reason == '2' || current.hold_reason == '4') && (!current.u_capture_time.nil())) {
        gs.addErrorMessage('one');
        current.u_hold_release = popsTime;
    }

    // state is on hold and on hold is any one option we are setting the future date/time in on release field level.
    else if ((current.state == '3') && (current.hold_reason == '1' || current.hold_reason == '2' || current.hold_reason == '4') && (current.u_capture_time.nil())) {
        gs.addErrorMessage('two');
        var schedule = new GlideSchedule('021479371bdb99d04649c845604bcb58', tz);
        var releaseDT = schedule.add(rightNow, dur);
        current.u_hold_release = releaseDT;
        current.u_capture_time = releaseDT;
    }
})(current, previous);

 
Thanks,
AR