Loading

Salesforce Platform: Error 'Cannot change field type' for an Auto Number Field Referenced in Apex Code

Udgivelsesdato: Oct 13, 2026
Beskrivelse

Changing an Auto Number field to another data type in Salesforce throws the following error if the field is referenced in Apex code:

"Validation Errors While Saving Record(s): There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was 'Cannot change field type of a custom field referenced in Apex class or trigger: AutoNumberTestMsg'."

This issue generally occurs during a data migration to a new org. If a custom field of type Auto Number is referenced in a trigger or Apex code, you normally need to temporarily change its type to Text to load data, and then change it back. However, the Apex reference blocks that type change entirely. This article explains two techniques to work around this issue using dynamic SOQL and the get() and put() methods.

Løsning

This article explains how to change the type of a Salesforce Auto Number field that is referenced in Apex code, using two different workaround techniques: the get() and put() methods, and dynamic SOQL.

Reproduce the Error

To illustrate the technique, create a custom object AutoNumberTest__c with a single custom field called autonumber__c of type Auto Number. You can confirm that the type of the autonumber__c field can be freely changed from Auto Number to Text and back again. This does not apply to the standard Name field set as auto number, because Name fields are treated differently and are not subject to the same Apex compiler type checking.

Now write a trigger on after insert or update of an AutoNumberTest__c object that references the autonumber__c field, creating a Note attached to the record:

trigger AutoNumberTestMsg
      on AutoNumberTest__c (after insert, after update) {

  List newNotes = new List();

  for (AutoNumberTest__c a : Trigger.new ) {

    Note msg = new Note(
      parentId = a.id,
      title = '' + a.autonumber__c,
      body = '' + a
    );
    newNotes.add(msg);
  }
  insert newNotes;
}

This code iterates through all new records in the trigger batch, creates a Note containing a string representation of the AutoNumberTest__c record, and inserts the notes in a single DML statement. After you create this trigger, attempting to change the autonumber__c field's type produces the same "Cannot change field type" error shown above. In a simple case like this, you can comment out the code, change the field type, and then uncomment the code. However, this is often impractical when deployments or more complex dependent code are involved.

Fix Option 1: Use get() and put()

The Object.get() and Object.put() methods, introduced in Winter '09, provide an alternative that avoids the compiler's direct field reference. Changing the expression a.autonumber__c to a.get('autonumber__c') lets you bypass the compile-time type check:

trigger AutoNumberTestMsg
      on AutoNumberTest__c (after insert, after update) {

  List newNotes = new List();

  for (AutoNumberTest__c a : Trigger.new ) {

    Note msg = new Note(
      parentId = a.id,
      title = '' + a.get('autonumber__c'),
      body = '' + a
    );
    newNotes.add(msg);
  }
  insert newNotes;
}

This trigger uses the get() method to read the autonumber__c field at runtime instead of referencing it directly. With this change, you can change the type of the autonumber__c field without generating an error. This approach trades compile-time safety for runtime risk. For example, if you rename the field's API name without updating the trigger, the code throws a runtime exception instead of failing to compile.

Fix Option 2: Use Dynamic SOQL

The same restriction applies to SOQL queries that directly reference the Auto Number field. The following query still triggers the compiler-enforced type-change restriction:

List aList =
    [select id, autonumber__c from AutoNumberTest__c];

To avoid the direct compiler reference, switch to dynamic SOQL using the Database.query() method:

List aList =
    Database.query('select id, autonumber__c from AutoNumberTest__c');

This query runs the same SELECT statement as a string at runtime, so the compiler does not register a reference to the Auto Number field. Dynamic SOQL queries can be more unwieldy to maintain, although the ability to use ':' interpolation in dynamic SOQL, introduced in Spring '09, makes this easier. When you need to temporarily change an Auto Number field's type to allow for updates, switching to dynamic SOQL is far simpler than the alternative workarounds this issue used to require.

Vidensartikelnummer

000385947

 
Indlæser
Salesforce Help | Article