AFTER DELETE
trigger for the Opportunity object in Salesforce. This trigger will log the details of deleted Opportunity
records into a custom object called OpportunityAudit__c
for auditing purposes.Create the Custom Object
First, create a custom object OpportunityAudit__c
with the following fields:
Opportunity Name
(Text)Opportunity Stage
(Text)Opportunity ID
(Text)Deleted At
(Date/Time)
trigger
OpportunityAfterDelete on Opportunity (after delete) {
// List to
store OpportunityAudit__c records
List<OpportunityAudit__c> opportunityAudits = new
List<OpportunityAudit__c>();
// Loop
through the deleted Opportunity records
for
(Opportunity opp : Trigger.old) {
// Create a new OpportunityAudit__c record for each deleted Opportunity
OpportunityAudit__c auditRecord = new OpportunityAudit__c(
Opportunity_Name__c = opp.Name,
Opportunity_Stage__c = opp.StageName,
Opportunity_ID__c = opp.Id,
Deleted_At__c = System.now() // Timestamp when the
Opportunity was deleted
);
// Add the audit record to the list
opportunityAudits.add(auditRecord);
}
// Insert
the audit records into the OpportunityAudit__c object
if
(!opportunityAudits.isEmpty()) {
insert opportunityAudits;
}
}
I have tried this scenario but it seems that Account__c field will be blank in case on After Delete so we will not get any records. I have tried this scenario. can you please double confirm on the same?
ReplyDeleteThis is not correct
ReplyDeleteThis is not correct. This will work on before delete
ReplyDeleteAbove is incorrect, this will work with "before delete" and not with "after delete"
ReplyDeletehi!.....
ReplyDeletebefore delete allows records to delete
whereas after delete blocks cascading and deletion of records ...
before insert and update .......is used for same kind of object record
after insert and update ..........is used for different kind of object records.