Why does SalesForce on before delete trigger delete record before processing?

1.9k views Asked by At

I have created a trigger in Salesforce :

 Trigger myTestDelete_D on Account (before delete) 
 {
   set<ID> ids = Trigger.oldMap.keyset(); 
   myClass.Details('Account','delete',ids); 
 }

So the steps are I create an account Acct1. I then delete the account , but in the call to myClass.Details I perform a select to the account with the id of the Acct1.

The issue is that the row has already been deleted and hence I get 0 rows. Is there some sort of setting that is required in Salesforce to wait until the trigger has completed before the entity is deleted ?

1

There are 1 answers

2
Óscar Gómez Balaguer On BEST ANSWER

I can't give you more details because we don't have the code of your "myClass" class but the query should retrieve the Accounts in the before delete trigger execution. I've created myself a trigger and set up some logs. Here are the results after deleting one Account record.

Trigger Code:

trigger Account on Account(before delete) {
  System.debug(Trigger.oldMap);
  System.debug(Trigger.newMap);

  List<Account> deletedAccounts = [
    SELECT Id, Name
    FROM Account
    WHERE Id IN :Trigger.oldMap.keySet()
  ];

  System.debug(deletedAccounts.size());
}

System.debug(Trigger.oldMap):

{0013O00000kVO2PQAW=Account:{Id=0013O00000kVO2PQAW, IsDeleted=false, MasterRecordId=null, Name=My Account, Type=null, ParentId=null, BillingStreet=null, BillingCity=null, BillingState=null, BillingPostalCode=null, BillingCountry=null, BillingLatitude=null, BillingLongitude=null, BillingGeocodeAccuracy=null, ShippingStreet=null, ShippingCity=null, ShippingState=null, ShippingPostalCode=null, ShippingCountry=null, ShippingLatitude=null, ShippingLongitude=null, ShippingGeocodeAccuracy=null, Phone=null, Fax=null, AccountNumber=null, Website=null, PhotoUrl=null, Sic=null, Industry=null, AnnualRevenue=null, NumberOfEmployees=null, Ownership=null, TickerSymbol=null, Description=null, Rating=null, Site=null, OwnerId=0053O0000044sSdQAI, CreatedDate=2021-10-05 16:47:55, CreatedById=0053O0000044sSdQAI, LastModifiedDate=2021-10-05 16:47:55, LastModifiedById=0053O0000044sSdQAI, SystemModstamp=2021-10-05 16:47:55, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, Jigsaw=null, JigsawCompanyId=null, CleanStatus=Pending, AccountSource=null, DunsNumber=null, Tradestyle=null, NaicsCode=null, NaicsDesc=null, YearStarted=null, SicDesc=null, DandbCompanyId=null, OperatingHoursId=null}}

System.debug(Trigger.newMap):

null

System.debug(deletedAccounts.size()):

1

So, I would suggest that the problem is probably in the class that is doing the Query as the records are still available for querying in the before delete trigger.