Hey guys, today in this post we are going to learn about How to write a apex trigger whenever opportunity is deleted the corresponding account and contact should be deleted uses of Apex trigger in Salesforce.
Real time scenarios:- Write a trigger on Opportunity when Opportunity is deleted the corresponding Account and Contact should be automatic deleted.
Apex Triggers
Apex can be invoked by using triggers. Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.
A trigger is Apex code that executes before or after the following types of operations:-
- insert
- update
- delete
- merge
- upsert
- undelete
To know more details about Apex Triggers, Click Here →
Final Output →
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps ▾
Create Apex Trigger
Step 1:- Create Apex Trigger : OpportunityDeleteTrigger.apxt
From Developer Console >> File >> New >> Apex Trigger
OpportunityDeleteTrigger.apxt [Apex Trigger]
TRIGGER OpportunityDeleteTrigger ON Opportunity (BEFORE DELETE, after DELETE) {
IF(TRIGGER.isBefore){
system.debug('trigger before trigger');
}
ELSE IF(TRIGGER.isAfter){
system.debug('trigger after trigger');
IF(TRIGGER.isDelete){
Map<Id,Opportunity> oppItemMap=NEW Map<Id,Opportunity>();
FOR(Opportunity opp:TRIGGER.old){
oppItemMap.put(opp.AccountId,opp);
}
List<Account> oppRelListDelete=NEW List<Account>();
List<Account> accObjlist=[SELECT Id,Name, Industry, Website, TYPE, Phone, Description FROM Account WHERE Id IN:oppItemMap.keyset()];
FOR(Account acc:accObjlist){
oppRelListDelete.add(acc);
}
IF(!oppRelListDelete.isEmpty() && oppRelListDelete.size()>0){
DELETE oppRelListDelete;
}
}
}
}
Further post that would you like to learn in Salesforce
Does After trigger work on delete?
rigger After Delete Salesforce executes the custom logic after the data is deleted from the Salesforce Database. If you are looking to delete related records, you can make use of Trigger After Delete Salesforce.
What is trigger isInsert?
isInsert. Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API. isUpdate. Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
What is the difference between trigger new and trigger old?
new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers. Trigger. old : Returns a list of the old versions of the sObject records.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |