function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prasanth sfdcprasanth sfdc 

Rest API test class issue, kindly help for callout exception

Hi,
  I am getting this error if i run the test class. Kindly help me to solve this, i tried for around 4 hours to solve this but, not to do.

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
 
@isTest
private class ProjectCalloutServiceTest {

 @testSetup
  public static void setupData() {
         // ProjectCalloutServiceTestData testdata = new ProjectCalloutServiceTestData();
         account acc = new account(name='testing acc');
        insert acc;
        opportunity opp1 = new opportunity(name='testing',closedate=date.today(),amount=200,accountid=acc.id,StageName='closed won');
        insert opp1;
        
        ServiceTokens__c token= new ServiceTokens__c(name='ProjectServiceToken',Token__c='a9b50c09-a94b-4826-9aa6-64407574cad6');
        insert token;
        
  
  }
   @istest 
   public static void testPostCallout() {
        list<id> oppidslst = new list<id>();
        for(opportunity opp:[select id,name from opportunity where accountid!=null])
        {
        oppidslst.add(opp.id);
        }
        
        
         Test.startTest();
      Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());  
                       
        
    ProjectCalloutService.postOpportunityToPMS(oppidslst);
    Test.stopTest();
    }
}

 
VinayVinay (Salesforce Developers) 
Hi Prasanth,

Review below links which can help you.

https://help.salesforce.com/articleView?id=000326129&type=1&mode=1
https://salesforce.stackexchange.com/questions/46438/unit-test-you-have-uncommitted-work-pending-please-commit-or-rollback-before-c/92873
https://patlatus.wordpress.com/2019/08/06/salesforce-bug-in-tests-system-calloutexception-you-have-uncommitted-work-pending-please-commit-or-rollback-before-calling-out/

KI: https://success.salesforce.com/issues_view?id=a1p3A000000ATF1QAO

Thanks,
Vinay Kumar
prasanth sfdcprasanth sfdc
Hi Vijay, Thank you for reply. It just got fixed. The problem is the same criteria record is going for execution and same record I am calling for call-out. That's why this error happened.
VinayVinay (Salesforce Developers) 
Hi Prasanth,

Good to know issue is fixed now.

Please mark as Best Answer if above information was helpful so that it can help others in the future.

Thanks,
Vinay Kumar