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

Auto numbering that is unique to each account.

Hi all,


Is there a way to have a field that will automatically create a number  for each Address that I create within an Account?


  1. I created an Object called Location - Were we store all of our address (Billing, Shipping, Servie and Corporate)
  2. I created an Auto Number field called Location #.
  3. This field needs to auto populate with a number upon creation for reporting purposes.
  4. This number needs to be unique by Account.
  5. Currently when I create an address it numbers correctly within the first Account: 1,2,3,etc. - but when I go to another Account, it continues the numbering cycle: 4,5,6, etc.
Any suggestions?
Best Answer chosen by Admin (Salesforce Developers) 

Figured this one out too, need help with deployment though.  Anyone have a how to guide on trigger deployment?




/* Loop through all locations and collect the current numbers. */ trigger LocationUpdate on Location__c (before insert) { /*Loop every new location you enter*/ for(Location__C { if(l.Location__C==null){ String cname = l.Account__c; /* This writes to the debug log*/ //System.debug('location data: '+l); /*This looks up all of the location numbers for the specific account*/ List <Location__c> a = [select location__c from Location__C where ACCOUNT__C= :cname ]; /*If the list results are empty*/ system.debug(a); if(a.isEmpty()){ l.Location__C=1; }Else { List <Decimal> b = new List<Decimal>(); for (Location__C tempLocation:a) { b.add(tempLocation.Location__c); } Decimal highest = 0; b.sort(); highest = b[b.size()-1]; l.Location__C=highest + 1; } } } }