Saturday, June 22, 2013

Autonumber Trigger

This is my third blog and i have a delicious content to post here. I had an requirement to complete like this the client wants an unique Id in the lead section and opportunity section in Salesforce. they might create lead and opportunity manually or either automatically using Rest API transaction. So i planned to create a unique id in lead and opportunity. For example L-38 for lead number 38, O-15 for lead number 15 . I understood what's running in your mind what will happen if the lead is converted ? Even for the converted lead the opportunity number becomes like L-38(even after conversion) so the user can differentiate which is a converted opportunity and which is a manually created opportunity. I came up with the solution of writing trigger to achieve this.

In order to achieve this we have to write two triggers one on lead and one on Opportunity. This is my first trigger on lead

The Main purpose of writing this trigger is to generate a unique number for each record.. So i gave the condition that it has to pick the maximum values among Lead and Opportunity assign a value with maximum number  and concatenate with L and O based on the records.Any way i didn't give condition for concatenation instead i created a formula field instead which will concatenate the number with L and O since the client want the field read only.
Never forget to instantiate the field value else this trigger will wont work so before creating the trigger add default value "0" for the field Lead_Record_ID__c. In order to avoid null pointer exceptions.

The trigger is very simple to understand i queried lead and opportunity and ordered by descending values so that the highest number will be stored in the first index position of the list and the "Limit 1" in the query will store only one value.
 /******  
 ***Created by:Nirmal  
 ***Created Date:22.04.2013  
 ***last modified date:23:04:2013  
 ***Description:This trigger is used to generate a unique ID on Lead  
 ******/  
 trigger AutonumberIDonlead on Lead (before insert) {  
 List leadList=[select Lead_Record_ID__c,Id from Lead where Lead_Record_ID__c !=: null order by Lead_Record_ID__c desc limit 1];  
 List OppList1=[Select Lead_Record_ID__c,Id from Opportunity where Lead_Record_ID__c !=: null order by Lead_Record_ID__c desc limit 1];  
 Decimal maxlead = leadList[0].Lead_Record_ID__c;  
 Decimal maxopp = OppList1[0].Lead_Record_ID__c;  
 for(Lead led :trigger.new){  
 led.Lead_Record_ID__c= Integer.valueOf( ( maxlead > maxopp ? maxlead: maxopp ) +1 ) ;  
 }  
 }  



The next trigger would be the trigger on opportunity similar to the above one and this trigger will work if the isconvertedlead value is false this is good for not generating the autonumber if the opportunity is converted from to a lead..
 /*********************************  
 ***Created by:Nirmal  
 ***Created Date:22.04.2013  
 ***last modified date:23:04:2013  
 ***Description:This trigger is used to generate unique id on opportunity  
 ********************************/  
 trigger Autonumberonopp on Opportunity (after insert) {  
     List<Opportunity> OppList=[SELECT Id,ParentOpportunity__r.Lead_Record_ID__c, Lead_Record_ID__c,Sys_IsConvertedLead__c FROM Opportunity Where ID=:trigger.new[0].id];  
     List <Lead> leadList=[select Lead_Record_ID__c,Id from Lead where Lead_Record_ID__c !=: null order by Lead_Record_ID__c desc limit 1];  
     List <Opportunity> OppList1=[Select Lead_Record_ID__c,Id from Opportunity where Lead_Record_ID__c !=: null order by Lead_Record_ID__c desc limit 1];  
     Decimal maxlead = leadList[0].Lead_Record_ID__c;  
     Decimal maxopp = OppList1[0].Lead_Record_ID__c;  
     for(Opportunity Opp1:OppList){  
         if(Opp1.Lead_Record_ID__c!=null && Opp1.Sys_IsConvertedLead__c==false){  
                    opp1.Lead_Record_ID__c= opp1.ParentOpportunity__c == null ?    Integer.valueOf( ( maxlead > maxopp ? maxlead: maxopp ) +1 ) : opp1.ParentOpportunity__r.Lead_Record_ID__c ;  
       }  
     }  
     update OppList;   
 }  
Both triggers worked fine but the new problem arises from the ashes if the lead is converted what will happen to the existing auto number field. i don't need a new value while converting instead i need the same value in the opportunity section i came up with a different trigger for that.  this trigger will will written on lead i can modify the existing lead trigger to add the condition but in order to make the beginners understand i have created this as a separate trigger.
 trigger autonumberafterconv on Lead (before update) {  
   for(Lead lead:System.Trigger.new) {  
   if (lead.IsConverted) {  
       // Assign the value from the Lead "Status" field to the Contact "Type" field  
       Opportunity opp = [SELECT Id,Contact__c,Lead_Record_ID__c FROM Opportunity WHERE       Id = :lead.ConvertedOpportunityId];  
       opp.Lead_Record_ID__c = lead.Lead_Record_ID__c;  
       update opp;  
     }  
   }  
 }  
In this way we can make the id's unique in lead and opportunity and concatenated with L and O using a  formula field.

2 comments:

dan said...

why not create a custom field and use the field type auto-number? if you want to maintain the lead auto-number, you can create a formula field to copy the value on the lead, then map it to a custom field on the opportunity.

Nirmal said...

Hi Dan,
I need a proper naming convention if L-38 on lead is converted and if opp rec count is o-15 the converted lead should be added as o-16 not L-38. If we create formula field and map it the converted opp will be stored as L-38 in a text field.