Activity Forums Salesforce® Discussions Getting Uncommitted work pending Error in my Test class

  • Neha

    Member
    April 13, 2016 at 11:44 am

    You might be doing DML operations before web service callout so for resolving this issue you may call DML operation and web service callout in different test methods.

  • Ravi

    Member
    April 13, 2016 at 12:43 pm

    Hi Akash,
    Sometimes a developer needs to create a record and then update it with information provided by a Web Service. However,  you can't do a web service callout after a DML operation in the same transaction. You should move your batch execution to the inside of the Test.startTest block.
    @isTest
    private class MyFunTest {
    @isTest
    static void runTest() {
    Account a = new Account(name='foo');
    insert a;

    Test.startTest();
    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
    MyCalloutClass.getInfoFromExternalService();
    Test.stopTest();
    }
    }

  • Akash

    Member
    April 13, 2016 at 12:59 pm

    Thanks @neha and @ravikant ...it worked for me

  • Parul

    Member
    September 20, 2018 at 1:24 am

    You may try the following for System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

    @isTest
    private class MyFunTest {
    @isTest
    static void runTest() {
    Account a = new Account(name=’foo’);
    insert a;

    Test.startTest();
    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
    MyCalloutClass.getInfoFromExternalService();
    Test.stopTest();
    }
    }

  • shariq

    Member
    September 20, 2018 at 7:07 pm

    Hi,

    Just call DML and webservice class in different transaction.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs