+ Start a Discussion
shoba shobashoba shoba 

How to move the Trigger in Apex method

Any anyone give me an idea how to move the trigger code to Apex class. My code is
trigger InsertingBU on Opportunity (after insert,after update) 
{
    Set<Id> accountIds = new Set<Id>();
    for(Opportunity o : Trigger.New)
        accountIds.add(o.AccountId);
        
    List<Business_Unit_Company__c> buCompaniesToBeUpserted = new List<Business_Unit_Company__c>();   

    Map<Id,Business_Unit_Company__c> existingBuCompanies = new Map<Id,Business_unit_Company__c>();
    
    for(Business_Unit_Company__c bu : [Select Business_Unit__c,Company__c,Stage__c from Business_Unit_Company__c where Company__c IN: accountIds])
      existingBuCompanies.put(bu.Business_Unit__c, bu);
      
    for(Opportunity o : Trigger.New) 
    {
      if(o.Opportunity_Business_Unit__c != null && existingBuCompanies.get(o.Opportunity_Business_Unit__c) != null)
      {
        Business_Unit_Company__c relatedBuCompany = existingBuCompanies.get(o.Opportunity_Business_Unit__c);                              
        if(relatedBuCompany.Stage__c != 'Closed Won')
        {
          if(o.StageName == 'Closed Won')
          {
            relatedBuCompany.Stage__c = 'Closed Won';
            buCompaniesToBeUpserted.add(relatedBuCompany);
          }
          else if(relatedBuCompany.Stage__c != 'Closed Lost' && o.StageName == 'Closed Lost')
          {
            relatedBuCompany.Stage__c = 'Closed Lost';
            buCompaniesToBeUpserted.add(relatedBuCompany);
          }
          else if(relatedBuCompany.Stage__c != 'Open')
          {
            relatedBuCompany.Stage__c = 'Open';
            buCompaniesToBeUpserted.add(relatedBuCompany);
          }
        }        
      } 
      else if(o.Opportunity_Business_Unit__c != null)
      {
        Business_Unit_Company__c newBuCompany = new Business_Unit_Company__c();
        newBuCompany.Company__c = o.AccountId;
        newBuCompany.Business_Unit__c = o.Opportunity_Business_Unit__c;
        if(o.StageName.contains('Closed'))
        {
          newBuCompany.stage__c = o.StageName ;
        }
        else
        {
          newBuCompany.stage__c ='Open';
        }
        buCompaniesToBeUpserted.add(newBuCompany);
      }   
    }
    if(!buCompaniesToBeUpserted.IsEmpty())
      upsert buCompaniesToBeUpserted;
}

 
Sudhir ChowdarySudhir Chowdary
Hi,

Use following way to mode your trigger code to apex classs:
trigger InsertingBU on Opportunity (after insert,after update) 
{
	if(Trigger.isAfter && Trigger.isInsert) {
		opportunityHelper.oppHelperMethod(Trigger.New);
	}
	
	if(Trigger.isAfter && Trigger.isUpdate) {
		opportunityHelper.oppHelperMethod(Trigger.New);
	}
}

public class opportunityHelper{

 public static void oppHelperMethod( List<Opportunity> lstOpp){
	Set<Id> accountIds = new Set<Id>();
    for(Opportunity o : lstOpp)
        accountIds.add(o.AccountId);
        
    List<Business_Unit_Company__c> buCompaniesToBeUpserted = new List<Business_Unit_Company__c>();   

    Map<Id,Business_Unit_Company__c> existingBuCompanies = new Map<Id,Business_unit_Company__c>();
    
    for(Business_Unit_Company__c bu : [Select Business_Unit__c,Company__c,Stage__c from Business_Unit_Company__c where Company__c IN: accountIds])
      existingBuCompanies.put(bu.Business_Unit__c, bu);
      
    for(Opportunity o : lstOpp) 
    {
      if(o.Opportunity_Business_Unit__c != null && existingBuCompanies.get(o.Opportunity_Business_Unit__c) != null)
      {
        Business_Unit_Company__c relatedBuCompany = existingBuCompanies.get(o.Opportunity_Business_Unit__c);                              
        if(relatedBuCompany.Stage__c != 'Closed Won')
        {
          if(o.StageName == 'Closed Won')
          {
            relatedBuCompany.Stage__c = 'Closed Won';
            buCompaniesToBeUpserted.add(relatedBuCompany);
          }
          else if(relatedBuCompany.Stage__c != 'Closed Lost' && o.StageName == 'Closed Lost')
          {
            relatedBuCompany.Stage__c = 'Closed Lost';
            buCompaniesToBeUpserted.add(relatedBuCompany);
          }
          else if(relatedBuCompany.Stage__c != 'Open')
          {
            relatedBuCompany.Stage__c = 'Open';
            buCompaniesToBeUpserted.add(relatedBuCompany);
          }
        }        
      } 
      else if(o.Opportunity_Business_Unit__c != null)
      {
        Business_Unit_Company__c newBuCompany = new Business_Unit_Company__c();
        newBuCompany.Company__c = o.AccountId;
        newBuCompany.Business_Unit__c = o.Opportunity_Business_Unit__c;
        if(o.StageName.contains('Closed'))
        {
          newBuCompany.stage__c = o.StageName ;
        }
        else
        {
          newBuCompany.stage__c ='Open';
        }
        buCompaniesToBeUpserted.add(newBuCompany);
      }   
    }
    if(!buCompaniesToBeUpserted.IsEmpty())
      upsert buCompaniesToBeUpserted;
 }
 
}