I have batch classes performing callout + [insert, update, upsert], so I set up everything about DML first:

    this.dml = new SObjectUnitOfWork.DatabaseDML();
    this.unitOfWork = new SObjectUnitOfWork(new List<Schema.SObjectType>{
    }, this.dml);
    this.dmlOptions = new Database.DMLOptions();
    this.dmlOptions.AllowFieldTruncation = true;

And then:

  // Stuff
  // Batch stuff

But then I still get:

Error saving record: STRING_TOO_LONG: [FIELD]: data value too large: 22222 (max length=4)

Any idea why it fails ?

  • 1
    Does setting the dmlOptions attribute on your batch have anything to do with the DML Operation which eventually gets performed? What happens after you call setDmlOptions? Do you call update record? Have you tried Database.update(recordToUpdate, dmlOptions)?
    – Adrian Larson
    Jan 24 '18 at 16:30
  • @AdrianLarson record is registered to a SObjectUnitOfWork for an upsert
    – Simonp
    Jan 25 '18 at 12:41


It seems that assigning DMLOptions to SObject is not working (anymore) (according to this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_setOptions ):

DMLOptions dmlOptions = new DMLOptions();
dmlOptions.allowFieldTruncation  = true;
SObject object = new Account( Name = 'Stack' );

Database.insert(new SObject[] { object }, false);

Assigning DMLOptions to Database (for each records then) is working (according to this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm#apex_System_Database_insert_4 ):

DMLOptions dmlOptions = new DMLOptions();
dmlOptions.allowFieldTruncation  = true;
dmlOptions.optAllOrNone          = true;
SObject object = new Account( Name = 'Stack' );

Database.insert(new SObject[] { object }, dmlOptions);

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.