Default "Close Notes" is not working with ServiceNow Regex

VijaysaikumarB
Tera Contributor

 

Hi Everyone,

I have a requirement regarding the incident form. When a user changes the state to "Resolved," the close notes information will be auto populated with the default format below. Users need to update the information after the colon in this format. If the information is not updated/matches the original content, or is left empty after the colon, the action will be aborted.

Here is the business rule script I have, but it is not working as expected. Could you please help me resolve this issue?

 

Default format for close notes below:

Resolution Steps: 

Step 1

  • Action Taken: [DETAILS OF THE ACTION]
  • Observed Outcome: [WHAT WAS OBSERVED]

Step 2

  • Action Taken: [DETAILS OF THE ACTION]
  • Observed Outcome: [WHAT WAS OBSERVED]

Summary

  • Root Cause Identified: [EXPLAIN THE MAIN CAUSE]
  • Final Resolution: [DESCRIBE THE SOLUTION IMPLEMENTED]
  • Comments or Notes: [ADDITIONAL USEFUL INFORMATION]

Script:

(function executeRule(current, previous /*null when async*/) {
    
    // Define the default resolution template values
    var resolutionTemplate = `
    Resolution Steps:
    Step 1
    • Action Taken: [DETAILS OF THE ACTION]
    • Observed Outcome: [WHAT WAS OBSERVED]
    Step 2
    • Action Taken: [DETAILS OF THE ACTION]
    • Observed Outcome: [WHAT WAS OBSERVED]
    Summary
    • Root Cause Identified: [EXPLAIN THE MAIN CAUSE]
    • Final Resolution: [DESCRIBE THE SOLUTION IMPLEMENTED]
    • Comments or Notes: [ADDITIONAL USEFUL INFORMATION]
    `;
    
    // Extract each section of the resolution template to validate
    var sections = resolutionTemplate.split("\n");
    var invalidUpdate = false;

    // Iterate through each section of the resolution template
    for (var i = 0; i < sections.length; i++) {
        var line = sections[i];
        
        // Skip the 'Resolution Steps:' section
        if (line.indexOf("Resolution Steps:") > -1) {
            continue;
        }

        // Only check sections after the colon ":"
        if (line.indexOf(":") > -1) {
            var sectionContent = line.split(":")[1].trim();

            // Check if the content after the colon in 'close_notes' is unchanged or contains the placeholder
            if (current.close_notes.indexOf(sectionContent) > -1 || sectionContent === "" || 
                current.close_notes.indexOf("[") > -1) {
                invalidUpdate = true;
                break; // If any invalid content is found, abort the action
            }
        }
    }

    // If invalid update is detected, abort the action and show an error message
    if (invalidUpdate) {
        gs.addErrorMessage("You must update all sections after the colon in the 'close_notes' field. Please provide new information.");
        current.setAbortAction(true); // Prevent saving the record
    }

})(current, previous);

 

Thanks,

Vijay

 

1 REPLY 1

Viraj Hudlikar
Giga Sage

Hello @VijaysaikumarB 

It seems your thread is duplicate, however I posted a reply on it. Do check it.

Re: ServiceNow Regex - Close Notes - ServiceNow Community

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.