Setting DML Option in Apex

You can specify DML options for insert and update operations by setting the desired options in the Database.DMLOptions object.

The Database.DMLOptions class has the following properties:

  • allowFieldTruncation Property [truncation behavior of fields]
  • assignmentRuleHeader Property [Assignment rule information]
  • dupicateRuleHeader [Duplicate rule information]
  • emailHeader Property [Whether automatic emails are sent]
  • localeOptions Property [The user locale for labels]
  • optAllOrNone Property [Whether the operation allows for partial success]

1. allowFieldTruncation Property

The allowFieldTruncation property specifies the truncation behavior of strings.  If you specify a value for a string and that value is too large, the value is truncated in API version below 15.

For API version 15.0 and later, if a value is specified that is too large, the operation fails and an error message is returned. The allowFieldTruncation property allows you to specify that the previous behavior, truncation, be used instead of the new behavior in Apex saved against API versions 15.0 and later.

If allowFieldTruncation  value is true, the property truncates String values that are too long.

Database.DMLOptions dml = new Database.DMLOptions();                                                         dml.allowFieldTruncation = true;

2. assignmentRuleHeader Property

The assignmentRuleHeader property specifies the assignment rule to be used when creating a case or lead.

Using the assignmentRuleHeader property, you can set these options:

  • assignmentRuleID: The ID of an assignment rule for the case or lead. The assignment rule can be active or inactive. The ID can be retrieved by querying the AssignmentRule sObject. If specified, do not specify useDefaultRule. If the value is not in the correct ID format (15-character or 18-character Salesforce ID), the call fails and an exception is returned.

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.assignmentRuleId= '01QD0000000EqAn';

Lead l = new Lead(company='ABC', lastname='Smith');
l.setOptions(dmo);
insert l;

  • useDefaultRule: Indicates whether the default (active) assignment rule will be used for a case or lead. If specified, do not specify an assignmentRuleId.

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;

Lead l = new Lead(company='ABC', lastname='Smith');
l.setOptions(dmo);
insert l;

3. dupicateRuleHeader Property

The dupicateRuleHeader property determines whether a record that’s identified as a duplicate can be saved. Duplicate rules are part of the Duplicate Management feature.

Using the dupicateRuleHeader property, you can set these options.

  • allowSave: Indicates whether a record that’s identified as a duplicate can be saved.

Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.AllowSave = true;
Account duplicateAccount = new Account(Name='dupe');
Database.SaveResult sr = Database.insert(duplicateAccount, dml);
if (sr.isSuccess()) {
System.debug('### - Duplicate account has been inserted in Salesforce!');
}

4. optAllOrNone Property

The optAllOrNone property specifies whether the operation allows for partial success. If optAllOrNone is set to true, all changes are rolled back if any record causes errors. The default for this property is false and successfully processed records are committed while records with errors aren’t.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s