1.4 DML executes in one transaction

The concept

One important concept about DML is it will be executed in one transaction. So if you insert or update or delete a list of records, it either all succeed or all fail.

Below is a experiment on this. To do this, we need to setup a validation rule on our Merchandise object. Let’s setup a validation rule to validate the original price is no lower than 50 dollars in order to have a discount. If you don’t know how to do it, follow the screenshot below:

validationrule

Then we can write relevant code to test this:


Merchandise__c mer = new Merchandise__c();
mer.name = 'Books';
mer.Original_Price__c = 20;

Merchandise__c mer2 = new Merchandise__c();
mer2.name = 'Bike';
mer2.Original_Price__c = 300;

List<Merchandise__c> merList = new List<Merchandise__c>();
merList.add(mer);
merList.add(mer2);
insert merList;

Execute the code in anonymous block and you will get the validation error. Turn to Salesforce platform, you can find that neither of the records are inserted.

Next Post

1.5 Use Database Object to do DML operations

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *