Perform Bulk DML Operations with the UI Record API in LWC

Telegram logo Join our Telegram Channel
Perform Bulk DML Operations with the UI Record API in LWC

Greetings, Fellow Trailblazers! Often, we need to work with multiple records from LWC components. The go-to option for that is to use an Apex controller, which allows us to perform DML operations on multiple records from LWC. However, in "some scenarios", you can avoid using an Apex controller and perform DML operations on Salesforce records directly from the LWC JavaScript controller.

In this article, we will explore, how to use the UI Record API to modify multiple records. But you might wonder how we can achieve this since the UI Record API functions support DML operations for single records only.

The key is that we use JavaScript promises to call these functions for multiple records. Essentially, we can group all the DML function calls into a list of promises and execute them simultaneously.

Let’s explore this with some examples.

Note: This blog article focuses solely on DML operations, so the user interface aspects are not covered.


Bulk Insert records using createRecord UI Record API

As we know, the createRecord function is used to create a single record from an LWC component. If you're not familiar with it, I have covered the createRecord function in detail in this post: Lightning Web Component createRecord example

Let’s consider a scenario where you are building a component that allows users to create multiple contacts. The component provides a table component where users can add a row for each contact. Now, you want to save these contacts to Salesforce.

Assuming these contacts are stored in an array called contacts, you can use the following code to save them:

  1. createBulkContacts() {
  2. // create list of promises with record input for each contact.
  3. const promises = this.contacts.map(con => createRecord(
  4. { apiName: CONTACT_OBJECT.objectApiName, fields: { ...con } }
  5. ));
  6. // execute all requests simultaneously
  7. Promise.allSettled(promises).then(results => {
  8. results.forEach((result, index) => {
  9. if (result.status === "fulfilled") {
  10. console.log('contact saved:', result.value);
  11. } else {
  12. console.log(`contact ${index} failed:`, result.reason);
  13. }
  14. });
  15. }).catch(errors => {
  16. console.log("error while saving contacts", errors)
  17. })
  18. }

Understanding the above code:

  • The UI Record functions are promises that are executed asynchronously to perform DML operations on Salesforce records, createRecord is one of them. We are using that to create the contacts. Line 3 to 5 builds a list of promises, each promise represents the creation of a single contact.
  • Line #8 to 18 executes these promises all at once and handles save result one by one.

Result & Error handling

Let's assume that we try to save 10 records and 4 of them are failed, then:

  • The results array returned by Promise.allSettled will contain 10 elements, each representing the outcome of a single contact creation. The results are categorized by status like below:
    • fulfilled: The promise succeeded, and the result will be available in result.value.
    • rejected: The promise failed, and the reason for the failure will be in result.reason.
  • If any unexpected error is occured it will be handled by the catch block.


Similarly, you can follow the same pattern for other DML operations like Update or Delete.


Caveats

While this approach is easiest way to there are some caveats to using this approach, that you should consider before using this approach.

Partial Success:

The operation is not an "all-or-none" transaction. This means if some records fail, others may still succeed, and there is no automatic rollback for the failed records.

Performance Overhead: 

Each record is inserted individually using separate API calls, which can lead to significant performance degradation compared to bulk DML operations that handle multiple records in a single transaction.

Error Handling Complexity: 

Since errors are handled on a per-record basis, it can become more complex to manage failures and retries. You need to implement custom logic to handle errors, such as determining which records failed and retrying them, or notifying users appropriately.


No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com