1

I have come up with the following trigger to assign a sequential number on the Account Number field.

trigger updateAccountNumber on Opportunity(before insert,before update)
{
  List<Id> accIds = new List<Id>();

  for(Opportunity opp:trigger.new)
  {
    if(opp.AccountId!=null)
    {
        accIds.add(opp.AccountId);
    }
  }

  Map<Id,Account> accMap = new Map<Id,Account>([select id,Region__c,AccountNumber from Account where id in:accIds]);

  for(Opportunity opp :trigger.new)
  {
    if(!accMap.IsEmpty())
    {
      if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber==null))
      {
        if(accMap.get(opp.AccountId).Region__c=='Americas')
        {
            accMap.get(opp.AccountId).AccountNumber = '10000';
        }
        else if(accMap.get(opp.AccountId).Region__c=='International')
        {
            accMap.get(opp.AccountId).AccountNumber = '20000';
        }
      }
    }
  }
}

My question is concerning the number assignment. The above code would just assign 10000 to the Account Number field everytime.

  1. I would like for this number to be sequential. i.e, 1st Account number would be 10000, followed by 10001, 10002, ...and so on.
  2. Even though i have a null condition in the trigger, the existing account numbers are being overwritten. I would like for the trigger to only fire if account number is null. Any pointers on how to achieve this?
1
  • 3
    So what happens if 2 people update 100 Opportunities via dataloader at the same time ? How can you prevent duplicates in the Accountnumber ? I would recommend using some autonumber field instead
    – ManSpan
    Jun 2, 2014 at 13:14

2 Answers 2

1

If you only want to assign the number for certain records and not all of them an alternate approach is to use a custom object and lookup to this object. The custom object's name is an auto number, in the format you need, and your trigger would create and assign the new auto number record to the Opportunity.

We are looking into doing something like this as we have a certain record type that needs it but the number has to be incremental, and in order. If we used an autonumber field on the Opportunity the number would be incremented many times more than it should.

3
  • if you do this, you need to be sure to check the 'Independent Auto-Number Sequence' in all orgs via Develop | Apex test Execution | Options otherwise your testmethods will cause gaps in the auto-number sequence
    – cropredy
    Nov 12, 2014 at 19:16
  • Yeah good point. Ive already done that for other reasons but definitely worth noting.
    – Girbot
    Nov 12, 2014 at 20:14
  • Nice approach. We ended up using a custom setting with values that would be incremented whenever the required criteria is met.
    – user8556
    Nov 20, 2014 at 20:21
0

While I must say I'm a bit puzzled at the business logic (or need) for this, I think the solution is quite simple. You don't want to only fire the trigger when your conditions are right. This is a decision that has to be made per record, while triggers run on batches of records!

You'll want to prevent to assign a number to a record that already has a number, that will stop you from overwriting that account number.

for instance:

if(accMap.get(opp.AccountId).Region__c=='Americas' && accMap.get(opp.AccountId).AccountNumber == null){
              accMap.get(opp.AccountId).AccountNumber = '10000';
}

Do you understand that your code will only assign numbers when opportunities are made for accounts ? I don't know your full setup and business case, but in my opinion it would make more sense to assign a number when the account is created or as soon as the region field is populated or changed.

You must log in to answer this question.

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