SalesforceLabs / ActionPlansV4

Action Plans encapsulates best practices into reusable task templates.
https://salesforcelabs.github.io/ActionPlansV4/
BSD 3-Clause "New" or "Revised" License
21 stars 11 forks source link

Unable to delete existing Action Plan(s) with 'Delete Records' Flow element #170

Closed jsams-merkle closed 8 months ago

jsams-merkle commented 8 months ago

Describe the bug I have a Record-Triggered Flow set up to manage Action Plans created from AP Templates against Opportunities. The AP that should be running is based on the Stage of the Opportunity. When the Stage changes, I need any/all previous AP against that Opportunity to be closed/deleted/cancelled so that only the one for the current stage is running.

For example:

To manage the creation of the Action Plans against the Opportunity, I'm using the invocable Flow Action within my Record-Triggered Flow. This part works exactly as expected.

What does not work is deleting the existing APs on the Opportunity using a "Delete Records" element within the Flow. I get the following error from Flow.

(line breaks added for ease of viewing)
Error element Delete_Previous_Action_Plans (FlowRecordDelete).
The flow tried to delete these records: a6Sxxxxxxxxxxxxxxx. This error occurred:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: LabsActionPlans.ActionPlan:
execution of BeforeDelete caused by: System.DmlException: Delete failed.
First exception on row 0 with id 00Txxxxxxxxxxxxxxx; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LabsActionPlans.APTask:
execution of BeforeDelete caused by: System.DmlException: Update failed.
First exception on row 0 with id a6Pxxxxxxxxxxxxxxx; first error:
FIELD_CUSTOM_VALIDATION_EXCEPTION, Error when deleting newly-dependent Task related to APTaskTemplate:
Delete failed. First exception on row 0 with id 00Txxxxxxxxxxxxxxx;
first error: SELF_REFERENCE_FROM_TRIGGER,
Object (id = 00Txxxxxxxxxxxxxxx) is currently in trigger LabsActionPlans.APTask,
therefore it cannot recursively delete itself: []: [] (LabsActionPlans): [] (LabsActionPlans).
You can look up ExceptionCode values in the SOAP API Developer Guide.

To Reproduce Steps to reproduce the behavior:

  1. Create multiple Action Plan Templates
  2. Go to Setup > Process Automation > Flows
  3. Click on 'New Flow'
  4. Select 'Record-Triggered Flow'
  5. Select the desired object (Opportunity, for me)
  6. Add a 'Get Records' element to look for existing Action Plan records related to the triggering record. Save the results in a collection variable.
  7. Add a 'Decision' element to check that the collection resource has content (I've used an Assignment element to set a number variable to the count of records contained in the collection)
  8. Add a 'Delete Records' element to delete the records contained in the collection
  9. Save your Flow and run a debug to test
  10. See error

I've included two screenshots showing the relevant portion of the Flow build. Screenshot from 2024-01-24 13-09-06

Screenshot from 2024-01-24 13-09-27

Expected behavior I would expect the 'Delete Records' element in a Flow is able to successfully delete any Action Plans, including uncompleted AP Tasks and Tasks, without error. Or I would expect another Flow Action that can be used to cancel/close/delete any Action Plan.

Screenshots This screenshot shows the error. Screenshot from 2024-01-24 13-10-03

Environment (please complete the following information):

Additional context N/A

jsams-merkle commented 8 months ago

Just a thought, but do incomplete Action Plan Tasks have to be deleted before the Action Plan(s) can be deleted?

jsams-merkle commented 8 months ago

Yeah, that was the issue. Hopefully this issue thread helps someone else out there in the future :)

  1. Retrieve any AP Task records associated with the retrieved Action Plans
  2. Delete those AP Task records
  3. Then delete the Action Plan records
  4. voila!