Monday, 5 June 2023

"Uncommitted Work Pending" error in Salesforce

The "𝐔𝐧𝐜𝐨𝐦𝐦𝐢𝐭𝐭𝐞𝐝 𝐖𝐨𝐫𝐤 𝐏𝐞𝐧𝐝𝐢𝐧𝐠" error is typically encountered when performing database operations, such as DML (Data Manipulation Language) statements or callouts, within the same transaction as a platform event or email sending operation.

 

Salesforce follows a pattern called "ACID" (Atomicity, Consistency, Isolation, Durability) for maintaining data integrity. This means that database changes are committed together or rolled back together to ensure consistency. When a database operation encounters an error, Salesforce automatically rolls back all changes made within that transaction.

 

However, platform events and email sending operations are not part of the ACID transaction. They cannot be rolled back, even if an error occurs in the transaction. This is where the "Uncommitted Work Pending" error comes into play.

 

To resolve this error, you can follow these steps:

 

1.Move the database operations, such as DML statements or callouts, to a point after the platform event or email sending operation.

2.If you need to update data based on a platform event, consider using Apex triggers on the platform event object to perform the necessary database operations separately.

3.If you encounter the error while performing an email sending operation, make sure to separate it from other database operations by using a separate transaction.

No comments:

Post a Comment