I am consuming webservice rest api(post method) in apex class using webservice static method.i wrote test class but i am getting System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.

Here is mycode:

Global with sharing class WebServiceClassGetQLIT 
WebService static String InsertQuoLiItems(String QuoteNumber,String quoteID)
String jsonpostdata = '{"quoteNo":"'+quoteObj.Erapid_Quote_no__c+'","environment": "'+environment+'","userId":"'+userId+'"}';
                string endpointUrl=Label.SFXT_Server_IP+'/rep_web/api/quote/lineItems';
                Http http=new Http();
                HttpRequest req=new HttpRequest();
                req.setHeader('Content-Type', 'application/json');
                req.setBody(jsonpostdata );

                HttpResponse res;
                res=new http().send(req);
                String jsonparse = res.getBody();
                QuoteLineItemResponse lineItemResponse = (QuoteLineItemResponse)JSON.deserialize(jsonparse, QuoteLineItemResponse.class);
                system.debug('lineitem response'+lineItemResponse);
                Quote_line_Items__c QuoLObj;
                list<Quote_line_Items__c> QuoLOList = new list<Quote_line_Items__c>();
//mapping feilds

test class is:

private class WebServiceClassGetQLIT_UnitTest 
static testmethod void testAccountCallout() {
         RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        SingleRequestMock fakeResponse = new SingleRequestMock(200,'OK','[{"quoteNo":"102153_00","environment": "US_SANDBOX","userId":"gennesse"}]',null);
        Test.setMock(HttpCalloutMock.class, fakeResponse);

        Account acc = new Account(name = 'Testing Account',Country__c = 'India',type = 'C/S Company');
        insert acc;
        Contact con = new Contact(LastName = 'testing',accountId=acc.Id);
        insert con;
        Project__c proj = new Project__c(Country__c = 'Afghanistan',Name = 'Test Project',Project_Type__c = 'New Construction',Lead_Source__c = 'Advertisement',
            Project_Source__c = 'sac',Project_Stage__c = 'Completed',Postal_Code_Zip_Code__c = '6514',Funding_Type__c = 'Private');
        insert proj;
        C_S_Opportunity__c opp = new C_S_Opportunity__c(Project__c = proj.id,Stage__c = 'Prospecting',CS_Product_Added__c = 'ADS',Project_type_text__c = 'Trade(C/S)',       
                                        Close_Date__c = system.today());
        insert opp;     
        Product2 p = new Product2(name='unittest',Family = 'License');
        insert p;
        //Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
        string stdPb = '01s30000000J5mMAAS';   //01s30000000J5mMAAS    
                 //pbe.pricebook2id = stdPb.id;
        PricebookEntry pbe = new PricebookEntry(pricebook2id = stdPb,product2id = p.id,unitprice=1.0,isActive = true);
        insert pbe;

        Quotes__c Quo=new Quotes__c();

        Quo.Erapid_Quote_no__c = '015403_00';
        Quo.Account__c = Acc.Id;
        Quo.C_S_Opportunity__c = opp.Id;
        Quo.Contact__c = Con.Id;
        Quo.Status__c = 'Draft';
        Quo.Description__c = 'ae';
        Quo.Contact_customer__c = con.id;
        Quo.Quote_Type__c = 'New';
        Quo.Quote_Origin__c = 'ALTERNATE';
        Quo.First_Date_of_Forecasted__c = date.newInstance(2008, 12, 1);
        Quo.Expiration_Date__c = date.newInstance(2008, 12, 1);
        Quo.Grand_Total__c = 12;
        Quo.Discount__c = 12;
        Quo.Project__c = proj.Id;
        Insert Quo;
        string r = WebServiceClassGetQLIT.InsertQuoLiItems('102153_00' , quo.ID);

  • IN addition your test is not actually testing anything. All it is doing is running code. You need to check for outcomes both for expected and unexpected.
    – Eric
    May 15, 2016 at 6:00

1 Answer 1


See this: You have uncommitted work pending. Please commit or rollback before calling out

A Web Service Callout may not occur after a DML statement within the same transaction. To achieve the required action, the transaction must be separated into two parts so that the DML transaction is completed before the Web Service Callout occurs.

and this is the reason of your issue:

The Test.startTest statement must appear before the Test.setMock statement.

Ref::System.CalloutException: You have uncommitted work pending. Please commit or rollback

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .