+ Start a Discussion
Ganesh Punde 10Ganesh Punde 10 

Not able to insert Sobject throwing System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.

Hi All,
I am not able to insert the SObject from Apex class. It throws System.CalloutException exception.

When I hit the button from UI. I have created the SObect by fetching data from some external Https APIs and when I tried to insert it. The callout exception is thrown. 
I have tried to Add future Annotation for the method as well as tried to add a Queueable job. Unfortunately, it not worked for me.

Could you please help me to solve the issue?
Thanks in Advance.

Queueable class is mentioned below:-

global class QueueableLoggerCallout implements Queueable , Database.AllowsCallouts 
{
    TestLogs__c sl;
    
    public void execute(QueueableContext context) {
        System.debug('QueueableLoggerCallout.execute Start.');
        insert sl;
        System.debug('QueueableLoggerCallout.execute End.');
    }
    
    global QueueableLoggerCallout(TestLogs__c qsl)
    {
        sl = qsl;
    }
}


Class with future method :-

global class Logger
{
    TestLogs__c sl;
    
    @future(callout=true)
    global static void addLog(TestLogs__c qsl)
    {
        insert qsl;
    }
}
bob_buzzardbob_buzzard
I can't see how you would get that error message by attempting to insert a record. Typically the issue is that you have made some changes to the database and then you attempt to make a callout. The platform correctly blocks this as it would lock the database for the duration of the callout, which could be minutes.  Can you post the code that is executed when the button is pressed? I'd imagine that is inserting/updating a record before the callout is executed.