I am new to Salesforce and the bulkified logic of Apex development and I am trying to wrap my head around a task I have to accomplish.

I am writing a trigger logic , where, for each new inserted Account, I need to generate a value for a custom String field that will be formed/concatenated from two other custom fields that, a priori, will hold the values that the user is going to insert when he's going to create a new Account object.

The custom field is called Codigo_Socio__c and the value that needs to be generated has this form: BCN0001

The first 3 letters represent his\her city of commercial activity, a value that I can obtain from the Account's custom field Ciudad_direccion_comercial__c where a city is saved in this form: Barcelona-BCN.

The second part of the resulting String is a sequential number(that I have declared in my trigger as a public variable named accountCounter, that will keep track of the registers inserted. Below is an example code that more or less represents what I'm trying to achieve:


/*we format the counter that will keep track of all the inserted   Accounts*/

public DecimalFormat formatter = new DecimalFormat("0000");
public int accountCounter = 1 ;
public String counterFormatted;

public void generateCodigoSocioSequence(List<Account> newList) {

    String ciudad;
    String[] parts;

    for(Account acc : newList){

        /*split the name of the city 
          so we can obtain the second part of the string, 
         e.g 'BCN'*/

        ciudad = acc.Ciudad_direccion_comercial__c;
        parts = ciudad.split("-");

        /*format the accountCounter as "0000", concatenate and save 
          the resulting string to Codigo_Socio__c*/

        counterFormatted = formatter.format(accountCounter);
        acc.Codigo_Socio__c = parts[1] + counterFormatted;


Say that 100 Accounts will be inserted, accountCounter will start counting from 1 and will keep increasing until the insertion is finished. The problem(and here's where I am stuck) is that on the next Account insertion, the trigger will fire again and accountCounter will start counting again from 1, thus leading to replications of the generated sequential number in various Accounts. Of course we don't want that, right?

So the question is, how do I keep track of the accountCounter? I am thinking of quering the so-far Account objects stored in SF, before proceeding to generate a value forCodigo_Socio__c in the aforementioned trigger logic. Thus I will be able to check what value the field Codigo_Socio__c holded for the last inserted Account and then structure my trigger logic accordingly. Would that be a way? Other things/ways that I have to keep in mind?

Thanks in advance.

  • You can add an auto-number field to Account (leave out of the layout) and in an after trigger query that to use its value in your expression. But that is only good enough if you don't mind the occasional gap in the sequence. If gaps must be avoided, incrementing a custom setting with "for update" is one pattern - search this forum for "sequence number". Are gaps allowable?
    – Keith C
    May 26, 2015 at 10:37
  • Gaps are not allowable in this case I'm afraid. Thanks for the tip though, I will search for it and see what I can get!
    – Menio
    May 26, 2015 at 10:43

1 Answer 1


that would be a good approach to query the last inserted account and extract the counter from it. But, what if any account record(s) is deleted. You must also consider this scenario, so that in case of account deletion from where the counter will begin.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .