Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

To mark checkbox to true depending on valid to date.

Sam Hanson
Tera Contributor

Hi Peeps,

I have active field -checkbox- on cmn_cost_center table. Now I want to make checkbox to true depending on valid to date. For that reason I created the below BR

Name : Set active to True 

AFTER - Insert, Update.

 

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

// Get the "valid to" and current date
var validToDate = new GlideDateTime(current.valid_to);
var currentDate = new GlideDateTime();

// Check if "valid to" date is in the future
if (validToDate > currentDate) {
// Set the "active" field to true
current.active = true;
}

})(current, previous);
 
I have 2 questions here
1) do I need to create any conditions on this BR.
2) do I need to create a fix script to mark the existing cost centers active??
 
can anyone give me the best practice here

 

5 REPLIES 5

James Chun
Kilo Patron

Hi @Sam Hanson,

 

I have not checked the script itself, but noticed a few things:

  • Since you are missing current.update(), this script will not do anything. However, note that you should not use current.update() in a Business Rule as it may end up in a recursive loop - https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715782
  • Since the BR runs when a record is updated, there is a high probability that your script will never run (unless someone/system continuously updates the records but I highly doubt it)
  • Do you need a logic to set the 'active' back to false if the 'valid to' changes?

With the above in mind, I would suggest using a Scheduled Job that runs daily. But this is based on my assumption that it's not critical to update the 'active' field in real-time. Also, what was the business need for adding a custom field as it seems like you can use the' Valid to' field instead to check if a Cost Centre is valid or not.

 

Cheers

Hi @James Chun 

 From your first bullet, do you want current.update() to be used?? - I believe no.

Second Bullet - yes you are correct, script will not run🙂

Third Bullet - yes I need to set the active to false when valid to is past.

 

I agree with your idea of proceeding in a scheduled job route. But in this case if the scheduled job to set inactive will run at 00:10 but the cost center will be active untill the job ran right..? so its not a feasible solution..

 

Now the business need is I want the cost center records to be shown if only 'valid_to' is future. So I tried javascript: [{{'valid_to'>gs.now()}}], but this seems not working so I decided to create an active field on cost center table and make it true when it passes the valid_to date.

It should be okay, if I get the right reference qualifier.

Hi @Sam Hanson,

 

Apologies if I wasn't clear on the first bullet point. To elaborate, if you are using 'an After' Business Rule, you would need to use .update() to update a record. But since we shouldn't be using current.update() in a BR, it shouldn't be there in the first place.

 

In regards to using the 'valid_to', have you considered using an encoded query something like
valid_to>=javascript:gs.beginningOfToday()

JamesChun_0-1712088561707.png

Note that this will not take account of the time, only the date.

 

Cheers

I've tried using this and yes, it will not consider time-- So this was not feasible for me 

Another option would be writing a script include and use it in the reference qualifier, in order to avoid that I've chose the path of creating an active field and updating it with a BR.