+ Start a Discussion
SFDC@ErrorSFDC@Error 

How to merge two trigger in same object

Hi All

How can i merge two triiger in a single trigger on a perticular object.
I have created two trigger but not working at a time both trigger(one is working only ).so how to merge this two trigger on a single trigger.
 
trigger CreateOpportunity on Boutique__c (after insert , after update) {
    
    List<Opportunity> opps = new List<Opportunity>();

    for (Boutique__c boutique : Trigger.new) {
        
        if((boutique.Visit_Type__c == 'Browsing' || boutique.Visit_Type__c == 'Enquiry') 
            && boutique.LeadType__c== 'Regular'
            && String.isNotBlank(boutique.Opportunity_Name__c)) {

            Opportunity childOpp                        = new Opportunity();
                        childOpp.Boutique__c            = boutique.Id;
                        childOpp.Name                   = boutique.Opportunity_Name__c;
                        childOpp.StageName              = 'Invitation';
                        childOpp.CloseDate              = System.Today()+150;
                        childOpp.Contact__c             = boutique.Customer_Name__c;
                        childOpp.Amount                 = boutique.Amount__c;
                        childOpp.AccountId              = '0012800001J3sMRAAZ';
                        childOpp.Assisted_By__c         = boutique.Assisted_By_Look_Up__c;
                        childOpp.Budget_Range__c        = boutique.Budget_Range__c;
                        childOpp.Stone__c               = boutique.Stone__c;
                        childOpp.Shadow_Assistance__c   = boutique.Shadow_Assistance_Look_Up__c;
                        childOpp.Visit_Type__c          = boutique.Visit_Type__c;
                        //childOpp.Enquiry_Details__c     = boutique.Enquiry_Details__c;
                        childOpp.Enquiry_Details1__c     = boutique.Enquiry_Details__c;
                        childOpp.FullName__c            = boutique.Customer_Name__c;

            opps.add(childOpp);    
        } 
    }  
        
    if (!opps.isEmpty()) 
        insert opps;
}
 
Trigger ContactCountTrigger on Boutique__c(After insert,After Delete,After Undelete)
{
  Set<Id> setAccountIds = new Set<Id>();
  
  //Whenever your working with After Undelete operation you can access data through 
  //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
  if(Trigger.isInsert || Trigger.isUndelete)
  {
   for(Boutique__c con : Trigger.new)
   {
    setAccountIds.add(con.Customer_Name__c);
   }
  }
  
  if(Trigger.isDelete)
  {
   //if you use Trigger.new below in place of Trigger.old you will end up with 
   //System.NullPointerException:Attempt to de-reference a null object
   for(Boutique__c con : Trigger.old) 
   {
    setAccountIds.add(con.Customer_Name__c);
   }
  }
  
 List<Contact> listAccs = [Select id,name,Total_Visit__c,(Select id from Boutiques__r) from Contact where Id in : setAccountIds];
  for(Contact acc :listAccs)
  {
   acc.Total_Visit__c = acc.Boutiques__r.size();
  }
  update listAccs;
}

 
Suraj TripathiSuraj Tripathi
Hi,
Please try this piece of code .
 
Trigger ContactCountTrigger on Boutique__c(After insert,After Delete, After Undelet , after update)
{
 if(Trigger.IsAfter & (Trigger.isDelete || Trigger.Undelete || Trigger.isInsert)){
	 Set<Id> setAccountIds = new Set<Id>();
  
  //Whenever your working with After Undelete operation you can access data through 
  //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
  if(Trigger.isInsert || Trigger.isUndelete)
  {
   for(Boutique__c con : Trigger.new)
   {
    setAccountIds.add(con.Customer_Name__c);
   }
  }
  
  if(Trigger.isDelete)
  {
   //if you use Trigger.new below in place of Trigger.old you will end up with 
   //System.NullPointerException:Attempt to de-reference a null object
   for(Boutique__c con : Trigger.old) 
   {
    setAccountIds.add(con.Customer_Name__c);
   }
  }
  
 List<Contact> listAccs = [Select id,name,Total_Visit__c,(Select id from Boutiques__r) from Contact where Id in : setAccountIds];
  for(Contact acc :listAccs)
  {
   acc.Total_Visit__c = acc.Boutiques__r.size();
  }
  update listAccs;
}
	
	if(Trigger.isAfter & (Trigger.isUpdate || Trigger.isInsert)){
			 List<Opportunity> opps = new List<Opportunity>();

    for (Boutique__c boutique : Trigger.new) {
        
        if((boutique.Visit_Type__c == 'Browsing' || boutique.Visit_Type__c == 'Enquiry') 
            && boutique.LeadType__c== 'Regular'
            && String.isNotBlank(boutique.Opportunity_Name__c)) {

            Opportunity childOpp                        = new Opportunity();
                        childOpp.Boutique__c            = boutique.Id;
                        childOpp.Name                   = boutique.Opportunity_Name__c;
                        childOpp.StageName              = 'Invitation';
                        childOpp.CloseDate              = System.Today()+150;
                        childOpp.Contact__c             = boutique.Customer_Name__c;
                        childOpp.Amount                 = boutique.Amount__c;
                        childOpp.AccountId              = '0012800001J3sMRAAZ';
                        childOpp.Assisted_By__c         = boutique.Assisted_By_Look_Up__c;
                        childOpp.Budget_Range__c        = boutique.Budget_Range__c;
                        childOpp.Stone__c               = boutique.Stone__c;
                        childOpp.Shadow_Assistance__c   = boutique.Shadow_Assistance_Look_Up__c;
                        childOpp.Visit_Type__c          = boutique.Visit_Type__c;
                        //childOpp.Enquiry_Details__c     = boutique.Enquiry_Details__c;
                        childOpp.Enquiry_Details1__c     = boutique.Enquiry_Details__c;
                        childOpp.FullName__c            = boutique.Customer_Name__c;

            opps.add(childOpp);    
        } 
    }  
        
    if (!opps.isEmpty()) 
        insert opps;
	}
 
}

Hope its help you

Regards,
Suraj​
Amol ChAmol Ch
As a best practice, always create one trigger per object.

You can use below code:
Trigger ContactCountTrigger on Boutique__c(After insert,after update,After Delete,After Undelete)
{
  Set<Id> setAccountIds = new Set<Id>();
  List<Opportunity> opps = new List<Opportunity>();
  //Whenever your working with After Undelete operation you can access data through 
  //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
   if(Trigger.isInsert || Trigger.isUpdate)
   {
	 for (Boutique__c boutique : Trigger.new) {
        
        if((boutique.Visit_Type__c == 'Browsing' || boutique.Visit_Type__c == 'Enquiry') 
            && boutique.LeadType__c== 'Regular'
            && String.isNotBlank(boutique.Opportunity_Name__c)) {

            Opportunity childOpp                        = new Opportunity();
                        childOpp.Boutique__c            = boutique.Id;
                        childOpp.Name                   = boutique.Opportunity_Name__c;
                        childOpp.StageName              = 'Invitation';
                        childOpp.CloseDate              = System.Today()+150;
                        childOpp.Contact__c             = boutique.Customer_Name__c;
                        childOpp.Amount                 = boutique.Amount__c;
                        childOpp.AccountId              = '0012800001J3sMRAAZ';
                        childOpp.Assisted_By__c         = boutique.Assisted_By_Look_Up__c;
                        childOpp.Budget_Range__c        = boutique.Budget_Range__c;
                        childOpp.Stone__c               = boutique.Stone__c;
                        childOpp.Shadow_Assistance__c   = boutique.Shadow_Assistance_Look_Up__c;
                        childOpp.Visit_Type__c          = boutique.Visit_Type__c;
                        //childOpp.Enquiry_Details__c     = boutique.Enquiry_Details__c;
                        childOpp.Enquiry_Details1__c     = boutique.Enquiry_Details__c;
                        childOpp.FullName__c            = boutique.Customer_Name__c;

            opps.add(childOpp);    
        }
			setAccountIds.add(boutique.Customer_Name__c);
    }  
   }
  
  if(Trigger.isUndelete)
  {
   for(Boutique__c con : Trigger.new)
   {
    setAccountIds.add(con.Customer_Name__c);
   }
  }
  
  if(Trigger.isDelete)
  {
   //if you use Trigger.new below in place of Trigger.old you will end up with 
   //System.NullPointerException:Attempt to de-reference a null object
   for(Boutique__c con : Trigger.old) 
   {
    setAccountIds.add(con.Customer_Name__c);
   }
  }
  
  if(setAccountIds.size()>0)
  {
	 List<Contact> listAccs = [Select id,name,Total_Visit__c,(Select id from Boutiques__r) from Contact where Id in : setAccountIds];
	  if(listAccs.size()>0)
	  {
		  for(Contact acc :listAccs)
		  {
		   acc.Total_Visit__c = acc.Boutiques__r.size();
		  }
		  update listAccs;
	  }
  }
  
  if (opps.size()>0) 
        insert opps;
}