Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

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,

2 ACCEPTED SOLUTIONS

joshuajacks
Kilo Sage

Hey @shubhi_shubhi If auditing is enabled for the table, you could query the "sys_history_line" table in a script include for the change to "New" on an onLoad client script. Then show the message if the script include returns true.

View solution in original post

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

View solution in original post

5 REPLIES 5

Tanushree Maiti
Tera Patron

Hi @shubhi_shubhi 

 

1)To create new enhancement following roles are required,

 

Before you begin

Role required: Any one of the feature_user, scrum_story_creator, scrum_admin, or rm_enhancement_admin roles

 

Ensure you are not assigning  above roles to your end user so that they can create new.

 

2) To display a message every time an existing enhancement record is accessed, use a Client Script and check for the desired state. 

  • Table: rm_enhancement
  • Type: OnLoad

 

function onLoad() {

    var currentState = g_form.getValue('state');

        if (currentState == '4')  //update New state’s value

{

        g_form.addInfoMessage("Your custom info message goes here.");

    }

}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

we are using rm_enhancement table for different use

Hi @shubhi_shubhi 

 

For getting the correct solution , you need to mention -  for what purpose you are looking for this customization. what is the business justification for this requirement .

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

joshuajacks
Kilo Sage

Hey @shubhi_shubhi If auditing is enabled for the table, you could query the "sys_history_line" table in a script include for the change to "New" on an onLoad client script. Then show the message if the script include returns true.