Is there a way to generate a contract number while creating a contract such that If the Contract created on "july 3rd 2022" then Starting number will be 2300010 If the Contract created on "july 3rd 2023" then Starting number will be 2400010


1 Answer 1


Solution w/ limitations (size of table is under 200,000 records, not the Name field)

  • Define the autonumber as {YY}{00000} and then
  • Use a formula field to add 1 to the {YY} part and add 10 to the {00000} part using your date cutoff as a guide

As formula fields can't be indexed, doing any query on that field requires a table scan so think carefully before going this route. Avoid this for the Name field as that field is displayed by default in all UX for that record and it would be different than what you want

Solution that works when the field is the Name or any other autonumber field, regardless of table size.

  • You'll need a scheduled job that runs at midnight on the date you define when the year part of your autonumber needs to be bumped up by one
  • The scheduled job calls the Metadata API to redefine the autonumber structure for your Contract field (Name or otherwise).
  • Change the displayFormat and startingNumber for the field in question using the Apex metadata API wrapper library

This approaches basically resets the autonumber rule on a given date. Just be sure the scheduled job doesn't get inadvertently canceled (have it run under a service user, never a real person) as it needs to run every year, like clockwork.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.