You need to sign in to do that
Don't have an account?
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.
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
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;
}
}
}
}
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 !
THANK YOU SO MUCH Ritesh Aswaney, exactly what I needed. And I've noted the governor limits, thanks a lot!!!
Cheers mate !
Hi,
I have one small doubt...
Can u tell me Num1__c and Num2__c are wat data types.