I needed an autonumber field in account for an apex class which sends mails to customers. In sandbox I added an autonumber field and checked the option to generate values for existing records/accounts.

After deploying to production no values were generated for existing accounts. Autonumber field is readonly, so it seems I cannot use a script to update it. Since the field is related to my Apex class, it cannot be deleted (and added again) or changed to another type (and changed back to autonumber with option to generate values).

Is there any chance to get my autonumber values?

  • 1
    Change it to a text field, data load the values you want, then change it back to auto number.
    – Adrian Larson
    Aug 31, 2016 at 15:44
  • cannot change field type since it's related to my apex class.
    – Obi_77
    Aug 31, 2016 at 15:45
  • Sounds like a bug. You should open a case with support.
    – Adrian Larson
    Aug 31, 2016 at 15:47
  • And also, probably would have been better to create that field in prod and check that option than trying to deploy it. Lessons learned. Still, support should be able to help you fix this issue.
    – Adrian Larson
    Aug 31, 2016 at 16:27

3 Answers 3


These steps should work

  1. comment out the code which refers to the autonumber field
  2. change the field type of the autonumber field to text. If this is displayed on a screen, you'll notice the field is now editable.
  3. using data loader, load the data you need into the field
  4. change the field type back to autonumber - remember to set the starting number to the next value or else it will start the new records from 1

related articles which helped were - How do I reset/restart an auto number field?


In addition to Anil's answer, there's a way to do this as part of the deploy so you can avoid the Data Loader and fieldtype swap.

  • You will need to manually edit your source file for the object in question (e.g. Foo__c)
  • You will need to add in two lines as shown below for the autonumber field you desire to deploy and generate values for existing records.

      <label>Foo Number</label>
      <populateExistingRows>true</populateExistingRows>  // add this line
      <startingNumber>1</startingNumber>  // add this line with desired startVal
    • Don't forget to deploy xxx.profile files to make the field visible to those profiles that care.
    • Deploy with your IDE (or ant, sfdx, Gearset or equivalent)

When you deploy to the target org, SFDC will set the starting number to the value of <startingNumber> and will launch the populate existing records background job.

The reason this works is that tools that use the Metadata API (as of V42) don't have the ability to fetch either of the populateExistingRecords or startingNumber attributes of the CustomField metadata object. But they can deploy those values.

You can’t retrieve the starting number of an auto-number field through Metadata API. To specify a Starting Number while deploying, add a startingNumber tag for your field to your package.xml file. For example: 42


Source https://developer.salesforce.com/forums/?id=906F00000008l9ZIAQ Try anonymous APEX code to populate existing records.

If this does not work you need to deploy after commenting your field use in apex. Then change field populate it. Deploy back

  • 2
    It's a read-only field...
    – Adrian Larson
    Aug 31, 2016 at 15:46

You must log in to answer this question.

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