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 β
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]
TRIGGER AccountTrigger ON Account (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE, BEFORE DELETE,after DELETE) {
// 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"
// IF any OF record OF child recod GET "Closed Losed", it will be through an error.
//
IF(TRIGGER.isBefore) {
IF(TRIGGER.isUpdate){
accountTriggerHandler.changeActiveHandler(TRIGGER.new);
}
}ELSE IF(TRIGGER.isAfter){
}
}
Create Apex Class Controller β
Step 2:- Create Apex Controller : accountTriggerHandler.apxc
SFDX:Create Apex Class >> New >> accountTriggerHandler.apxc
accountTriggerHandler.apxc [Apex Class]
public class accountTriggerHandler {
// 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"
// IF any OF record OF child recod GET "Closed Losed", it will be through an error.
//
public static void changeActiveHandler(List<Account> accList){
Map<Id, List<Opportunity>> optMapList = NEW Map<Id, List<Opportunity>>();
set<Id> setId = NEW set<Id>();
FOR(Account acc1:accList){
setId.add(acc1.Id);
}
List<Account> accObjList = [SELECT Id, Name, Active__c,(SELECT Id, Name, StageName, AccountId FROM Opportunities) FROM Account WHERE Id IN:setId];
FOR(Account acc2:accObjList){
optMapList.put(acc2.Id, acc2.Opportunities);
}
Set<String> optStageStr = NEW Set<String>();
List<Opportunity> optObjList = [SELECT Id, Name, StageName, AccountId FROM Opportunity WHERE AccountId=:optMapList.keySet()];
FOR(Opportunity opp:optObjList){
optStageStr.add(opp.StageName);
}
FOR(Account acc3:accList){
IF(acc3.Active__c=='Yes' && (optStageStr.contains('Closed Lost'))){
acc3.addError('Do not allow update record, if related Opportunity has Closed Lost');
}
}
}
}
Further post that would you like to learn in Salesforce
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 |