To mark checkbox to true depending on valid to date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 11:57 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 12:30 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 01:02 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 01:10 PM
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()
Note that this will not take account of the time, only the date.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 01:22 PM
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.