Automatic Status Update via Business Rules

vcicchirillo
Tera Contributor

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 */ ) {
     
        var updated = current.sys_updated_on
        var todayTs = gs.nowDateTime(); // this includes the time stamp so we need to remove it
        var today = (todayTs.split(' '))[0];

        if (gs.dateDiff(today, updated.getDisplayValue(), true) > 90) {
            current.u_status = 2; // Inactive
        }

        if (gs.dateDiff(today, updated.getDisplayValue(), true) < 90) {
            current.u_status = 1; // Active
        }
    }
    (current, previous))
 
Unfortunately, the Business Rule does not work. Could someone help me?

Thanks.
Vito
 
1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@vcicchirillo 

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@vcicchirillo 

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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