The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How To close the Task via Transform Map Script (or cancel workflow)

Atik
Tera Contributor

Hi All,

I have a requirement to create RITMs from an Excel sheet for a catalog form. I've already imported the open records and generated RITMs successfully. Now, the customer wants us to import closed or historical data too.
According to the workflow, first an approval goes to the manager group, then a task is created. After the task is completed, another approval is triggered.

For historic data, I  have bypassed both approvals.
Now I need to close both the RITM and the Task. RITM is getting closed properly, but the Task is still stuck in the open state. I'm not able to close it.
Requesting help from anyone who can guide me on how to close the Task as well.

3 ACCEPTED SOLUTIONS

Rafael Batistot
Kilo Patron

Hi @Atik 

 

May you consider a simple business rule

 

Business Rule Setup:

Table: sc_req_item (RITM)

When: after update

Filter condition: state changes to Closed Complete (or any “closed” state you use)

Action: find all related sc_task records and close them.


Code:

(function executeRule(current, previous /*null when async*/) {

// Only run when RITM moves to closed state
if (current.state == 3 && previous.state != 3) { // 3 = Closed Complete

var task = new GlideRecord("sc_task");
task.addQuery("request_item", current.sys_id);
task.addQuery("active", true); // only open tasks
task.query();

while (task.next()) {
task.state = 3; // Closed Complete
task.active = false; // mark inactive
task.work_notes = "Auto-closed because parent RITM was closed (historical load)";
task.update();
}
}

})(current, previous);

 

View solution in original post

kaushal_snow
Mega Sage

Hi @Atik ,

 

You can Set up a Business Rule on the sc_req_item (RITM) table that triggers when an RITM is updated to a closed state, and automatically closes any associated open Tasks…This is the best practice…

 

but, if you want another way..

 

 

If you want Tasks closed immediately during import, you could attempt it via a run script in your Transform Map..


var grTask = new GlideRecord('sc_task');
grTask.addQuery('request_item', target.sys_id);
grTask.addQuery('active', true);
grTask.query();
while (grTask.next()) {
grTask.state = 3;
grTask.active = false;
grTask.work_notes = "Closed during Transform Map script";
grTask.update();
}

 

But keep in mind: Transform Map scripts run during import, not after RITM state changes and You must include run script checkbox and reference the correct fields (target.sys_id) in the Transform Map.



If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

View solution in original post

MackI
Kilo Sage

hi @Atik 

 

Based on your case and the provided sources, the issue where your Service Catalog Tasks (sc_task records) remain open even though their associated Request Items (RITMs) are closed, is a direct consequence of bypassing the approval workflow for historical data. Standard workflows are typically responsible for managing the lifecycle of these tasks, including their closure.
Here's an explanation and guidance on how to close these tasks, specifically focusing on a Transform Map script or workflow cancellation.
 

Understanding the Root Cause

When you import historical RITMs and bypass approvals, you are essentially short-circuiting the designed fulfillment process. Service Catalog workflows often contain activities that create tasks, manage their states, and eventually close them once certain conditions (like approvals or other tasks completing) are met.
Unlike Incident Tasks, which have a system property (com.snc.incident.incident_task.closure) to automatically set open incident tasks to "Closed Incomplete" when an Incident is closed, or "Closed Skipped" when cancelled, there isn't a directly analogous, universally applied system property mentioned in the sources for Service Catalog Tasks (sc_task).
Similarly, for Change Requests, all open change tasks are set to "Canceled" when the parent Change Request moves to the "Review" state. However, even for Change, "manually created change tasks are not automatically closed or cancelled when state is changed from Implement to Review". This highlights that automatic closure of tasks is typically an explicit action within a workflow or a specific system property, not an inherent cascade for all task types.
Therefore, for your imported historical data, the tasks are stuck because the workflow path that would have closed them was never fully executed due to the bypassed approvals.
 

Solution: Closing the Task via Transform Map Script

