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 

AutoNumbering Opportunities

Hi All,

 

I have a request to auto-number opportunities based on a custom field, business unit. Eg.:

SSA-001

BTE-001

SSA-002

NTE-001

SSA-003 etc

 

I have a cutom object that will hold these numbers and increment based on the business unit in the Opportunity.  This object has no relation (link) to the Opportunity object.  I am currently updating a field on the Opportunity, based on the business unit.  I am currently hitting governor limits when I mass import opportunities.  Please assis urgently.  Herewith my code below:

 

public with sharing class AutoNumberHolder {

public static void display(Opportunity [] oppr){

      Integer m;

 

//List<Opportunity> oppList = new List<Opportunity>();

for( Opportunity o: oppr){

          if(o.Business_Unit__c=='Bateman Engineering (SSA)'){

              AutoNumberHolder__c n =[select id, opportunity__c, Num1__c, num2__c from AutoNumberHolder__c ];

              m=n.Num1__c.intValue();

              m=m+1;

              o.Test_Num__c=m;

              n.Num1__c=m;

 

                  update n;

        }

 

      if(o.Business_Unit__c=='Bateman Engineering (AUS)'){

                  AutoNumberHolder__c n =[select id, opportunity__c, Num1__c, num2__c from AutoNumberHolder__c ];

                  m=n.Num2__c.intValue();

                  m=m+1;

                 o.Test_Num__c=m;

                 n.Num2__c=m;

 

                  update n;

        }

 

 

     }

   }

}

 

Here’s the Trigger:

 

trigger UpdateAutoNumber on Opportunity (before insert,

before update) {

      if(Trigger.isInsert){

 

Opportunity [] oppr =Trigger.new;

                  AutoNumberHolder.display(oppr);

 

}

if(Trigger.isUpdate){  

 

for (Integer i = 0; i < Trigger.new.size(); i++)    {

 

if (Trigger.new[i].business_unit__c != Trigger.old[i].Business_Unit__c){

      Opportunity [] oppr = Trigger.new;

   AutoNumberHolder.display(oppr); 

   }

 

 } 

 

Much appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

public with sharing class AutoNumberHolder {

 

public static void display(Opportunity [] oppr){

      integer ssaCounter = 0;

      integer ausCounter = 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;

 }

 

AutoNumberHolder__c autoNum =[select id, opportunity__c, Num1__c, num2__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 +1 ;

integer ausCtrStarts = autoNum.Num2__c  + 1;

 

autoNum.Num1__c  +=  ssaCounter;

autoNum.Num2__c  += ausCounter;

 

update autoNum;

 

for( Opportunity o: oppr){

          if(o.Business_Unit__c=='Bateman Engineering (SSA)') {

  o.Test_Num__c = ssaCtrStarts;

 ssaCtrStarts +=1; 

 }

 

         else  if(o.Business_Unit__c=='Bateman Engineering (AUS)'){

                o.Test_Num__c = ausCtrStarts;  

 ausCtrStarts += 1;

  }

 

 }

 

  }

 }

 

All Answers

Ritesh AswaneyRitesh Aswaney

public with sharing class AutoNumberHolder {

 

public static void display(Opportunity [] oppr){

      integer ssaCounter = 0;

      integer ausCounter = 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;

 }

 

AutoNumberHolder__c autoNum =[select id, opportunity__c, Num1__c, num2__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 +1 ;

integer ausCtrStarts = autoNum.Num2__c  + 1;

 

autoNum.Num1__c  +=  ssaCounter;

autoNum.Num2__c  += ausCounter;

 

update autoNum;

 

for( Opportunity o: oppr){

          if(o.Business_Unit__c=='Bateman Engineering (SSA)') {

  o.Test_Num__c = ssaCtrStarts;

 ssaCtrStarts +=1; 

 }

 

         else  if(o.Business_Unit__c=='Bateman Engineering (AUS)'){

                o.Test_Num__c = ausCtrStarts;  

 ausCtrStarts += 1;

  }

 

 }

 

  }

 }

 

This was selected as the best answer
Ritesh AswaneyRitesh Aswaney

You were hitting governor limits because of the SOQL Queries and DML Updates in for-loops. These should be avoided at all costs ! :) Spend your limits wisely !

krispkrisp

THANK YOU SO MUCH Ritesh Aswaney, exactly what I needed. And I've noted the governor limits, thanks a lot!!!

Ritesh AswaneyRitesh Aswaney

Cheers mate !

rajafurajafu

Hi,

 

I have one small doubt...

Can u tell me Num1__c and Num2__c are wat data types.