In servicenow i wrote a business rule that glide record sc task and search the particular ritm and for all records we store the row count in one variable and then put condition. If variable a <2 return false and then if else loop again to check if any task whose short description is awesome and state is closed complete or incomplete then return false. Of both the loops doesnt matched then return true. How to do it share the code please
Assistant
Certainly! To create a Business Rule in ServiceNow that accomplishes what you've described, you'll want to focus on scripting using the GlideRecord API to gather data from sc_task records linked to a specific RITM. Here is an example of how you might write this code:
Business Rule Setup:
Trigger: Before Insert/Update.
Table: The table you need to target (likely sc_task or another relevant one).
Conditions: Based on your requirement, ensure it's applicable only to cases where this logic should run.
Business Rule Script:
Create the script in the "Script" section of the Business Rule.
Here's a possible implementation:
javascript
(function executeRule(current, previous /*null when async*/ ) {
// Initialize GlideRecord for the sc_task table
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.request_item); // Filter based on the RITM linked to the current record
taskGR.query();
var count = 0; // Variable to keep track of the task record count
while (taskGR.next()) {
count++; // Increment count for each record found
}
// Check if the count is less than 2
if (count < 2) {
gs.addInfoMessage('Less than 2 tasks found for this RITM.');
return false; // Return false if less than 2 tasks
}
// Re-query to check for specific conditions on tasks
taskGR.initialize();
taskGR.addQuery('request_item', current.request_item);
taskGR.addQuery('short_description', 'CONTAINS', 'awesome');
taskGR.addQuery('state', 'IN', ['3', '4']); // Assuming 3 and 4 are states for Closed Complete or Incomplete
taskGR.query();
while (taskGR.next()) {
// Check for tasks matching new criteria
if ((taskGR.state == '3' || taskGR.state == '4') && taskGR.short_description.toLowerCase().indexOf('awesome') != -1) {
gs.addInfoMessage('Task with awesome description found that is Closed Complete or Incomplete.');
return false; // Return false if conditions match
}
}
// If none of the conditions match, return true
return true;
})(current, previous);
Explanation:
Querying Tasks: First, a GlideRecord query is used to retrieve all tasks linked to the specified Request Item (RITM). We use the condition taskGR.addQuery('request_item', current.request_item); to filter tasks for the current RITM.
Count Check: We count the tasks and check if there are fewer than two.
Conditional Task Check: A second pass through the tasks checks for any tasks with specific properties (e.g., short description contains 'awesome', and the state is either closed complete or incomplete).
Return Logic: Depending on the tasks found, the function will return false or true.
Remember to replace state values with the appropriate numeric values for your instance, and adjust field names as needed to match your table structure.