+ Start a Discussion
Shalaka KokateShalaka Kokate 

Customize auto-numbering

In SalesForce, we would like to add a field (or multiple) for all of our accounts that designates them to a certain route. The format of this field would look something like : RouteName-#, 
where # denotes the position of the account for that RouteName. For example, if there are 50 accounts in 5CHA route, the field (let's call this Route Position) for all those accounts will start off as 5CHA, but will all have a unique position from 5CHA-1 to 5CHA-50. 

What we are hoping to achieve is to this field automatically increase or reduce for all affected accounts when we add or remove accounts, respectively, from the same route. The overall route order will most likely not change. For example, we have decided a new account will be in Route Position 5CHA-23, after the current 5CHA-22 but before the current 5CHA-23. This will place the new account at 5CHA-23, while all the subsequent account starting 5CHA-23 to 5CHA-50 will be updated by a +1 in their position. Similar trend will apply for when we are removing an account with a -1 to their Route Position. 

With that said, there will be no duplicates in Route Position for any accounts. There might be cases where an account might have up to 3 route positions if they belong in 3 different routes. 

Route is a multi-picklist field on accounts object.
Usman AslamUsman Aslam
You will need:

- A text field to hold the auto number (NOT an AutoNumber)
- A batch job that will run nightly to populate new field.

Pseudo Code of the Logic in batch job:
 - SELECT Id, Name, Region_Number__c (new text field for AutoNumber) from Account order by Region__c (multi-picklist field)
 - Iterate over accounts
 - Set Region_Number__c based on Region__c field
 - Update all accounts

Additional considerations:
If you this number of accounts for each region can be more that the batch size. Implement Stateful interface as well for the Batch Job class. And store the last seq number in a Map<String, Integer> regionLastSequenceNumner so you can use it next batch of the batch job.

Hope this will resolve the issue.

If this solution works for you, mark it as the best answer.