0

I have a country picklist on invoice object, I want to auto generate invoice number for each country following this format:

UK

GB-00001

Germany

DE-00001

Swiss

CH-00001

I want to have each country its own set of numbers, for all three countries UK, DE, and CH, 0001 is a possible invoice number for example.

Would appreciate it if someone has already done this and can share the code snippet

2 Answers 2

1

you could create a custom formula text field to do this

3
  • Yes, this one you can do without any code, workflow or processes. Commented Jan 21, 2021 at 11:20
  • 2
    Please include an example with this so it is a complete answer.
    – Dave Humm
    Commented Jan 21, 2021 at 11:43
  • I want to have each country its own set of numbers, with the above solution I can not have 0001 for all three UK, DE, and CH, auto number will not solve this problem Commented Jan 21, 2021 at 13:23
0

When it comes to using the auto-number field, your options are limited to a number and the current date (ex: {YYYY}-{MM}-{DD}-{000}). You can't pull data from elsewhere. So your auto-number field will simply be digits. If you want 5 digits like in your question, it will be: {00000}.

So if you need to take some other value from the record and pair it with the auto number field, then a formula is in order.

Create a text formula field and have it concatenate the relevant data together:

Country_Iso2_Code__c && '-' && Name

In the formula above I am taking a field called Country ISO 2 Code (Country_Iso2_Code__c) that holds the ISO 3166 Alpha-2 code for the country on the record and concatenates it with a hyphen and an auto-number Name (Name) field. This will generate codes like:

  • GB-00020
  • DE-00101
  • JP-00999
  • US-99999

This is all assuming of course that your auto-number can increment with each new record regardless of country. If you need an auto-number paired with each country code (so that you can have GB-1, DE-1, JP-1 etc.) then I'm afraid it's going to take a lot of custom code because you will need to get the total count for any given country each time you insert a record.

2
  • I want to have each country its own set of numbers, with the above solution I can not have 0001 for all three UK, DE, and CH, auto number will not solve this problem Commented Jan 21, 2021 at 13:22
  • I'm afraid there isn't a quick and easy way of handling that. You would need to have trigger logic on insert that takes a count of the current number of records by country, adds one to the total, and then use that for your numbering. It would be resource intensive since you would be querying for a large dataset that grows over time. If you make the field unique that would prevent scenarios where two fields have the same value. You'd want to make sure the country field is indexed to gain the most efficiency there.
    – nbrown
    Commented Jan 21, 2021 at 14:06

You must log in to answer this question.

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