Retrieve, Modify, Persist – Part 7

Persist

This is it, The Final Chapter. In this series we’ve come a long way learning how to be successful while developing back end code in Salesforce. We’ve gained knowledge about how to retrieve Data with SOQL and SOSL, and we can modify our records in memory using a looping mechanism while keeping our data secure with Access Control. Now it’s time to persist our newly created or modified records to the database. Ready?

Learning to Speak DML

Wait, what’s DML? DML stands for Data Manipulation Language. It’s a set of commands that tells the platform to perform an action in the Database. For example, the most frequently used commands are: Insert, Update, and  Delete. They are very self-explanatory.

There are two other commands you can use:

  • Upsert is a combination of the update and insert commands (Get it? Up..sert?). It works as follows: If the records you are working on have an ID (or an external ID) that already exists in your database, the record will be updated. If the system is unable to find a match, then the record is inserted.
  • Undelete allows you to restore previously-deleted records. This is the same command that is used when you recover a record out of the recycle bin.

The 5 DML Secrets any Wizard Developer should know

  1. DML statements can operate on a single record, or they can operate on a group of records at a time. Know that if you insert a group of records, the platform will break it up into batches of 200. This means that if you insert a List of Accounts that contains a thousand accounts, the platform will divide it in smaller groups of 200 accounts.
  2. Mind the context. Each DML action has two possible contexts: before and after, EXCEPT the undelete action which only has the after context. The context refers to when the actions happens. This is important when it comes to extending functionality via triggers.
  3. Two operations might be faster than one. Even though the upsert command is very convenient, it is also known to be a slower method when compared to executing an insert followed by an update call.
  4. The power of sorting. One of the common failures when using DMLs is called a Lock Contention. This happens when two records share a reference to a third record and are updated in the same transaction. For example, inserting or updating two contacts for the same parent account in a single transaction might cause a Record Lock Contention Error, especially if you have Data Skew on the parent account. We strongly recommend sorting your records based on the parent reference; this reduces the chances of two child records being updated in separate batches and therefore reduces the chance of a Lock Contention. Ex: If you want to insert a thousand contacts using an ETL tool like Data Loader, make sure that any contacts related to the same account are organized consecutively in your list or csv file.
  5. Be aware of limits. There are two main per-transaction limits you MUST respect when dealing with DMLs:
    1. No more than 150 DMLs per transaction. This governor limit is the reason we don’t put DMLs inside of FOR Loops and we carefully craft our triggers to be bulkified.
    2. You can’t act on more than 10,000 records at a time. This includes not only the records you issue the action against, but also the records that are updated/created because of your action.

Show me the code!

Issuing DML statements is pretty straightforward. Here are some examples of code:

This is as simple as it gets. In Line 1 we are creating a new account record in memory, and in Line 2 we are issuing the Insert statement against the account we just created.

A more complex scenario:

Here we see the whole cycle of this series — Retrieve, Modify, Persist —

  • In Line 1 we retrieve several records from the database. These records were inserted with a misspelled value on industry.
  • On Lines 3-5 we iterate over the data set and modify the value in memory.
  • Lastly, on Line 6 we perform an update DML statement to persist our changes in the system.

Until now we’ve been issuing DML statements to perform Database Operations. There is another way of accomplishing the same: Using the Database Class.

The Database Class provides the same DML operations as methods. For example, using the Database Class, our previous examples would look like this:

There are three key things to note:

  1. The Database.Update() method will return a List of type Database.SaveResult object. This list stores the result of your operation for each record contained in the list.
  2. The Database.Update() method takes two parameters. The first one is the list of objects you want to operate on.
  3. The second parameter is called the allOrNone attribute. It is a boolean value that designates whether the operation is partial or not. If you specify False, then even if the list of objects you provided contains invalid records, the records that are valid will be successfully updated. If you specify True, then all the records provided on the list must be valid in order for the operation to succeed.

Just one more thing…

If you are like me and you are a big fan of transaction control, make special note of the following methods from the Database class:

  • Database.setSavePoint() – returns a savepoint variable that holds the current state of your database; use it before issuing any DML operations.
  • Database.rollBack() – if something went wrong, use this method along with the savepoint variable to roll back any DML operations you previously issued.

That’s it! You now have two great tools in your developer tool belt: DML statements and the Database Class. Use them wisely, and happy coding!

 

About the Author:

Clara Perez is a Salesforce MVP and Lead Developer at Great Wave who loves teaching Salesforce concepts.

 

Tags: DML, transaction control, database rollback, database savepoint, governor limits, save results.

