0

Hi I have below class and test class.

I am getting the "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" error.

Public class GE_OG_CalcCustomerCreditLimit
{
//AfterInsert
public void calcCustomerCreditLimitAfterInsert(List<Opportunity>  newOpptyList)
{

    List<Opportunity>  newOpptyList1= (List<Opportunity>)newOpptyList;
    Set<Id> setAccId = new Set<Id>();
    List<GE_HQ_CUST_CREDIT_HIST__c> lstCustCreditHistoryUpdate = new List<GE_HQ_CUST_CREDIT_HIST__c>();

    for (Opportunity Opp : newOpptyList1) 
    {


            if((Opp.StageName=='Active - Commit' || Opp.StageName=='Closed Won')&& (Opp.GE_ES_R_Status__c !=null)  && (Opp.GE_ES_R_Status__c != 'R0') && Opp.GE_HQ_Business_Tier1__c == 'Oil & Gas (O&G)')
            {
                if(Opp.AccountId != null)
                {
                setAccId.add(Opp.AccountId);
                }
            }

    } 
    if(!setAccId.isEmpty())
    {

        for(GE_HQ_CUST_CREDIT_HIST__c Cch: [select Id, GE_HQ_Acct_Cr_Limit__c, GE_HQ_Cr_Limit_Exceeded__c, GE_HQ_Tot_AR_s__c, GE_HQ_Acc__c from GE_HQ_CUST_CREDIT_HIST__c where GE_HQ_Acc__c in: setAccId])
        {
            GE_HQ_CUST_CREDIT_HIST__c CchUpdate = new GE_HQ_CUST_CREDIT_HIST__c(Id=Cch.Id);
            CchUpdate.GE_HQ_Acct_Cr_Limit__c = Cch.GE_HQ_Acct_Cr_Limit__c;
            lstCustCreditHistoryUpdate.add(CchUpdate);
        }
    }
    if(!lstCustCreditHistoryUpdate.isEmpty())
    {
        update lstCustCreditHistoryUpdate ;
    }




}
//afterInser closed

//for AfterUpdate
public void calcCustomerCreditLimitAfterUpdate(List<Opportunity>  newOpptyList,Map<Id,Opportunity>  OldMapOpportunity)
{
            List<Opportunity>  newOpptyList1= (List<Opportunity>)newOpptyList;

             Map<Id,Opportunity>  oldMapOpportunity1=(Map<Id,Opportunity>)  OldMapOpportunity; 


    Set<Id> setAccId = new Set<Id>();
    List<GE_HQ_CUST_CREDIT_HIST__c> lstCustCreditHistoryUpdate = new List<GE_HQ_CUST_CREDIT_HIST__c>();

    for (Opportunity Opp : newOpptyList1) 
    {


            Opportunity OppBeforeUpdate = oldMapOpportunity1.get(Opp.Id); 
            if(OppBeforeUpdate.Amount != Opp.Amount || OppBeforeUpdate.StageName != Opp.StageName || OppBeforeUpdate.GE_ES_R_Status__c != Opp.GE_ES_R_Status__c)
            {
                System.debug('In Update====');
                if(Opp.AccountId != null){setAccId.add(Opp.AccountId);}
            }        


    } 
    if(!setAccId.isEmpty())
    {

        for(GE_HQ_CUST_CREDIT_HIST__c Cch: [select Id, GE_HQ_Acct_Cr_Limit__c, GE_HQ_Cr_Limit_Exceeded__c, GE_HQ_Tot_AR_s__c, GE_HQ_Acc__c from GE_HQ_CUST_CREDIT_HIST__c where GE_HQ_Acc__c in: setAccId])
        {
            GE_HQ_CUST_CREDIT_HIST__c CchUpdate = new GE_HQ_CUST_CREDIT_HIST__c(Id=Cch.Id);
            CchUpdate.GE_HQ_Acct_Cr_Limit__c = Cch.GE_HQ_Acct_Cr_Limit__c;
            lstCustCreditHistoryUpdate.add(CchUpdate);
        }
    }
    if(!lstCustCreditHistoryUpdate.isEmpty())
    {
        update lstCustCreditHistoryUpdate ;
    }


}
// for AfterUpdate close
}

testclass:

@isTest
private class GEOGCalcCustomerCreditLimit_Test{
static testMethod void GEOGCalcCustomerCreditLimit_Test(){ 

    //User usr = new User(LastName='test user',Alias='tuser',DefaultGroupNotificationFrequency='N',DigestFrequency='N',Email='testuser@ge.com',Username='testuserr2@ge.com',EmailEncodingKey='UTF-8',LanguageLocaleKey='en_US',LocaleSidKey='IN',ProfileId= ProfId,TimeZoneSidKey='Asia/Kolkata',UserPermissionsCallCenterAutoLogin=false,UserPermissionsMarketingUser=false,UserPermissionsOfflineUser=false);
    //Insert usr;
    Account Acc = new Account();
    Acc.Name = 'Test account';
    Acc.BillingCountry = 'USA';
    Acc.ShippingCountry = 'USA';
    Acc.ShippingState = 'AT';
    Acc.ShippingCity = 'TestShipping City';
    Acc.ShippingStreet = 'Test Shipping Street';
    Acc.ShippingPostalCode = '101010';
    Acc.GE_HQ_Request_Status__c = 'CMF Approved';
    insert Acc;  

    GE_HQ_CUST_CREDIT_HIST__c Cch = new GE_HQ_CUST_CREDIT_HIST__c();
    Cch.GE_HQ_Acc__c = Acc.Id;
    Cch.GE_HQ_Acct_Cr_Limit__c = 2500000;
    Cch.GE_HQ_Cr_Limit_Remaining__c = 1000;
    Cch.GE_HQ_Tot_AR_s__c = 2000; 
    insert Cch;

 GE_Configuration_Setting__c cs= new GE_Configuration_Setting__c();
 cs.GE_OG_DTS_OpptyID__c=5490;
 cs.GE_PWNUC_Oppty_ID__c=5490;
 cs.GE_OGGS_Oppty_Id__c=5490;
 cs.GE_OG_Oppty_Id_Counter__c=5490;
 cs.GE_OG_PII_OIDCnt__c=5490;
 cs.GE_OG_SS_WSIDCntr__c=5490;
 cs.name='OG Counter';

 insert cs;



    GE_ES_Project__c prj = new GE_ES_Project__c();
    prj.Name = 'TestProject1';
    prj.GE_OG_SS_First_Oil_Date__c=Date.today();
    insert prj;


     Opportunity oppty = new Opportunity();
    oppty.RecordTypeId =[select id,name from recordtype where name like 'Grouped Flow' limit 1].id;
    oppty.Name= 'TestOppty1';
    oppty.GE_ES_Project__c = prj.Id;
    oppty.AccountId = Acc.Id;
    oppty.Type = 'Primary';
    oppty.StageName = 'Active - Commit';
    oppty.GE_ES_R_Status__c = 'R1 Submitted';
    oppty.GE_HQ_Business_Tier1__c = 'Oil & Gas (O&G)';
    oppty.CurrencyIsoCode = 'USD';
    oppty.GE_ES_Bid_Due_Date__c = Date.today();
    oppty.CloseDate= Date.today();
    oppty.Amount= 1000;
    oppty.GE_OG_TM_Commercial_Center__c= 'Bari';
    oppty.GE_OG_Stream__c = 'Core Services - Aberdeen';
    oppty.GE_OG_TM_Quote_Type__c = 'Budget';
    oppty.GE_OG_TM_Industrial_Sector__c = 'LNG';
    oppty.GE_OG_TM_Subsector__c = 'CCR';
    oppty.GE_OG_TM_Application_Center__c = 'Bari';
    oppty.GE_OG_TM_RFQ_Received_Date__c = Date.today();
    oppty.GE_OG_TM_Proposal_Assesment__c = 'Tech & Comm With Same Due Date';
    //oppty.GE_OG_Oppty_End_User__c = buyer.Id;
    oppty.End_User__c = Acc.Id;
    oppty.GE_ES_OPPTY_CM__c = 35;
    insert oppty;

    }

