The Salesforce platform provides a wide range of features to developers that help them create powerful and robust applications. One such feature is the Database.dmloptions class, which allows developers to specify additional options for DML (Data Manipulation Language) operations. DML operations are used to insert, update, and delete records in the Salesforce database The Database.dmloptions class provides a way to set additional options for these operations, such as specifying how to handle errors or controlling how triggers are fired. In this blog, we will explore the various options available in the Database.dmloptions class and how they can be used to improve the performance of your applications.
Database.SaveResult results = Database.insert(recordsToInsert, new Database.DMLOptions());
In this example, we are creating a new instance of the Database.DMLOptions class and passing it to the insert method. We are not setting any options in this case, so the default options will be used.
Setting the DML Options
Available DML Options the Database.dmloptions class provides several options that can be set for DML operations. Here are the available options:
Assignment Rule Header:
The Assignment Rule Header option allows you to specify whether assignment rules should be triggered for the records being inserted or updated. By default, the assignment rules are triggered, but you can set it to false to skip them. This can be useful if you want to manually assign records to users or teams.
Here are some of the actions that can be customized using Assignment Rule Header:
In this example, we are using a specific assignment rule (specific Id), this rule we can get by querying the AssignmentRule sObject. While using this property do not specify useDefaultRule.
Duplicate Rule Header:
Duplicate Rule Header is a header object that can be included in a DML operation to override the default behaviour of Duplicate Rules. By using this header object, you can customize the actions taken when a duplicate record is detected.
Here are some of the actions that can be customized using Duplicate Rule Header:
- Allow Save
By default, if a Duplicate Rule identifies a potential duplicate record, Salesforce will block the creation of the record. However, by using Duplicate Rule Header, you can override this behavior and allow the record to be created.
To allow the record to be created, you can set the AllowSave parameter to true:
This property indicates whether duplicate rules run in context with the current user’s sharing or not. If it is set as true, then the duplicate rules results will not include record details for duplicates that are not available to the user.
This option allows you to specify whether to send email notifications for the records being inserted or updated. You can set it to true to send the notifications or false to skip them.
Some of the commonly used properties are:
This property indicates whether the email will be triggered that is sent to the user in the organization.
In this example, we are setting the Email Header option to not trigger email notifications to the user who initiated the DML operation.
This property indicates whether the auto response rules will be triggered or not.
In this example, we are setting the triggerAutoResponseEmail as true so whenever we will perform DML operation on leads and cases, the auto response rule will be triggered.
This property controls whether to trigger the email outside the organization or not.
This option allows you to specify whether to roll back the entire transaction if any records fail to insert, update, or delete.
You can set it to true to roll back the entire transaction or false to allow the successful records to be committed.
In this example, we are setting the Opt All Or None Child option to roll back the entire transaction if any child records fail.
We can keep it false if we want to perform operation partially.
Allow Field Truncation:
This option allows you to specify whether to truncate field values that are too long to fit in the database field. You can set it to true to allow truncation or false to throw an error if the value is too long.
In this example, we are setting the Allow Field Truncation option to truncate the field value if it’s too long.
In summary, the Database.dmloptions class provides a way to set additional options for DML operations in Salesforce. These options can be used to control how triggers are fired, handle errors, and set various other settings. By setting the appropriate options, you can customize the behavior of your DML operations and ensure that they perform as expected.