Business rule to validate/check dates

Baggies
Kilo Guru

I am trying to create a business rule to validate the work_start and work_end dates on a Change Request. I can do this with this BR:

Table: Change Request

Order: 40

When: Before

Insert - true

Update - true

Condition - current.work_start.changes() || current.work_end.changes()

Script:

if ((!current.work_start.nil()) && (!current.work_end.nil())) {

      var start = current.work_start.getGlideObject().getNumericValue();

      var end = current.work_end.getGlideObject().getNumericValue();

      if (start > end) {

  current.work_start.setError('start must be before end');

  current.work_end.setError('start must be before end');

              gs.addErrorMessage('Work Start date must be before Work End date');

              current.setAbortAction(true);

      }

}


This works just fine when I am on the change form, and hit save or update -great!

Now, the other requirement, is that when I close the Implementation Change Task, I also want the validation to run against the parent Change Request. If the dates are not validated, then do NOT close the Implementation Change Task, and ensure that the Implementation Change Task remains as its original state, and not "Closed" (ie Open or Work in Progress). (This part is fixed by adding the current.state = previous.state; before the abort action.

 

So I have copied, and modified the original BR to run on the task:

Name: Validate Work Dates - from Task

Table: Change Task

Order: 35

When: Before

Insert - true

Update - true

Condition - current.work_start.changes() || current.work_end.changes()

Script:

if ((!current.change_request.work_start.nil()) && (!current.change_request.work_end.nil())) {
      var start = current.change_request.work_start.getGlideObject().getNumericValue();
      var end = current.change_request.work_end.getGlideObject().getNumericValue();
      if (start > end) {

  gs.log("**work start date is:   " + current.change_request.work_start);
  current.change_request.work_start.setError('start must be before end');
  current.change_request.work_end.setError('start must be before end');
              gs.addErrorMessage('*** Work Start date must be before Work End date on Change Request ***');
            current.state = previous.state;          

            current.setAbortAction(true);
      }
}


So I now have 2 working business rules running. My issues are:

How to only run one business rule when needed, I thought I could do this with the order

 

There are a few wiki/forum articles on this, and I have tried a few of them, in fact, this   is where I got the script from. Appreciate any input, I will post best answers, thanks, Mark S.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

You seem to have solved your issue with both of those Business Rules, so I'm not sure what the concern is.   Only one of the BRs will run at a time, it all depends on what record is being saved.   If you are attempting to save the Change Task, only the second BR will run.   The first will only run if you are attempting to save the original Change Request record.



Are you concerned about maintaining the two bits of code?   You don't have much choice but to do it this way.   You could get fancy and create a Script Include with the logic in it to check in both scenarios, but you would still need the Business Rules to call the Script Include.


View solution in original post

4 REPLIES 4

Jim Coyne
Kilo Patron

You seem to have solved your issue with both of those Business Rules, so I'm not sure what the concern is.   Only one of the BRs will run at a time, it all depends on what record is being saved.   If you are attempting to save the Change Task, only the second BR will run.   The first will only run if you are attempting to save the original Change Request record.



Are you concerned about maintaining the two bits of code?   You don't have much choice but to do it this way.   You could get fancy and create a Script Include with the logic in it to check in both scenarios, but you would still need the Business Rules to call the Script Include.


As Jim stated, I have achieved what I wanted, but with 2 pieces of code. I was annoyed at having 2 error messages displayed on each date field - but simply commented out the field error on one BR, and let the other one do the job. I will leave this here as someone else might find the BR useful. Thanks for the reply Jim.


Mark S.


I understand now.   I did not notice the...



current.change_request.work_start.setError('start must be before end');


current.change_request.work_end.setError('start must be before end');



...lines in the second BR.   They are really not needed because you are viewing the Change Task at that point and not the Change Request.   Are those the ones you removed?


My client has a section on the task form for Change Request details - so that part is visible from the task. Thanks.