Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

In servicenow how to add condition in if loop that, if the gr.state is closed complete…

  1. User

    In servicenow how to add condition in if loop that, if the gr.state is closed complete or incomplete then do the following in business rule

  2. Assistant

    In ServiceNow, when writing a business rule to handle conditions based on the state of a record, you can use GlideRecord and JavaScript syntax to define the logic in your script. Here's an example of how you can add a condition in an if loop to check if the gr.state is either "Closed Complete" or "Incomplete" and then perform specific actions based on that check:

    1. Identify the numeric values or constant values used in your ServiceNow instance for the "Closed Complete" and "Incomplete" states. You will need these to accurately compare the state field in your condition. These are typically stored in the state dictionary entry of the table that you are working with (like task or a specific inherited table like incident).

    2. Create or edit a business rule with the appropriate conditions. You'll script your logic in the "Advanced" section if it's not already bespoke.

    Here is an example of how you might write a business rule script that checks for these states:

    (function executeRule(current, previous /*null when async*/) {
    
        // Assuming "Closed Complete" and "Incomplete" states are represented by these values
        var CLOSED_COMPLETE = 3; // Example value for closed complete
        var INCOMPLETE = 4; // Example value for incomplete
    
        // Check if the state is either Closed Complete or Incomplete
        if (current.state == CLOSED_COMPLETE || current.state == INCOMPLETE) {
            // Perform your actions here, for example:
            gs.addInfoMessage("The record is either in Closed Complete or Incomplete state.");
            // Additional logic can be added here
        }
    
    })(current, previous);
    

    Notes:

    • State Values: Modify CLOSED_COMPLETE and INCOMPLETE to the actual values used in your ServiceNow instance. You can find these values by looking at the dictionary entries for state or checking existing records with these states.
    • Trigger Conditions: Ensure your business rule has appropriate conditions to prevent it from running unnecessarily, such as running only on update and after the record has been saved (after business rule).
    • Testing: Test the business rule carefully in a sub-production environment before rolling it out to ensure it doesn't disrupt users by running unexpectedly.

    This script provides basic logic; be sure to integrate it appropriately into your business rules according to your specific needs and configurations in ServiceNow.