Sub topic 12
Database.DMLOptions for field truncation and other behaviors
This chapter assumes you can write basic Apex, know SObjects and fields, use try/catch, and know that Apex runs in transactions under governor limits. It also assumes you have seen Database.insert with the allOrNone flag. Here you will use a Database.DMLOptions object to change how a DML call behaves.
What DMLOptions is
Database.DMLOptions is a class that bundles settings for a single DML call. You create one, set the properties you want, and pass it to a Database method instead of the Boolean allOrNone flag. DML statements such as insert accounts; have no place to pass options. To use DMLOptions, you must call a Database method.
Truncating over-long values
By default, a string that is longer than its field allows causes a STRING_TOO_LONG error for that record. Setting allowFieldTruncation to true tells Salesforce to cut the value to the field length and save the record.
Database.DMLOptions opts = new Database.DMLOptions();
opts.allowFieldTruncation = true;
opts.optAllOrNone = false;
List<Account> accounts = new List<Account>{
new Account(Name = 'A'.repeat(300))
};
List<Database.SaveResult> results = Database.insert(accounts, opts);
System.debug(results[0].isSuccess());The Name field on Account holds 255 characters, so the 300-character value is cut to 255 and isSuccess() returns true. Without the option, the same insert would fail for that record.
The optAllOrNone property replaces the Boolean argument. Setting it to false allows partial success, so a failing record does not stop the valid ones from saving. Its default is true.
Other behaviors you can set
DMLOptions has more properties than the two above. These are the ones you are most likely to use.
| Property | What it does |
|---|---|
| `allowFieldTruncation` | Truncates string values that exceed the field length instead of failing. |
| `optAllOrNone` | Controls whether one failure rolls back the whole call. |
| `duplicateRuleHeader.allowSave` | Lets a record save even when a duplicate rule would normally block it. |
| `emailHeader.triggerUserEmail` | Controls whether Salesforce sends emails for events such as new user creation. |
| `assignmentRuleHeader.useDefaultRule` | Applies the active assignment rule to Case or Lead records. |
Each header-style property is an object with its own fields, so you set a nested value:
Database.DMLOptions opts = new Database.DMLOptions();
opts.assignmentRuleHeader.useDefaultRule = true;
Lead l = new Lead(LastName = 'Rivera', Company = 'Northwind');
Database.SaveResult sr = Database.insert(l, opts);
System.debug(sr.isSuccess());Common errors
- Using options with a statement.
insert accounts opts;does not compile. Switch toDatabase.insert(accounts, opts). - Expecting truncation on every type.
allowFieldTruncationonly affects string values. A number outside its range or a value not in a restricted picklist still fails. - Forgetting
optAllOrNone. A newDMLOptionsobject defaults to all-or-none. If you want partial success, setoptAllOrNone = falseyourself. - Silent data loss. Truncation saves the record without telling you a value was cut. Use it only where losing trailing characters is acceptable, such as free-text notes.
- Ignoring results. With
optAllOrNone = false, errors do not throw. Check eachSaveResult, as covered in the earlier chapters.
Exercise
You call Database.insert(accounts, opts) with opts.allowFieldTruncation = true and nothing else set. One account has a 300-character Name and another has no Name at all. What happens to each, and why?
Show answer
The 300-character Name is truncated to 255 characters and saved. The account with no Name fails, because truncation does not fill in missing required fields. Since optAllOrNone still has its default of true, the failure rolls back the whole call, so neither record is saved. To save the valid record, also set opts.optAllOrNone = false.
Exercise
Why can't you pass a DMLOptions object to an update statement?
Show answer
DML statements accept only the record or list of records. Options can only be passed as an argument to a Database class method such as Database.update(records, opts).