- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 02:35 PM
Hello, good morning. I am working on a flow of a catalog that generates 10 tasks where each one has close complete and close incomplete but I encounter the following problem, I cannot do that depending on the status in which these tasks are closed Close complete or incomplete then the status of the entire process is closed in the status that most tasks have been closed
Could someone guide me please?
How can I do so that my workflow counts how many tasks are being closed in "close Incomplete" and how many in "close Complete" and knowing the total of each one with its Status (close complete or close incomplete) condition it
a if 10 tasks 9 are "close incomplete" then the Ritm and Request are closed in Close Incomplete
but if 9 tasks are closed in "Close Complete" then the Ritm and Request are closed with a "Close Complete"
Does anyone know how I can achieve this from the workflow to let them know in which Status they should close the process?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 04:07 PM
Hello @williams2,
You can use a script to count the number of tasks that are closed in “Close Incomplete” and “Close Complete” and compare them. You can use the GlideAggregate class to perform aggregate queries on a table, such as counting the number of records that match a condition. You can also use the GlideRecord class to update the RITM and Request records based on the result of the comparison.
Here is an example of a script that you can use in a Run Script activity in your workflow:
// Get the current RITM sys_id from the workflow scratchpad
var ritmSysId = workflow.scratchpad.ritmSysId;
// Create a GlideAggregate object for the sc_task table
var taskAgg = new GlideAggregate('sc_task');
// Add a query to filter by the RITM sys_id
taskAgg.addQuery('request_item', ritmSysId);
// Add an aggregate function to count the number of tasks
taskAgg.addAggregate('COUNT');
// Group by the state field
taskAgg.groupBy('state');
// Execute the query
taskAgg.query();
// Initialize variables to store the number of tasks in each state
var closeCompleteCount = 0;
var closeIncompleteCount = 0;
// Loop through the results
while (taskAgg.next()) {
// Get the state value and the count value
var state = taskAgg.state.getDisplayValue();
var count = taskAgg.getAggregate('COUNT');
// Check if the state is Close Complete or Close Incomplete and update the corresponding variable
if (state == 'Close Complete') {
closeCompleteCount = count;
} else if (state == 'Close Incomplete') {
closeIncompleteCount = count;
}
}
// Create a GlideRecord object for the sc_req_item table
var ritmGr = new GlideRecord('sc_req_item');
// Get the RITM record by sys_id
if (ritmGr.get(ritmSysId)) {
// Compare the number of tasks in each state and update the RITM state accordingly
if (closeCompleteCount > closeIncompleteCount) {
// Set the RITM state to Close Complete
ritmGr.state = '3';
} else if (closeIncompleteCount > closeCompleteCount) {
// Set the RITM state to Close Incomplete
ritmGr.state = '4';
} else {
// Set the RITM state to Closed with No Status Change
ritmGr.state = '5';
}
// Update the RITM record
ritmGr.update();
}
// Get the Request sys_id from the RITM record
var requestSysId = ritmGr.request;
// Create a GlideRecord object for the sc_request table
var requestGr = new GlideRecord('sc_request');
// Get the Request record by sys_id
if (requestGr.get(requestSysId)) {
// Set the Request state to match the RITM state
requestGr.state = ritmGr.state;
// Update the Request record
requestGr.update();
}
Hope this helps.
Kind Regards,
Swarnadeep Nandy

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 04:07 PM
Hello @williams2,
You can use a script to count the number of tasks that are closed in “Close Incomplete” and “Close Complete” and compare them. You can use the GlideAggregate class to perform aggregate queries on a table, such as counting the number of records that match a condition. You can also use the GlideRecord class to update the RITM and Request records based on the result of the comparison.
Here is an example of a script that you can use in a Run Script activity in your workflow:
// Get the current RITM sys_id from the workflow scratchpad
var ritmSysId = workflow.scratchpad.ritmSysId;
// Create a GlideAggregate object for the sc_task table
var taskAgg = new GlideAggregate('sc_task');
// Add a query to filter by the RITM sys_id
taskAgg.addQuery('request_item', ritmSysId);
// Add an aggregate function to count the number of tasks
taskAgg.addAggregate('COUNT');
// Group by the state field
taskAgg.groupBy('state');
// Execute the query
taskAgg.query();
// Initialize variables to store the number of tasks in each state
var closeCompleteCount = 0;
var closeIncompleteCount = 0;
// Loop through the results
while (taskAgg.next()) {
// Get the state value and the count value
var state = taskAgg.state.getDisplayValue();
var count = taskAgg.getAggregate('COUNT');
// Check if the state is Close Complete or Close Incomplete and update the corresponding variable
if (state == 'Close Complete') {
closeCompleteCount = count;
} else if (state == 'Close Incomplete') {
closeIncompleteCount = count;
}
}
// Create a GlideRecord object for the sc_req_item table
var ritmGr = new GlideRecord('sc_req_item');
// Get the RITM record by sys_id
if (ritmGr.get(ritmSysId)) {
// Compare the number of tasks in each state and update the RITM state accordingly
if (closeCompleteCount > closeIncompleteCount) {
// Set the RITM state to Close Complete
ritmGr.state = '3';
} else if (closeIncompleteCount > closeCompleteCount) {
// Set the RITM state to Close Incomplete
ritmGr.state = '4';
} else {
// Set the RITM state to Closed with No Status Change
ritmGr.state = '5';
}
// Update the RITM record
ritmGr.update();
}
// Get the Request sys_id from the RITM record
var requestSysId = ritmGr.request;
// Create a GlideRecord object for the sc_request table
var requestGr = new GlideRecord('sc_request');
// Get the Request record by sys_id
if (requestGr.get(requestSysId)) {
// Set the Request state to match the RITM state
requestGr.state = ritmGr.state;
// Update the Request record
requestGr.update();
}
Hope this helps.
Kind Regards,
Swarnadeep Nandy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 10:51 AM
hi swarnadeep
Thank you very much for your help, I will be reviewing and coupling the example code that you have shown me
I really appreciate your help
Best regards
Williams