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
Gaurav AgnihotriGaurav Agnihotri 

Trigger Test Class error "CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY"

I am creating a test class for a trigger on Quote. Below is the trigger:
trigger updateOpptyAccount on Quote (before insert,before update) {
    for (Quote QuoteInLoop : Trigger.new){
        /*****************************
         * Created by Gaurav Agnihotri 07/25/2015
         * Updates Price Book and 
         * Quote Accounts
         * Inserting new records in 
         * Quote Account
         * ***************************/
       // if(QuoteInLoop.Opportunity.name != '.'){
        Boolean sMainAccount=QuoteInLoop.Main_Account__c;
        String sMainAccountId=QuoteInLoop.AccountId;
        String sQuoteAccountId=QuoteInLoop.Pelco_Account_Name__c;
        String sQuotePriceBook=QuoteInLoop.PriceBook2Id;
        String sQuoteId=QuoteInLoop.Id;
        String sOptyId=QuoteInLoop.OpportunityId;
        String currencyCode;
        String currencyNumber;
        //String quoteAccountID;
        Decimal exchangeRateForQuote;  
        //checking if price book is attached to the quote
        //if no price book exist than defaulting the standard price book
        if(sQuotePriceBook == null)
        {
            QuoteInLoop.Pricebook2Id =[SELECT Id FROM Pricebook2 Limit 1].Id;
        }
       
        //if pelco quote account is null
        if (sQuoteAccountId == null)
        {
            QuoteInLoop.Pelco_Account_Name__c=QuoteInLoop.AccountId;
            sQuoteAccountId=QuoteInLoop.AccountId;
        }
        //if pelco quote account is not null
        if (sQuoteAccountId != null)
        {    
            if(sMainAccountId <>sQuoteAccountId)
              {
               //make sure pelco account is not added to opportunity
               Integer iPelAccnt1=[SELECT count() FROM Pelco_Account__c WHERE AccountName__c = :sQuoteAccountId AND OpportunityName__c =:sOptyId ];
               if(iPelAccnt1 == 0)
                 {
                      // Inserting a new Pelco Account
                      String sAccountName=[SELECT Name FROM Account WHERE Id = :sQuoteAccountId].Name;
                      List <Pelco_Account__c > PelcoAccountLst =new List <Pelco_Account__c>();
                      Pelco_Account__c PelAcc1= new Pelco_Account__c();
                      PelAcc1.OpportunityName__c=sOptyId;
                      PelAcc1.AccountName__c=sQuoteAccountId;
                      PelAcc1.Name=sAccountName;
                      PelcoAccountLst.add(PelAcc1);
                      insert PelcoAccountLst;  
                  }//closing if for iPelAccnt1 
             }//end if
        }//end if 
      /*********************************
       * Created by Ron 09/04/2015
       * Update Exchange rate
       * 
       * *******************************/     
      

        
        //sQuoteAccountId = QuoteInLoop.Pelco_Account_Name__c;
        /*Account quoteAccount; 
        quoteAccount = [SELECT Currency_Code__c,Currency_Number__c FROM Account where ID = :sQuoteAccountId];
        currencyCode = quoteAccount.Currency_Code__c;
        currencyNumber = quoteAccount.Currency_Number__c;
        */
        currencyCode=[SELECT Currency_Code__c FROM Account where ID = :sQuoteAccountId LIMIT 1].Currency_Code__c;
        CurrencyNumber=[SELECT Currency_Number__c FROM Account where ID = :sQuoteAccountId LIMIT 1].Currency_Number__c;

        exchangeRateForQuote = [SELECT Exchange_Rate__c FROM Exchange_Rate__c Where Currency_Code__c = :currencyCode AND Currency_Number__c = :currencyNumber LIMIT 1].Exchange_Rate__c;
       
       if(exchangeRateForQuote == NULL){
           exchangeRateForQuote = [SELECT Exchange_Rate__c FROM Exchange_Rate__c WHERE Currency_Code__c = :currencyCode AND Default_Rate_for_Currency__c = TRUE LIMIT 1].Exchange_Rate__c;
       }
       
       QuoteInLoop.Exchange_Rate__c = exchangeRateForQuote;
       QuoteInLoop.Currency_Code_of_Account__c = currencyCode;
      
     }//closing forLoop
 }//closing trigger updateOpptyAccount

