Salesforce process builder doesn’t allow to delete records in as of yet. You can either write a trigger or use flows to delete the records automatically from salesforce.
Though flow builder really makes it easy it still is a challenge for most admins who are not familiar with the flow builder and doesn’t want to invest too much time creating a diagrammatic conditions. Hands down a process is much faster to create but is limited in terms of features when compared to flows.

But as a developer you can give the power of delete to your admins by creating invocable methods in apex and calling them using process builder.

The below apex class gets the object type and the record id and then deletes it if process builder’s condition is met. The great thing about it is that it is not object specific and can be used to delete any object’s record in salesforce.

public class invocableDeleteRecord {
    
    @InvocableMethod(label='Delete Record')
    
    public static void DeleteRecord(List recordIds){
        
        if(recordIds.size()>0){
            
            List deleteIds = new List();
        
        for(string recordID :recordIds ){
            Id rId = recordId;
            		
        sobject sO = rId.getSobjectType().newsobject(rId);
            
            deleteIds.add(sO);
        }
            
            delete deleteIds;
   }
        
 }

}

Test Class:

@isTest
public class invocableDeleteRecordTest {

    @isTest static void deleteRecordAccount(){
        
        List insertedAccount = new List();
        List accountIdList = new List();
        List newAccount = new List();
        
        for(integer i=0;i<5;i++){
            
            Account acc= new Account();
            acc.Name = 'Test123'+i;
            insertedAccount.add(acc);
            
        }
        	insert insertedAccount;
			        	
        for(Account a :insertedAccount )    {
            	accountIdList.add(a.Id);
        }
 	
        invocableDeleteRecord.DeleteRecord(accountIdList);
        
            Account acc= new Account();
            acc.Name = 'Test123';
            newAccount.add(acc);
            insert newAccount;
        
        List updatedAccount = [select id from Account where id IN: newAccount];
        
        system.assertEquals(1, updatedAccount.size());
        
   }
               
}

Then create a process by selecting the action Call Apex.
Select the class from the apex class option and add the record id as the input parameter.

This Process deletes opportunity record where checkbox Waiver is set to true:


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *