function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
krispkrisp 

Triggers Firing more than once

Hi All, I have a class and trigger that assign numbers to Opportunities depending on the business unit (Custom Field).  On a mass update (Dataloadre) I get duplicate values for this number, meaning two records will receive one number.  Below is my Trigger, thereafter my class.

 

Thanks in Advance.

 

trigger UpdateAutoNumber on Opportunity (before insert, before update) {
   
List <Opportunity> updates = new List <Opportunity>();   
if(Trigger.isInsert){
    Opportunity [] oppr =Trigger.new;
        AutoNumberHolder.display(oppr);
    }
   
   
   
if(Trigger.isUpdate){
    if (AutoNumberHolder.firstRun == true) {
     for (Opportunity oppr: Trigger.new){
        
          
                if(oppr.business_unit__c != Trigger.oldMap.get(oppr.Id).business_unit__c||
                   oppr.Product_Type__c != Trigger.oldMap.get(oppr.Id).Product_Type__c ){
                     updates.add(oppr);
                      
       
               }

         }
   

    }
    AutoNumberHolder.display(updates);
    AutoNumberHolder.firstRun = false;
  }
}

 

Class

 

public with sharing class AutoNumberHolder {
public static boolean firstRun = true;

public static void display(Opportunity [] oppr){

      integer ssaCounter = 0;
      integer ausCounter = 0;
      integer cisCounter = 0;
      integer indCounter = 0;
      integer bgtCounter = 0;
      integer betCounter = 0;
      integer delCounter = 0;
      integer bbaCounter = 0;
      integer baaCounter = 0;
      integer balCounter = 0;

for( Opportunity o: oppr){
          if(o.Business_Unit__c=='Bateman Engineering (SSA)')
                  ssaCounter += 1;

         else  if(o.Business_Unit__c=='Bateman Engineering (AUS)')
                  ausCounter += 1;
                 
         else if(o.Business_Unit__c=='Bateman Engineering (CIS)')
                  cisCounter += 1;

         else  if(o.Business_Unit__c=='Bateman Engineering (IND)')
                  indCounter += 1;
                 
         else if(o.Business_Unit__c=='Bateman Global Technology (BGT)')
                  bgtCounter += 1;

  
      
       else  if (o.Business_Unit__c=='Bateman Engineered Technologies (BET)')
                 betCounter = betCounter + 1;
                
         else if(o.Business_Unit__c=='Delkor Global (DEL)')
                  delCounter += 1;

         else  if(o.Business_Unit__c=='Bateman Beijing Axis (BBA)')
                  bbaCounter += 1;
                 
         else if(o.Business_Unit__c=='Bateman Africa (BAA)')
                  baaCounter += 1;

         else  if(o.Business_Unit__c=='Bateman Litwin (BAL)')
                  balCounter += 1;
 }

AutoNumberHolder__c autoNum =[select id, Num1__c, Num2__c, Num3__c, Num4__c, Num5__c, Num6__c, Num7__c, Num8__c, Num9__c, Num10__c
                               from AutoNumberHolder__c LIMIT 1];

//assuming its an integer field - we are reserving numbers from the counter
//for the opportunities we have to number

integer ssaCtrStarts = autoNum.Num1__c.intValue() + 1;
integer ausCtrStarts = autoNum.Num2__c.intValue() + 1;
integer cisCtrStarts = autoNum.Num3__c.intValue() + 1;
integer indCtrStarts = autoNum.Num4__c.intValue()  + 1;
integer bgtCtrStarts = autoNum.Num5__c.intValue() + 1 ;

integer betCtrStarts = autoNum.Num6__c.intValue()  + 1;
integer delCtrStarts = autoNum.Num7__c.intValue() + 1;
integer bbaCtrStarts = autoNum.Num8__c.intValue()  + 1;
integer baaCtrStarts = autoNum.Num9__c.intValue()  + 1;
integer balCtrStarts = autoNum.Num10__c.intValue() + 1;

autoNum.Num1__c  +=  ssaCounter;
autoNum.Num2__c  += ausCounter;
autoNum.Num3__c  +=  cisCounter;
autoNum.Num4__c  += indCounter;
autoNum.Num5__c  +=  bgtCounter;

autoNum.Num6__c  += betCounter;
autoNum.Num7__c  +=  delCounter;
autoNum.Num8__c  += bbaCounter;
autoNum.Num9__c  += baaCounter;
autoNum.Num10__c  += balCounter;


update autoNum;

for( Opportunity o: oppr){
          if(o.Business_Unit__c=='Bateman Engineering (SSA)') {

               o.Enquiry_Number2__c = ssaCtrStarts.format();
               ssaCtrStarts +=1;
 }
         else if(o.Business_Unit__c=='Bateman Engineering (AUS)'){
                o.Enquiry_Number2__c = ausCtrStarts.format(); 
                ausCtrStarts += 1;
  }
         else  if(o.Business_Unit__c=='Bateman Engineering (CIS)'){
                o.Enquiry_Number2__c = cisCtrStarts.format(); 
                cisCtrStarts += 1;
 }
         else  if(o.Business_Unit__c=='Bateman Engineering (IND)'){
                o.Enquiry_Number2__c = indCtrStarts.format(); 
                indCtrStarts += 1;
 }
         else  if(o.Business_Unit__c=='Bateman Global Technology (BGT)'){

                o.Enquiry_Number2__c = bgtCtrStarts.format(); 
                bgtCtrStarts += 1;
 }
        
       
else  if(o.Business_Unit__c=='Bateman Engineered Technologies (BET)'){

                o.Enquiry_Number2__c = betCtrStarts.format(); 
                betCtrStarts += 1;
                system.debug('bmc Controller '+ betCtrStarts);
                system.debug('Enquiry Number '+ o.Enquiry_Number2__c);
 }
 

         else  if(o.Business_Unit__c=='Delkor Global (DEL)'){
                o.Enquiry_Number2__c = delCtrStarts.format(); 
                delCtrStarts += 1;
 }
         else  if(o.Business_Unit__c=='Bateman Beijing Axis (BBA)'){
                o.Enquiry_Number2__c = bbaCtrStarts.format(); 
                bbaCtrStarts += 1;
  }
         else  if(o.Business_Unit__c=='Bateman Africa (BAA)'){
                o.Enquiry_Number2__c = bbaCtrStarts.format(); 
                baaCtrStarts += 1;
 }
         else  if(o.Business_Unit__c=='Bateman Litwin (BAL)'){
                o.Enquiry_Number2__c = delCtrStarts.format(); 
                balCtrStarts += 1;
  }
 
}
 }
}

Best Answer chosen by Admin (Salesforce Developers) 
pankaj.raijadepankaj.raijade

 Hi krisp,

 

Its good that your issue is resolved.

But actual problem was not because of batch size. 

 

It was because

when you update record

1. trigger will fire once

2. then it will fire the workflow and work flow will update the record again.

3. and new on update the trigger will fire again.

 

so it used to fire trigger two time.

 

Regards,

Pankaj Raijade.


All Answers

pankaj.raijadepankaj.raijade

Do you have any workflow rule of "after trigger " on opportunity?

krispkrisp

I have one workflows that is active on the Opportunity, I need this as it also assists with the unique number assignment

krispkrisp

Odd thing is when I tried importing 400+ records it duplicated, I cut that to batches of 50 and all worked fine.

 

Thanks.

pankaj.raijadepankaj.raijade

 Hi krisp,

 

Its good that your issue is resolved.

But actual problem was not because of batch size. 

 

It was because

when you update record

1. trigger will fire once

2. then it will fire the workflow and work flow will update the record again.

3. and new on update the trigger will fire again.

 

so it used to fire trigger two time.

 

Regards,

Pankaj Raijade.


This was selected as the best answer
krispkrisp

Hi pankaj

 

Thanks for your feedback and you're right.  I read up on this and i was just controlling it wrong (structure of code).

 

Thanks a lot!