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
Nagesh NagarajaraoNagesh Nagarajarao 

Trigger / Apex to generate a code (autonumber) in the text field

Hi,

I am looking at generating a 12 digit code code as autonumber for a text field.
Output : JOHN-2201-0025
{!LEFT( <object>.OwnerId , 4)} + '-' +day(today())+month(today())+ '-' + <autonumber>}

a. Let me know whether this is possible using triggers (before insert, before update>?
 
Best Answer chosen by Nagesh Nagarajarao
Dinesh MultaniDinesh Multani
Here is the example in reference to Account object. You can change the Object and field API. JFYI We have not used before event as tha auto number field will be popolated in After event. This is partial code you need to check for recursive update.  
 
trigger populateOutputField on Account (after insert) {

    //Creating a map to fetch Owner's first Name from Owner Id.
    map<string,string> userMap=new map<string,string>();
    for(User usr:[select id,firstname from User])
    {
        //Populating the pam where key will be the Owner Id and value will be its respective firstname
        userMap.put(usr.id,usr.firstname);
    }
    
    for(Account acc:Trigger.New)
    {
        date todaysDate=system.today();
        //Calling the Function from Helper Class to populate the Output field
        populateOutputFieldHelper.updateOutputField(todaysDate,acc.AutoNumber__c,userMap.get(acc.ownerid),string.valueof(acc.id));
    }
}

 Helper Class
 
public class populateOutputFieldHelper
{  
    @future(callout=true)
    public static void updateOutputField(date todaysDate,string autoNumber,string ownerName,string Accid)
    {
        if(String.isNotBlank(Accid))
        {
            Account act=[select id,AutoNumber__c,OutputField__c from Account where id=:Accid];
            act.OutputField__c=ownerName+'-'+todaysDate.day()+todaysDate.month()+'-'+autoNumber;
            update act;           
        }
    }
}

 

All Answers

Jainam ContractorJainam Contractor
Hi Nagesh,

Yes this is possible using APEX Trigger on before Insert or before Update operation. Using the same above formula you can update the field in the Trigger.

Other workaround can be using the Formula field itself rather than using APEX trigger.

Please let me know if any concerns.

Thanks,
Jainam Contactor,
Salesforce Consultant
Varasi LLC
www.varasi.com 
Dinesh MultaniDinesh Multani
Here is the example in reference to Account object. You can change the Object and field API. JFYI We have not used before event as tha auto number field will be popolated in After event. This is partial code you need to check for recursive update.  
 
trigger populateOutputField on Account (after insert) {

    //Creating a map to fetch Owner's first Name from Owner Id.
    map<string,string> userMap=new map<string,string>();
    for(User usr:[select id,firstname from User])
    {
        //Populating the pam where key will be the Owner Id and value will be its respective firstname
        userMap.put(usr.id,usr.firstname);
    }
    
    for(Account acc:Trigger.New)
    {
        date todaysDate=system.today();
        //Calling the Function from Helper Class to populate the Output field
        populateOutputFieldHelper.updateOutputField(todaysDate,acc.AutoNumber__c,userMap.get(acc.ownerid),string.valueof(acc.id));
    }
}

 Helper Class
 
public class populateOutputFieldHelper
{  
    @future(callout=true)
    public static void updateOutputField(date todaysDate,string autoNumber,string ownerName,string Accid)
    {
        if(String.isNotBlank(Accid))
        {
            Account act=[select id,AutoNumber__c,OutputField__c from Account where id=:Accid];
            act.OutputField__c=ownerName+'-'+todaysDate.day()+todaysDate.month()+'-'+autoNumber;
            update act;           
        }
    }
}

 
This was selected as the best answer
Nagesh NagarajaraoNagesh Nagarajarao
Thanks Jainam and Dinesh.
I have tried both methods, (a) setting default value and (b) Apex trigger and it works