    static testMethod void GEOGCalcCustomerCreditLimit_Test1()
    { 

GE_Profile__c profile=new GE_Profile__c();
profile.name='GE_ES System Integration';
profile.Profile_SFDC_ID__c=[select id, name from Profile where  name = 'GE_ES System Integration' limit 1].id;
insert profile;

 Account Acc = new Account();
    Acc.Name = 'Test account';
    Acc.BillingCountry = 'USA';
    Acc.ShippingCountry = 'USA';
    Acc.ShippingState = 'AT';
    Acc.ShippingCity = 'TestShipping City';
    Acc.ShippingStreet = 'Test Shipping Street';
    Acc.ShippingPostalCode = '101010';
    Acc.GE_HQ_Request_Status__c = 'CMF Approved';
    insert Acc;  

    GE_HQ_CUST_CREDIT_HIST__c Cch = new GE_HQ_CUST_CREDIT_HIST__c();
    Cch.GE_HQ_Acc__c = Acc.Id;
    Cch.GE_HQ_Acct_Cr_Limit__c = 2500000;
    Cch.GE_HQ_Cr_Limit_Remaining__c = 1000;
    Cch.GE_HQ_Tot_AR_s__c = 2000; 
    insert Cch;

 GE_Configuration_Setting__c cs= new GE_Configuration_Setting__c();
 cs.GE_OG_DTS_OpptyID__c=5490;
 cs.GE_PWNUC_Oppty_ID__c=5490;
 cs.GE_OGGS_Oppty_Id__c=5490;
 cs.GE_OG_Oppty_Id_Counter__c=5490;
 cs.GE_OG_PII_OIDCnt__c=5490;
 cs.GE_OG_SS_WSIDCntr__c=5490;
 cs.name='OG Counter';

 insert cs;



    GE_ES_Project__c prj = new GE_ES_Project__c();
    prj.Name = 'TestProject1';
    prj.GE_OG_SS_First_Oil_Date__c=Date.today();
    insert prj;


     Opportunity oppty = new Opportunity();
    oppty.RecordTypeId =[select id,name from recordtype where name like 'Grouped Flow' limit 1].id;
    oppty.Name= 'TestOppty1';
    oppty.GE_ES_Project__c = prj.Id;
    oppty.AccountId = Acc.Id;
    oppty.Type = 'Primary';
    oppty.StageName = 'Active - Commit';
    oppty.GE_ES_R_Status__c = 'R1 Submitted';
    oppty.GE_HQ_Business_Tier1__c = 'Oil & Gas (O&G)';
    oppty.CurrencyIsoCode = 'USD';
    oppty.GE_ES_Bid_Due_Date__c = Date.today();
    oppty.CloseDate= Date.today();
    oppty.Amount= 1000;
    oppty.GE_OG_TM_Commercial_Center__c= 'Bari';
    oppty.GE_OG_Stream__c = 'Core Services - Aberdeen';
    oppty.GE_OG_TM_Quote_Type__c = 'Budget';
    oppty.GE_OG_TM_Industrial_Sector__c = 'LNG';
    oppty.GE_OG_TM_Subsector__c = 'CCR';
    oppty.GE_OG_TM_Application_Center__c = 'Bari';
    oppty.GE_OG_TM_RFQ_Received_Date__c = Date.today();
    oppty.GE_OG_TM_Proposal_Assesment__c = 'Tech & Comm With Same Due Date';
    //oppty.GE_OG_Oppty_End_User__c = buyer.Id;
    oppty.End_User__c = Acc.Id;
    oppty.GE_ES_OPPTY_CM__c = 35;
    insert oppty;

  test.starttest();

oppty.name= 'hai';
update oppty;

 test.stoptest();
    }

}
0

As salesforce documentation, you can not perform database (DML) to call before making external calls.

Source:Source

Check if you have any trigger that performs some external call.

  • This class is the related to trigger only . can you please tell me clearly – venkatesh May 21 '15 at 13:10
  • You could post the complete exception? – RAP_7 May 22 '15 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.