In servicenow how to add condition in if loop that, if the gr.state is closed complete…
- 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
- 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
ifloop to check if thegr.stateis either "Closed Complete" or "Incomplete" and then perform specific actions based on that check:-
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
statedictionary entry of the table that you are working with (liketaskor a specific inherited table likeincident). -
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_COMPLETEandINCOMPLETEto the actual values used in your ServiceNow instance. You can find these values by looking at the dictionary entries forstateor 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 (
afterbusiness 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.
-