119 thoughts on “Retrieve, Modify, Persist – Part 7

  1. Pingback: online blackjack echeck

  2. Pingback: sildenafil 100mg pill

  3. Pingback: cialis online canada

  4. Pingback: size of 5 mg pill cialis

  5. Pingback: sildenafil 50mg tablets

  6. Pingback: best casino to play

  7. Pingback: black viagra pills cenforce 200mg

  8. Pingback: tadalafil 10mg

  9. Pingback: viagra 25mg

  10. Pingback: cialis discount

  11. Pingback: buy viagra uk only

  12. Pingback: online drugstore reviews

  13. Pingback: where to get male viagra pills

  14. Pingback: when will the cost of sildenafil go down

  15. Pingback: stromectol canada

  16. Pingback: albuterol purchase online

  17. Pingback: ivermectin cream cost

  18. Pingback: how to get cialis discount

  19. Pingback: goodrx viagra

  20. Pingback: female viagra for sale

  21. Pingback: ivermectin withdrawal time

  22. Pingback: dr pierre kory ivermectine

  23. Pingback: proventil hfa 90 mcg inhaler

  24. Pingback: flccc ivermectin

  25. Pingback: flcc covid protocol

  26. Pingback: i-mask protocol

  27. Pingback: ivermectin overdose

  28. Pingback: buy ivermectin for humans uk

  29. Pingback: stromectol brand

  30. Pingback: topical ivermectin cost

  31. Pingback: ivermectin 50

  32. Pingback: stromectol 3 mg tablet

  33. Pingback: stromectol price

  34. Pingback: ivermectin 3

  35. Pingback: stromectol generic name

  36. Pingback: ivermectin tablets

  37. Pingback: where can i get ivermectin

  38. Pingback: where can i buy ivermectin

  39. Pingback: where to purchase ivermectin

  40. Pingback: buy viagra korea

  41. Pingback: ivermectin price

  42. Pingback: ivermectin cost

  43. Pingback: ignition casino help

  44. Pingback: cost of ivermectin

  45. Pingback: generic cialis

  46. Pingback: prednisone online

  47. Pingback: can you buy cialis in canada

  48. Pingback: tadalafil generic india

  49. Pingback: buy provigil usa

  50. Pingback: ivermectin tablet 1mg

  51. Pingback: buy viagra greece

  52. Pingback: ivermectin 3 mg tablet dosage

  53. Pingback: canadian generic cialis online

  54. Pingback: cheapest generic viagra prices online

  55. Pingback: stromectol pill for humans

  56. Pingback: tadalafil vidal

  57. Pingback: generic cialis canada

  58. Pingback: buy canadian cialis online

  59. Pingback: where to purchase sildenafil pills

  60. Pingback: what is tadalafil

  61. Pingback: prescription cialis from canada

  62. Pingback: ivermectin rx

  63. Pingback: where to purchase sildenafil pills

  64. Pingback: cheap generic sildenafil pills

  65. Pingback: cialis super active

  66. Pingback: generic cialis

  67. Pingback: where can i buy cialis without a prescription

  68. Pingback: prednisone 20mg five days

  69. Pingback: buy prednisone vet

  70. Pingback: discount cialis no prescription

  71. Pingback: molpunavir

  72. Pingback: cheap cialis prescription

  73. Pingback: generic cialis over the counter

  74. Pingback: cialis tablet

  75. Pingback: ivermectin 5 mg price

  76. Pingback: side effects prednisone

  77. Pingback: how to get cheap viagra online

  78. Pingback: tadalafil cialis

  79. Pingback: online casino gaming

  80. Pingback: ivermectin brand name

  81. Pingback: generic

  82. Pingback: ivermectin tablets walmart

  83. Pingback: ivermectin bnf

  84. Pingback: play free win real money

  85. Pingback: cialis online

  86. Pingback: generic viagra price

  87. Pingback: cialis tablets

  88. Pingback: ivermectin human dosage

  89. Pingback: buy cialis online without prescription

  90. Pingback: where to buy ivermectin pills

  91. Pingback: where to get tadalafil without a prescription

  92. Pingback: ivermectin 3 mg

  93. Pingback: cost of ivermectin

  94. Pingback: ndc augmentin

  95. Pingback: where to buy ivermectin uk

  96. Pingback: azithromycin lek

  97. Pingback: augmentin chalazion

  98. Pingback: ciprofloxacin arthritis

  99. Pingback: zithromax price

  100. Pingback: azithromycin abuse

  101. Pingback: stromectol

  102. Pingback: ivermectin 1 cream 45gm

  103. Pingback: where to buy furosemide

  104. Pingback: furosemide 4 mg

  105. Pingback: stromectol buy

  106. Pingback: ivermectin 1% cream generic

  107. Pingback: use of ivermectin

  108. Pingback: durvet ivermectin

  109. Pingback: ivermectin humans

  110. Pingback: play luckyland slots real money

  111. Pingback: ivermectin syrup

  112. Pingback: ivermectin cost australia

  113. Pingback: ivermectin rx

  114. Pingback: ivermectin where to find

  115. Pingback: where to buy ivermectin uk

  116. Pingback: ivermectin rx

  117. Pingback: ivermectin drug

  118. Pingback: stromectol covid 19

  119. Pingback: ivermectin australia over the counter

Comments are closed.