I'll describe a series of actions that has been functioning 100% as expected in our org for a couple of years now and point out the issue:
- Leads come in via web-to-lead along with a campaign Id and a checkbox field indicating whether or not they are actually interested in the product.
- A Process (Process Builder flow) captures that event and runs the newly created lead into a Flow.
- If that checkbox is false - instead of adding the Lead to the Campaign we create a Person Account with its' details (what we would call a 'subscriber'), and add its related Contact to the campaign, and then delete the original Lead <--- this part, as of 31/12/20, has started causing the entire process to roll back - and I'm getting the error message in the email "Salesforce could not create this Lead", the error message being " Cannot save already-deleted object: {Lead.Id}".
I'm finding it really odd that the flow doesn't throw any errors but rather it propagates back to the web-to-lead as if the original saving of the record had failed - when, if the Delete element is removed from the flow, everything functions as expected, resulting in unwanted duplicate records*
Any help would be greatly appreciated.
*We're not using the built in duplicate rules because we do have a system of logging 'returns' on existing Leads and Person Accounts
1 answer
In this case you can create a invocable apex and pass Lead ID to it from flow, inside apex use a SOQL and get lead again based on lead ID, if found then delete it like an example
List<Lead> leads [ SELECT Id FROM Lead WHERE ID IN :leadIds];
if(leads.size() > 0) delete leads ;
For more information how to call apex from flow use this article
https://automationchampion.com/tag/call-an-apex-method-from-flow/