Here is the test class:
@istest
private class TestUpdateOptyAccount {
    static testMethod void TestOptyUpdate() {
        //Get pricebook Id
        Id PBId=test.getStandardPricebookId();
        //Create Account a
        Account a = new Account();
    	a.Name = 'Ritzy';
        a.Currency_Code__c='EUR';
        a.Currency_Number__c='2';
    	insert a;
        system.debug('Account a ID='+a.Id);
        
        //Create Account b
        Account b=new Account();
        b.Name='Testb';
        b.Currency_Code__c='EUR';
        b.Currency_Number__c='3';
        insert b;
        system.debug('Account b ID='+b.Id);
        
        //Create Opportunity
        Opportunity opty=new Opportunity();
		opty.Name='Test Opportunity';
        opty.StageName='Prospecting';
        opty.CloseDate=date.valueof('2015-09-10');
        opty.AccountId=a.Id;
        //opty.Pelco_Account_Name__c=PA.id;
		Insert opty; 
         system.debug('Opty Id='+Opty.Id);
        
        //Create pelco account
       /* Pelco_Account__c PA =new Pelco_Account__c();
        PA.Name='Ritzy';
        PA.AccountName__c=b.Id;
        PA.OpportunityName__c=Opty.id;
        Insert PA;
         system.debug('Pelco Account Id='+PA.id);
        */
         //Insert Quote
        Quote q =new Quote();
         q.name='Test Quote';
         q.opportunityId=opty.id;
         q.Pricebook2Id=PBId;
         //q.Pelco_Account_Name__c=a.Id;
        insert q;
        system.debug('qUOTE Id='+q.id);
        
        //insert Exchange Rate
        Exchange_Rate__c ER= new Exchange_Rate__c();
        ER.Currency_Code__c='USD';
        ER.Currency_Description__c='US DOLLARS DELIV';
        ER.Currency_Number__c='0';
        ER.Exchange_Rate__c=1.0;
        ER.Default_Rate_for_Currency__c=false;
        insert ER;
        
    }//end Method 1  

}

I am getting an error message when I am trying to create quote data 
 
16:45:17:934 EXCEPTION_THROWN [46]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateOpptyAccount: execution of BeforeInsert
Any Suggestions?

 
Best Answer chosen by Gaurav Agnihotri
Amit Chaudhary 8Amit Chaudhary 8
Please try to above SeeAllData = True because in this case your test class may be fail while production deployment.
Please try to create all Lookup record in test class and use that record only I hope that will resolve your issue.

Thanks,
Amit Chaudhary
 

All Answers

shashi lad 4shashi lad 4
This is happening because, you are trying to insert Quote and that makes updateopptyAccount to fire which is causing Pelco_Account__c insert. Please read more on this error message. You can twick your logic or try using "after insert" instead "before".

hope this helps

thanks
shashi
Amit Chaudhary 8Amit Chaudhary 8
Option 1:- In Setup's "Apex Test Execution" page there is an "Options" button that includes a "Disable Apex Parallel Testing" checkbox. Checking that will probably avoid the problem.

Option 2:- Please check below post
http://salesforce.stackexchange.com/questions/25047/test-class-for-a-trigger-failure-cannot-insert-update-activate-entity

I hope this will help u

Thanks
Amit Chaudhary
Gaurav AgnihotriGaurav Agnihotri
Thanks, Shashi and Amit.

Amit, 
I was able to resolve the issue by making (SeeAllData=true). I am still having some issue when I create data for Quote without Account Quote( A lookup field). However, my code coverage is 91% and I will come back to it later.

Thanks you for your help as always.

-Gaurav
Amit Chaudhary 8Amit Chaudhary 8
Please try to above SeeAllData = True because in this case your test class may be fail while production deployment.
Please try to create all Lookup record in test class and use that record only I hope that will resolve your issue.

Thanks,
Amit Chaudhary
 
This was selected as the best answer