Skip to main content

Latest Post

Record Types & Page Layout Strategy

  Record Types & Page Layout Strategy 💬 In plain words:   Record Types let one object act like different forms for different teams. Different picklist values, page layouts, and processes — same Account object. Use them for truly different business processes. Not for cosmetic layout changes. 📌 Example:   One Account object, two businesses: 'Retail Customer' record type shows B2C picklist values and layout. 'Enterprise Client' shows contract fields and a different sales process. Same table, two costumes. 🧠 One object, many costumes:   A Record Type is a costume on the same object — different picklists and layouts, same table underneath. Concept Record Types split one object into business variants. •   Each type can carry its own subset of picklist values, its own business process — Lead, Case and Opportunity stages — and its own page layout per profile. •   Lightning has changed the calculus. Dynamic Forms and conditional comp...

Uncommitted Work Pending in Salesforce: Handling and Best Practices

Introduction:

In Salesforce development, you might encounter the error message "You have uncommitted work pending. Please commit or rollback before calling out." This error occurs when you try to make an HTTP callout or perform a DML operation after performing a DML operation but before committing the transaction. In this blog post, we will explore what causes this error, why it is important to handle it properly, and provide code examples to illustrate how to resolve it.


Understanding the Error:


When working with Salesforce, DML operations (such as inserting, updating, or deleting records) and HTTP callouts are part of a transaction. Salesforce enforces a governor limit that prevents making a callout or performing another DML operation until the current transaction is committed or rolled back. If you attempt to make an HTTP callout without committing the previous DML operation, you will encounter the "uncommitted work pending" error.


Code Example:


Let's take a look at an example that triggers the error:


public class MySalesforceClass {

    public void performDmlAndCallout() {

        // Perform a DML operation

        Account acc = new Account(Name = 'Test Account');

        insert acc;


        // Make an HTTP callout

        HttpRequest req = new HttpRequest();

        req.setEndpoint('https://api.example.com');

        req.setMethod('GET');

        HttpResponse res = new Http().send(req);

    }

}


In the above code example, we insert an Account record and then immediately make an HTTP callout. This will result in the "uncommitted work pending" error.


Handling the Error:


To handle the "uncommitted work pending" error, you need to separate the DML operation and the HTTP callout into separate transactions. One common approach is to use the @future annotation to asynchronously invoke a method that performs the callout.


Here's an updated version of the code using the @future annotation:


public class MySalesforceClass {

    public void performDmlAndCallout() {

        // Perform a DML operation

        Account acc = new Account(Name = 'Test Account');

        insert acc;


        // Call a method asynchronously to perform the callout

        doCalloutAsync();

    }


    @future(callout=true)

    public static void doCalloutAsync() {

        HttpRequest req = new HttpRequest();

        req.setEndpoint('https://api.example.com');

        req.setMethod('GET');

        HttpResponse res = new Http().send(req);

    }

}


In this updated code, we move the HTTP callout to a separate method annotated with @future. This allows the callout to occur in a new transaction after the DML operation is committed. By separating the DML and callout operations, we avoid the "uncommitted work pending" error.


Conclusion:


Handling the "uncommitted work pending" error in Salesforce is crucial for ensuring the integrity of your data and preventing errors during transaction processing. By understanding the cause of the error and using proper techniques like asynchronous processing, you can avoid this issue and maintain smooth execution of your code.


Remember to always separate DML operations and HTTP callouts into separate transactions, using techniques such as the @future annotation, to handle uncommitted work effectively and maintain the stability and reliability of your Salesforce applications.

Popular Posts

Salesforce LWC Code for Multi-Select Lookup

Introduction: In Salesforce Lightning Web Components (LWC), implementing a multi-select lookup field can enhance the user experience and provide greater flexibility for selecting multiple related records. In this blog post, we will walk through the process of creating a multi-select lookup field using LWC. We will cover the required code snippets and provide step-by-step instructions to help you implement this functionality in your Salesforce org.