Write a apex trigger on Account, whenever Active is “Yes”, before save the record we will be check the child object (Opportunity) StageName should not be “Closed Lost”, if any of record of child record get “Closed Lost”, it will be through an error.

264 views

Hey guys, today in this post we are going to learn about How to write a apex trigger on Account, whenever Active is “Yes”, before save the record we will be check the child object (Opportunity) StageName should not be “Closed Lost”, if any of record of child record get “Closed Lost”, it will be through an error.

 

Final Output →

apex trigger on account to check opportunity stagename --  w3web.net
 

Other related post that would you like to learn in Salesforce

 

Create Apex Trigger →

Step 1:- Create Apex Controller : AccountTrigger.apxt

SFDX:Create Apex Class >> New >> AccountTrigger.apxt

AccountTrigger.apxt [Apex Trigger]

  1.   TRIGGER AccountTrigger ON Account (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE, BEFORE DELETE,after DELETE) {
  2.  
  3.      // WRITE a apex TRIGGER ON Account, whenever Active IS "Yes", befrore save the record we will be CHECK the child object (Opportunity) StageName should NOT be "Closed Lost"
  4.  
  5.      // IF any OF record OF child recod GET "Closed Losed", it will be through an error.
  6.      // 
  7.  
  8.     IF(TRIGGER.isBefore) {
  9.         IF(TRIGGER.isUpdate){
  10.             accountTriggerHandler.changeActiveHandler(TRIGGER.new); 
  11.         }
  12.     }ELSE IF(TRIGGER.isAfter){
  13.  
  14.     }
  15.  
  16. }

 

Create Apex Class Controller →

Step 2:- Create Apex Controller : accountTriggerHandler.apxc

SFDX:Create Apex Class >> New >> accountTriggerHandler.apxc

accountTriggerHandler.apxc [Apex Class]

  1.  public class accountTriggerHandler {
  2.  
  3.    // WRITE a apex TRIGGER ON Account, whenever Active IS "Yes", befrore save the record we will be CHECK the child object (Opportunity) StageName should NOT be "Closed Lost"
  4.  
  5.   // IF any OF record OF child recod GET "Closed Losed", it will be through an error.
  6.   // 
  7.  
  8.     public static void changeActiveHandler(List<Account> accList){
  9.         Map<Id, List<Opportunity>> optMapList = NEW Map<Id, List<Opportunity>>();
  10.         set<Id> setId = NEW set<Id>();
  11.         FOR(Account acc1:accList){
  12.             setId.add(acc1.Id);
  13.         }
  14.  
  15.         List<Account> accObjList = [SELECT Id, Name, Active__c,(SELECT Id, Name, StageName, AccountId FROM Opportunities) FROM Account WHERE Id IN:setId];
  16.         FOR(Account acc2:accObjList){
  17.             optMapList.put(acc2.Id, acc2.Opportunities);
  18.         }
  19.  
  20.         Set<String> optStageStr = NEW Set<String>();
  21.         List<Opportunity> optObjList = [SELECT Id, Name, StageName, AccountId FROM Opportunity WHERE AccountId=:optMapList.keySet()];
  22.         FOR(Opportunity opp:optObjList){
  23.             optStageStr.add(opp.StageName);
  24.         }
  25.  
  26.         FOR(Account acc3:accList){
  27.             IF(acc3.Active__c=='Yes' && (optStageStr.contains('Closed Lost'))){
  28.                 acc3.addError('Do not allow update record, if related Opportunity has Closed Lost');
  29.             }
  30.         } 
  31.  
  32.     }
  33.  
  34.  
  35.  
  36. }

 

 

Further post that would you like to learn in Salesforce

 

FAQ (Frequently Asked Questions)

Can we make callout from trigger?

Callout from triggers are currently not supported. You can invoke callouts from triggers by encapsulating the callouts in @future methods.

What are the events in Apex trigger?

In Salesforce, Apex trigger events are the events that occur before and after any DML operation. DML operations are Insert, Update, Delete, Undelete. Events are in either a BEFORE or AFTER category.

Why we Cannot make a callout from a trigger?

In certain scenarios, we need to make the callout from the trigger to call an external webservice however we are not able to do so as it gives the below mentioned error: Callout from triggers are currently not supported. You can invoke callouts from triggers by encapsulating the callouts in @future methods.

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

 
 


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