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

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

Hello Maddysunil,
Many thanks for your reply. I used the script and it worked. The only change I made was that I reversed today and updated.

Mark Manders
Mega Patron

How is this business rule triggered? 
Why not run a scheduled flow daily (or hourly) to just do that check? Check if updated is 90 days or longer ago and update the field. Since it's 90 days, I don't think it will come to a day or an hour?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hello Mark. 
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