State Change to "New" – Info Message Requirement

shubhi_shubhi
Kilo Sage

Hi team,

 

There is a requirement for the rm_enhancement table: whenever the state changes to "New", an informational message should be displayed to the user when they open the record( user will not open the record immediately), indicating that the state has been updated to New.

 

State will get changed by the script not manually updating the state

 

Currently, we do not have the option to create a new field to track this state change (e.g., a flag such as "state changed to New").

 

Please suggest possible approaches to implement this behavior without introducing a new field.

 

**We are not using rm_enhancement for Agile Story or anything related to Story

 

Thanks,

5 REPLIES 5

Abhit
Tera Guru

@shubhi_shubhi ,

You can leverage HistoryWalker on form load, provided that auditing is enabled at the field or table level.

1. Create Display BR
Table: rm_enhancement
When: Display
Condition: Optional, but ideally only run when state is New

(function executeRule(current, previous /* previous is not used in display BR */ ) {

    /*
     * Replace this with the actual internal value of the "New" choice.
     * Do not use the label unless the value is also "New".
     */
    var NEW_STATE_VALUE = 'New';

    g_scratchpad.showStateChangedToNewMessage = false;

    if (current.getValue('state') != NEW_STATE_VALUE) {
        return;
    }

    var modCount = parseInt(current.getValue('sys_mod_count'), 10);

    if (isNaN(modCount) || modCount < 1) {
        return;
    }

    try {
        var hw = new sn_hw.HistoryWalker(
            current.getTableName(),
            current.getUniqueValue(),
            'CHECKPOINT'
        );

        /*
         * Walk backwards to find the latest point where state was not New.
         * This handles cases where the state changed to New earlier,
         * and then other fields were updated later.
         */
        for (var i = modCount - 1; i >= 0; i--) {
            hw.walkTo(i);
            var oldRecord = hw.getWalkedRecordCopy ?
                hw.getWalkedRecordCopy() :
                hw.getWalkedRecord();
            if (!oldRecord || !oldRecord.isValidRecord()) {
                continue;
            }
            var oldState = oldRecord.getValue('state');
            if (oldState != NEW_STATE_VALUE) {
                g_scratchpad.showStateChangedToNewMessage = true;
                g_scratchpad.stateChangedToNewMessage =
                    'State has been updated to New.';
                break;
            }
        }
    } catch (ex) {
        gs.warn(
            'Unable to evaluate previous state for rm_enhancement ' +
            current.getUniqueValue() +
            '. Error: ' + ex
        );
    }
})(current, previous);


2. Create onLoad Client Script:

Table: rm_enhancement
Type: onLoad

function onLoad() {
    if (g_scratchpad.showStateChangedToNewMessage == true || g_scratchpad.showStateChangedToNewMessage == 'true') {
        g_form.addInfoMessage('State has been updated to New.');
    }
}

 

Note:

If the record has a high sys_mod_count, iterating through history can impact performance.
Consider limiting the traversal range if needed.


For additional details on the HistoryWalker API, refer to: How to Retrieve Previous Field Values in ServiceNow Scripting

~Ak