The most robust and controlled method for historical data imports where workflows are intentionally bypassed is to programmatically close the tasks using a script. This can be implemented within your Transform Map using an onAfter script or as a separate Fix Script.
Here's a breakdown of the approach:
1. Identify Target Tasks: You need to pinpoint the sc_task records that are associated with the RITMs you've already closed and are currently in an open state.
2. Develop an onAfter Transform Script: If your Transform Map is still active and you're importing RITM data (or a separate import for tasks), an onAfter script on the RITM transform can be used. If the RITM import is complete, a separate Fix Script would be more appropriate.
3. The script should:
◦ Query for the associated sc_task records for each RITM that has been processed and closed.
◦ Update the state of these tasks to an appropriate closed value. Common closed states for tasks include:
▪ 3 - Closed Complete: If you consider the task fulfilled as part of the historical record.
▪ 4 - Closed Incomplete: If the task was closed without full completion (e.g., due to bypassing).
▪ 7 - Closed Skipped: If the task was never truly "worked on" due to the historical nature and bypassed approvals.
 
Conclusion and Recommendations
1. Use a Script: Implement the provided script logic as an onAfter Transform Map script (if you are still importing data) or, more likely, as a Fix Script to target all the already imported and closed RITMs that have open tasks.
2. Verify State Values: Before running any script, always verify the exact numerical values for the 'Open', 'Work in Progress', 'Closed Complete', 'Closed Incomplete', and 'Closed Skipped' states on your sc_task table. These can be found by navigating to sys_choice.list and filtering for Table = sc_task and Element = state.
3. Test Thoroughly: Always test any script in a non-production environment (Development or Test) first. Ensure that the script correctly identifies and closes only the intended tasks and that the state, close code, and close notes are set as expected.
4. Review Audit History: Be aware that setWorkflow(false) prevents processing of all engines and may cause the update to not appear in the audit history of the task. For historical data, this might be acceptable, but it's important to be aware of.
◦ Provide close_code and close_notes to clearly indicate why and how the task was closed for auditing purposes.
◦ Crucially, use setWorkflow(false) before updating the task. This prevents any business rules, workflows, or engines from triggering on the task update, which could lead to unintended consequences or errors. This is essential when doing bulk, programmatic updates for historical data.
 
 
A small request from my end, If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌🌠‌  ‌‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community, If you want a script then  pls provide your Excel sheet content and based on that I will developed your custom script but before that make sure you tag my answer as best answer‌🌠‌ 
 
 
 
 
MackI | ServiceNow Technical Consultant | DXC Technology Australia | ServiceNow Practice | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia

View solution in original post

3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @Atik 

 

May you consider a simple business rule

 

Business Rule Setup:

Table: sc_req_item (RITM)

When: after update

Filter condition: state changes to Closed Complete (or any “closed” state you use)

Action: find all related sc_task records and close them.


Code:

(function executeRule(current, previous /*null when async*/) {

// Only run when RITM moves to closed state
if (current.state == 3 && previous.state != 3) { // 3 = Closed Complete

var task = new GlideRecord("sc_task");
task.addQuery("request_item", current.sys_id);
task.addQuery("active", true); // only open tasks
task.query();

while (task.next()) {
task.state = 3; // Closed Complete
task.active = false; // mark inactive
task.work_notes = "Auto-closed because parent RITM was closed (historical load)";
task.update();
}
}

})(current, previous);

 

kaushal_snow
Mega Sage

Hi @Atik ,

 

You can Set up a Business Rule on the sc_req_item (RITM) table that triggers when an RITM is updated to a closed state, and automatically closes any associated open Tasks…This is the best practice…

 

but, if you want another way..

 

 

If you want Tasks closed immediately during import, you could attempt it via a run script in your Transform Map..


var grTask = new GlideRecord('sc_task');
grTask.addQuery('request_item', target.sys_id);
grTask.addQuery('active', true);
grTask.query();
while (grTask.next()) {
grTask.state = 3;
grTask.active = false;
grTask.work_notes = "Closed during Transform Map script";
grTask.update();
}

 

But keep in mind: Transform Map scripts run during import, not after RITM state changes and You must include run script checkbox and reference the correct fields (target.sys_id) in the Transform Map.



If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

MackI
Kilo Sage

