Business rule to uncheck a checkbox

othomas1
Kilo Guru

Good Afternoon Community,

I am trying to write a business rule that unchecks a checkbox on a form when the expire date is reached, ive tested it out so far and i have not gotten the expected results, in fact nothing is happening. Can anyone see what the problem is? Maybe my script needs adjusting?

 

Business rule:

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

// Add your code here
var gdt = new GlideDateTime().getDate();

var acl = new GlideRecord('u_cmdb_ci_table_access_parameters');
acl.addQuery("u_expire_date", "==", gdt);
acl.query();

if (current.u_expire_date.setDisplayValue() == gs.nowDateTime()) {

current.u_active = false;

}
})(current, previous);

 

Form with checkbox and expire date variable:

find_real_file.png

 

 

find_real_file.png

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

It would be helpful if you described what your actual requirements are instead of how you are trying to accomplish them.  From what I gather, you want the "Active" checkbox to be unchecked once a certain date/time goes by, automatically, correct?

A Business Rule is probably not the best way to do that as it requires some kind of action in order for it to run.  And a Before Query one is definitely not the way.  They are normally used to alter a query/filter. (How business rules work).

A Workflow, as mentioned by someone else, makes more sense, but probably too expensive system-wise.

How many records are we talking about?  How time-sensitive is this data?  If not many and not very time-sensitive (i.e. does not have to happen immediately), a Scheduled Job may be your best bet.  It can be scheduled to run every hour or so (or more frequently) to update the records.

The script could look like this:

(function() {
    var gr = new GlideRecord("u_cmdb_ci_table_access_parameters");
    gr.addEncodedQuery("u_active=true^u_expire_dateRELATIVELE@minute@ago@0");
    gr.query();
    while (gr.next()) {
        gr.u_active = false;
        gr.update();
    }
})();

If it is time-sensitive, you could, in addition to the Scheduled Job, use a Before Display Business Rule to update the Active field when someone views the record in a form (will not affect when simply viewing records in a list view though):

find_real_file.png

And the Script field would be as simple as:

(function executeRule(current, previous /*null when async*/) {
	current.u_active = false;
	current.update();

})(current, previous);

View solution in original post

14 REPLIES 14

Abhijit4
Mega Sage

Hi,

I think you are making it more complicated for you, Please try with below code

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

// Add your code here


var todaysDate= new GlideDate();
var expire_date= new GlideDateTime(current.u_expire_date).getDate();

if (todaysDate.getDisplayValue() == expire_date.getDisplayValue()) {

current.u_active = false; 

}
})(current, previous);

 

Let me know if you need more help.

 

Mark it as correct or helpful if it really helps.

 

Thanks.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hello cody,

I tried using your updates but the checkbox remained checked.

 

find_real_file.png

If you have written after business rule then try using Current.update(); Use below code, function executeRule(current, previous /*null when async*/) { // Add your code here var todaysDate= new GlideDate(); var expire_date= new GlideDateTime(current.u_expire_date).getDate(); if (todaysDate.getDisplayValue() == expire_date.getDisplayValue()) { current.u_active = false; current.update(); } })(current, previous); Let me know if you need more help. Mark it as correct or helpful if it really helps. Thanks.
By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Jim Coyne
Kilo Patron

It would be helpful if you described what your actual requirements are instead of how you are trying to accomplish them.  From what I gather, you want the "Active" checkbox to be unchecked once a certain date/time goes by, automatically, correct?

A Business Rule is probably not the best way to do that as it requires some kind of action in order for it to run.  And a Before Query one is definitely not the way.  They are normally used to alter a query/filter. (How business rules work).

A Workflow, as mentioned by someone else, makes more sense, but probably too expensive system-wise.

How many records are we talking about?  How time-sensitive is this data?  If not many and not very time-sensitive (i.e. does not have to happen immediately), a Scheduled Job may be your best bet.  It can be scheduled to run every hour or so (or more frequently) to update the records.

The script could look like this:

(function() {
    var gr = new GlideRecord("u_cmdb_ci_table_access_parameters");
    gr.addEncodedQuery("u_active=true^u_expire_dateRELATIVELE@minute@ago@0");
    gr.query();
    while (gr.next()) {
        gr.u_active = false;
        gr.update();
    }
})();

If it is time-sensitive, you could, in addition to the Scheduled Job, use a Before Display Business Rule to update the Active field when someone views the record in a form (will not affect when simply viewing records in a list view though):

find_real_file.png

And the Script field would be as simple as:

(function executeRule(current, previous /*null when async*/) {
	current.u_active = false;
	current.update();

})(current, previous);

Good Morning Jim,

 

My requirements are as follows:

  1. Validate that when the expiration date/time is reached on a CMDB ACL that the ACL is automatically inactivated.
  2. Validate that when a CMDB ACL is automatically disabled, a notification is sent to the CAM team.
  3. Validate that when a CMDB ACL review date/time is reach that a notification for remove is sent to the CAM team.
  4. Validate that when a CMDB ACL is automatically disabled, a notification is sent to the person(s) whose access is removed.

In the module im working with there are only 14 records and none of them have the expire date populated yet, the requirements above are for future references when the module starts to be used more frequently.

 

 

find_real_file.png