Write a trigger whenever Opportunity is deleted the corresponding Account and Contact should be deleted uses of Apex trigger in Salesforce | Write a trigger on Opportunity to delete corresponding Account and contact of opportunity using Apex trigger in Salesforce

4,192 views


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.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

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 →

trigger to deleted relted list records of opportunity -- w3web.net

 

You can download file directly from github by Click Here.

 

Other related post that would you like to learn in Salesforce

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

 

  • 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]

  1.    TRIGGER OpportunityDeleteTrigger ON Opportunity (BEFORE DELETE, after DELETE) {
  2.  
  3.     IF(TRIGGER.isBefore){
  4.         system.debug('trigger before trigger');          
  5.     }
  6.     ELSE IF(TRIGGER.isAfter){
  7.         system.debug('trigger after trigger'); 
  8.         IF(TRIGGER.isDelete){
  9.             Map<Id,Opportunity> oppItemMap=NEW Map<Id,Opportunity>();
  10.             FOR(Opportunity opp:TRIGGER.old){
  11.                 oppItemMap.put(opp.AccountId,opp);
  12.  
  13.             }            
  14.             List<Account> oppRelListDelete=NEW List<Account>();
  15.             List<Account> accObjlist=[SELECT Id,Name, Industry, Website, TYPE, Phone, Description FROM Account WHERE Id IN:oppItemMap.keyset()];
  16.             FOR(Account acc:accObjlist){
  17.                 oppRelListDelete.add(acc);
  18.  
  19.             }
  20.  
  21.             IF(!oppRelListDelete.isEmpty() && oppRelListDelete.size()>0){
  22.  
  23.                 DELETE  oppRelListDelete;
  24.             }
  25.  
  26.         }
  27.     }
  28.  
  29. }

 
trigger to deleted relted list records of opportunity -- w3web.net
 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment