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

Mike Patel
Tera Sage

try changing 

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

also change

acl.addQuery("u_expire_date", "STARTSWITH", gdt);

Hello Mike,

Those changes did not work for me.

Derek Behunin
Tera Guru

acl.addQuery("u_expire_date", "==", gdt); // This query only returns records that expire today. Change this to:

acl.addQuery("u_expire_date", "<=", gdt); // This now returns all records with an expire date before today.

 

Now you need to loop through returned records and change the field value.

 

while(acl.next()){

   acl.u_active = false;

}

 

Hope this helps!

Hello Derek,

I tried making those changes and it did not work for me. Currently there are not any expire dates set, so i am testing individual records by setting the expire date and then testing it that way.