hi @Atik 

 

Based on your case and the provided sources, the issue where your Service Catalog Tasks (sc_task records) remain open even though their associated Request Items (RITMs) are closed, is a direct consequence of bypassing the approval workflow for historical data. Standard workflows are typically responsible for managing the lifecycle of these tasks, including their closure.
Here's an explanation and guidance on how to close these tasks, specifically focusing on a Transform Map script or workflow cancellation.
 

Understanding the Root Cause

When you import historical RITMs and bypass approvals, you are essentially short-circuiting the designed fulfillment process. Service Catalog workflows often contain activities that create tasks, manage their states, and eventually close them once certain conditions (like approvals or other tasks completing) are met.
Unlike Incident Tasks, which have a system property (com.snc.incident.incident_task.closure) to automatically set open incident tasks to "Closed Incomplete" when an Incident is closed, or "Closed Skipped" when cancelled, there isn't a directly analogous, universally applied system property mentioned in the sources for Service Catalog Tasks (sc_task).
Similarly, for Change Requests, all open change tasks are set to "Canceled" when the parent Change Request moves to the "Review" state. However, even for Change, "manually created change tasks are not automatically closed or cancelled when state is changed from Implement to Review". This highlights that automatic closure of tasks is typically an explicit action within a workflow or a specific system property, not an inherent cascade for all task types.
Therefore, for your imported historical data, the tasks are stuck because the workflow path that would have closed them was never fully executed due to the bypassed approvals.
 

Solution: Closing the Task via Transform Map Script

The most robust and controlled method for historical data imports where workflows are intentionally bypassed is to programmatically close the tasks using a script. This can be implemented within your Transform Map using an onAfter script or as a separate Fix Script.
Here's a breakdown of the approach:
1. Identify Target Tasks: You need to pinpoint the sc_task records that are associated with the RITMs you've already closed and are currently in an open state.
2. Develop an onAfter Transform Script: If your Transform Map is still active and you're importing RITM data (or a separate import for tasks), an onAfter script on the RITM transform can be used. If the RITM import is complete, a separate Fix Script would be more appropriate.
3. The script should:
◦ Query for the associated sc_task records for each RITM that has been processed and closed.
◦ Update the state of these tasks to an appropriate closed value. Common closed states for tasks include:
▪ 3 - Closed Complete: If you consider the task fulfilled as part of the historical record.
▪ 4 - Closed Incomplete: If the task was closed without full completion (e.g., due to bypassing).
▪ 7 - Closed Skipped: If the task was never truly "worked on" due to the historical nature and bypassed approvals.
 
Conclusion and Recommendations
1. Use a Script: Implement the provided script logic as an onAfter Transform Map script (if you are still importing data) or, more likely, as a Fix Script to target all the already imported and closed RITMs that have open tasks.
2. Verify State Values: Before running any script, always verify the exact numerical values for the 'Open', 'Work in Progress', 'Closed Complete', 'Closed Incomplete', and 'Closed Skipped' states on your sc_task table. These can be found by navigating to sys_choice.list and filtering for Table = sc_task and Element = state.
3. Test Thoroughly: Always test any script in a non-production environment (Development or Test) first. Ensure that the script correctly identifies and closes only the intended tasks and that the state, close code, and close notes are set as expected.
4. Review Audit History: Be aware that setWorkflow(false) prevents processing of all engines and may cause the update to not appear in the audit history of the task. For historical data, this might be acceptable, but it's important to be aware of.
◦ Provide close_code and close_notes to clearly indicate why and how the task was closed for auditing purposes.
◦ Crucially, use setWorkflow(false) before updating the task. This prevents any business rules, workflows, or engines from triggering on the task update, which could lead to unintended consequences or errors. This is essential when doing bulk, programmatic updates for historical data.
 
 
A small request from my end, If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌🌠‌  ‌‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community, If you want a script then  pls provide your Excel sheet content and based on that I will developed your custom script but before that make sure you tag my answer as best answer‌🌠‌ 
 
 
 
 
MackI | ServiceNow Technical Consultant | DXC Technology Australia | ServiceNow Practice | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia