Apex Triggers:
1. Before insert:
Update account description whenever Account is created.
Trigger AccountTrigger on Account(before insert) {
for(Account acc: Trigger.new) {
acc.Description = 'Test Description';
}
}
Call Apex Method through Trigger:
Trigger AccountTrigger on Account(before insert){
if(Trigger.IsInsert){
if(Trigger.isBefore){
AccountTriggerHandler.beforeInsert(Trigger.new);
AccountTriggerHandler.updateRating(Trigger.new);
}
else if(Trigger.isAfter){
AccountTriggerHandler.createOpportunity(Trigger.new);
}
}
else if(Trigger.isUpdate){
if(Trigger.isBefore){
AccountTriggerHandler.updateAccountPhone(Trigger.new, Trigger.oldMap);
}
else if(Trigger.isAfter){
AccountTriggerHandler.updateAccountPhoneOnOpportunties(Trigger.new, Trigger.oldMap);
}
}
}
public class AccountTriggerHandler {
public static void beforeInsert(List<Account> newAccounts){
for(Account acc: newAccounts){
acc.Description = 'Test Description';
}
}
}
2. Before Insert and Before Update:
Before Insert: If Industry is not null and having value as 'Media' then populate Rating as Hot.
After Insert: Create related Opportunity when Account is created.
public class AccountTriggerHandler {
public static void updateRating(List<Account> accListNew){
for(Account acc: accListNew){
if(acc.Industry != null && acc.Industry == 'Media'){
acc.Rating = 'Hot';
}
else {
acc.Rating = 'Normal';
}
}
}
public static void createOpportunity(List<Account> accListNew){
List<Opportunity> listOpp = new List<Opportunity>();
for(Account acc: accListNew){
Opportunity opp = new Opportunity();
opp.Name = acc.Name;
opp.StageName = 'New';
opp.AccountId = acc.Id;
opp.ClosedDate = System.Today()+30;
listOpp.add(opp);
}
if(listOpp.size()> 0 && !listOpp.isEmpty){
try{
Database.Insert(listOpp, false);
}
catch(Exception e){
System.debug('Exception occurred' +e);
}
}
}
}
3. Before Update, After Update:
Before Update: If Account Phone is updated, then put update message in Description.
After Update: If Account Phone is updated, then populate that on all related Opportunities.
public class AccountTriggerHandler {
public static void updateAccountPhone(List<Account> accListNew, Map<Id, Account> oldMapAccount){
for(Account acc: accListNew){
if(oldMapAccount != null && acc.Phone != oldMapAccount.get(acc.Id).Phone){
acc.Description = 'Account Phone number has been modified';
}
}
}
public static void updateAccountPhoneOnOpportunties(List<Account> newAccountsList, Map<Id, Account> oldMapAccount){
Map<Id, Account> accountMap = new Map<Id, Account>();
for(Account acc: newAccountsList){
if(oldMapAccount != null && acc.Phone != oldMapAccount.get(acc.Id).Phone){
accountMap.put(acc.Id);
}
}
List<Opportunity> opportunitiesTobeUpdated = new List<Opportunity>();
for(Opportunity opp: [SELECT Phone from Opportunity WHERE AccountId in: accountMap.keySet()]){
Opportunity opp1 = new Opportunity();
if(accountMap.containsKey(opp.AccountId)){
opp1.Id = opp.Id;
opp1.Phone = accountMap.get(opp.AccountId).Phone;
opportunitiesTobeUpdated.add(opp1);
}
}
if(!opportunitiesTobeUpdated.isEmpty && opportunitiesTobeUpdated.size() > 0 ){
try{
Database.update(opportunitiesTobeUpdated, false);
}
catch(Exception e){
System.debug('Exception occurred '+e.getMessage());
}
}
}
}
4. Before Delete, After Delete:
Before Delete: Employee record cannot be deleted if Active is true.
public class EmployeeTriggerHandler {
public static void checkEmployeeStatus(List<Employee__c> oldEmployeeList){
for(Employee__c emp: oldEmployeeList){
if(emp.Is_Active__c == TRUE){
emp.addError('Active Employee cannot be deleted !');
}
}
}
}
After Delete: When Employee record is deleted then update Left Employee Count on Account.
Set<Id> accIds = new Set<Id>();
List<Account> accToBeUpdated = new List<Account>();
Map<Id, Account> accIdToAccMap;
List<Employee__c> empList = new List<Employee__c>();
Map<Id, Decimal> accIdToTotalCount = new Map<Id, Decimal>();
for(Employee__c emp: oldList){
if(emp.Account__c != null){
accIds.add(emp.Account__c);
empList.add(emp);
}
}
if(!accIds.isEmpty()){
accIdToAccMap = new Map<Id, Account>([SELECT Id, Left_Employee_Count__c FROM Account WHERE Id IN: accIds]);
}
if(!empList.isEmpty()){
for(Employee__c emp: empList){
if(accIdToAccMap.containsKey(emp.Account__c)){
if(accIdToTotalCount.containsKey(emp.Account__c)){
Decimal count = accIdToTotalCount.get(emp.Account__c)+1;
accIdToTotalCount.put(emp.Account__c, count);
}
else {
accIdToTotalCount.put(emp.Account__c, accIdToAccMap.get(emp.Account__c).Left_Employee_Count__c +1);
}
}
}
}
for(Id accId: accIdToTotalCount.KeySet()){
Account acc = new Account();
acc.Id = accId;
acc.Left_Employee_Count__c = accIdToTotalCount.get(accId);
accToBeUpdated.add(acc);
}
if(!accToBeUpdated.isEmpty()){
update accToBeUpdated;
}
}
No comments:
Post a Comment