How can I solve the issue of “you have uncommitted work pending. Please commit or rollback before calling out”?

210    Asked by DylanForsyth in Salesforce , Asked on Feb 5, 2024

When I was going through my particular task in a Salesforce development environment then I recently received an error message that was showing “You have uncommitted work pending. Please commit or rollback before calling out”. This error message came when j was attempting to make a fallout after performing DML operations. Now, how can I solve this particular issue? 

Answered by Daniel BAKER

 In the context of selenium, if you are getting the issue of “you have uncommitted work pending. Please commit or rollback before calling out” then you should separate the DML operations and callouts. The solution may include using the annotation of “@future” or even asynchronous processing.

Here is the example given of how you as a developer can structure the code:-

Public class MyApexClass {
    @future
    Public static void performCalloutAsync(Id recordId) {
        // Perform the callout logic here
    }
    Public static void processRecordsWithDML(List records) {
        // Perform DML operations
        Insert records;
        // Get the IDs of the inserted records
        List recordIds = new List();
        For (MyObject__c record : records) {
            recordIds.add(record.Id);
        }
        // Call the asynchronous method for callout
        performCalloutAsync(recordIds);
    }
}


Your Answer

Interviews

Parent Categories