- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 06:20 AM
Hi All,
I would like to set a Business Rule in order to automatically assign a status for the record between Active (= 1) or Inactive (=2). The goal should be assign Active status to records that have been updated in the last 90 days, and Inactive status to records that have not been updated in the last 90 days.
So, within the table, I created a new field "Status" (u_status) where the information should be inserted, of which type is "Choice" with 2 choices: Active / Inactive; and I set up the following script in the business rule:
(function executeRule(current, previous /* null when async */ ) {
Thanks.
Vito
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 06:48 AM
Could you pls try with below updated code:
(function executeRule(current, previous /* null when async */) {
var updated = current.sys_updated_on;
var todayTs = gs.nowDateTime(); // Get current date and time
var today = todayTs.split(' ')[0]; // Extract only the date part
// Calculate the difference in days between today and the last update date
var daysSinceUpdate = gs.dateDiff(today, updated.getDisplayValue(), true);
// Set status based on the difference in days
if (daysSinceUpdate <= 90) {
current.u_status = '1'; // Active
} else {
current.u_status = '2'; // Inactive
}
})(current, previous);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 07:22 AM
business rule will run only when some insert/update happens
when should this be running?
I believe you would require a one time scheduled job or flow which runs only once and populates the choice value based on your logic
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 07:43 AM
Hello Ankur.
This business rule is triggered only when records are inserted or updated.
Our goal is to upload data via Import Set every 90 days. So, if the record is created or updated within 90 days, we will consider it as Active; if the record isn't updated within 90 days, we will consider it as Unactive.
Anyway, thank you for the reply. I'm open to hearing alternative ideas