Delete Record using Flow

There will be times when you may need to delete a record from Process Builder flows.

Business Problem

We have a shopping cart which was created by the user. For any cart ( and cart items) which are not checked out, they will be lying in the system and will not be easy to identify to cleanup later.

  1. The business requirement is to send an email to the Cart Owner after 1 day asking them for any help to complete their process
  2. If the cart is not checked out after 7 days, it needs to be removed.

For the solutionining, Process Builder flow is an easy option to look, however, flows does not let you delete records. You may want to write an apex class and have it scheduled but that is not a preferred option. We should stay with configurations as much as possible

Call an Apex Method from a Process

We can write an apex class with an invocable method to do just the deletion part. The apex class/method itself can be invoked from the flow, so this is a hybrid approach with more configuration and less code

public class DeleteOldCarts
{
    @InvocableMethod
    public static void DeleteCart(List<String> cartIdList)
    {
       List<RG_Cart__c> cartList =[select id from RG_Cart__c where id in :cartIdList];
        delete cartList;
   }
}

The complete flow for this user requirement is shown below.

  1. Whenever 1 day has passed from the cart creation, we send out an email alert
  2. Whenever 7 days has passed since the cart creation and it is still in pending stage, we delete the cart

Permanent link to this article: https://salesforcebuddy.com/2020/04/delete-record-from-flow/

Leave a Reply

Your email address will not be published.