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
BobBob 

Case Currecny Trigger Error CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

I have a trigger that I created that is working fine, but when i try to deploy it to our production org, it will not allow me to and gives me a few errors on apex test classes that i know work fine. Any help as to why this trigger is making these test classes fail woud be greatly appreciated. The trigger and error information is below. Another issue is I seem to have a problem with these test classes in eclipse. Any test class that references a Case object has a red X near the case where there's a insert case with a message stating sytax error mismatched input expecting RCurly Bracket)). But these test worked fine in my older galilieo eclipse. I am using Eclipse IDE for Java Developers Version: Luna Service Release 1a (4.4.1)

Case Currency Trigger:
trigger Trigger_ChangeCaseCurrency on Case (before update) {
    Set<Id> accIds=new Set<Id>();
    for (Case cs: trigger.new){
        accIds.add(cs.AccountId);
    }
    Map<Id,Account> accMap=new Map<Id,Account>([SELECT id, OwnerId,currencyisocode,Shipping_Country__c FROM Account WHERE  Id IN :accIds]);
    for (Case cs : trigger.new){
        if(accMap.get(cs.AccountId).Shipping_Country__c == 'GBR' || accMap.get(cs.AccountId).Shipping_Country__c == 'IRL' ){
           //update currency from Account
            cs.currencyisocode=accMap.get(cs.AccountId).currencyisocode;
           //set the curreny to GBP
           cs.currencyisocode='GBP';
           cs.Reason='Other';
           
        }
    }
}

Test Classes:

Test_AssignWebCaseQueue
@isTest
private class Test_AssignWebCaseQueue {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        test.startTest();
        
        Account acc = new Account( Name ='Key West Services', Market_Segment__c='Finance Company', Customer_Type__c='Finance Company', Preferred_SP__c ='0017000000WitqP', Registered_for_Seminar__c=true );
      insert acc;
      
      acc.Name='Key West Services';
      acc.Market_Segment__c ='Finance Company';
      acc.Customer_Type__c='Finance Company';
      acc.Preferred_SP__c ='0017000000WitqP';
      update acc;
      
      Case cse = new Case( Status='Open', Origin='Parts Support - Web', Subject='Parts', Web_Case_Type__c='Part Support');
      insert cse;

      cse.Status = 'Open';
      cse.Origin = 'Parts Support - Web';
      cse.OwnerId='00G70000001JGIA';
      cse.Web_Case_Type__c='Part Support';
      update cse;
      
      cse.Status = 'Open';
      cse.Origin = 'Tech Support- Web';
      cse.OwnerId='00G70000001hJGH';
      cse.Web_Case_Type__c='Tech Support';
      update cse;

      test.stopTest();
    }
}

Test_CaseFileUpload
@isTest
public class Test_CaseFileUpload {

      static testMethod void testAttachments()
    {
        Case cse=new Case();
        insert cse;
        BL_CaseFileUpload controller=new BL_CaseFileUpload(new ApexPages.StandardController(cse));
 
        controller.fileName='Unit Test Attachment';
        controller.fileBody=Blob.valueOf('Unit Test Attachment Body');
        controller.uploadFile();
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:cse.id];
        System.assertEquals(1, attachments.size());
    }
}

Test_VFController_survey_oppsmyUnitTest

@isTest
private class Test_VFController_survey_opps {

    static testMethod void myUnitTest() {
        VFController_survey_opps sv = new VFController_survey_opps();
        sv.qte1 = 'Excellent';
        sv.qte2 = 'Excellent';
        sv.qte3 = 'Excellent';
        sv.qte4 = 'Excellent';
        sv.qte5 = 'Excellent';
        sv.qte6 = 'Excellent';
        sv.qte7 = 'Excellent';
        sv.qte8 = 'Excellent';
        sv.qte9 = 'Excellent';
        sv.qte10 = 'Excellent';
        sv.qte11 = 'Excellent';
        sv.qte12 = 'Excellent';
        sv.qte13 = 'Excellent';
        sv.qte14 = 'Excellent';       
        sv.company = 'Key West Services';
        sv.Address = '10 Trotter' ;
        sv.city = 'Milford' ;
        sv.Country = 'USA,CAN,MEX';
        sv.zip = '02703' ;
        sv.state= 'MA' ;
        sv.email = 'bpoliquin@cybexintl.com' ;
        sv.Phone = '508-533-4300' ;
        sv.Comments = 'Test Commment' ;
        sv.oppId = '123456' ;
        sv.survey = true;
        sv.caseid = '123456';
        sv.getItemc();
        sv.getItems();
        sv.getItems2();
        sv.getItems3();
        sv.getItemst();
        
       
       
               
        
        // insert a case
        Case cs = new Case();
        cs.Subject = 'test';
        cs.Reason = 'test reason';
       insert cs;
        //--
        
        sv.caseid = cs.id;    
               
        
         //insert account
        Account acc = new Account();
        acc.Name = 'Lorenco';
        acc.Market_Segment__c = 'Commercial';
        acc.Customer_Type__c = 'Hospitality';
        acc.Shipping_Country__c = 'USA';  
        
        
        insert acc;           
       
        
        //insert opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Lorenco';
        
        opp.Market_Segment__c = 'Commercial';
        opp.Customer_Type__c = 'Hospitality';
        opp.Lead_Source_Detail__c = 'Trade Show';
        opp.Direction__c = 'Incoming';
        opp.StageName= 'In Progress';
        opp.Original_Created_Date__c = date.today();
        opp.CloseDate = date.today();
        opp.Survey_Returned__c = true;
       
        opp.Id = opp.id;
        insert opp;
        
         Boolean good = True;
        System.assert(good);
    }
}



 
Best Answer chosen by Bob
Prosenjit Sarkar 7Prosenjit Sarkar 7

Hi Bob,
There are errors in both Trigger_ChangeCaseCurrencyand Test_AssignWebCaseQueue.
In Trigger_ChangeCaseCurrency you have not null checked your if condition. The trigger should be like this.
trigger Trigger_ChangeCaseCurrency on Case (before update) {
    Set<Id> accIds=new Set<Id>();
    for (Case cs: trigger.new){
        accIds.add(cs.AccountId);
    }
    Map<Id,Account> accMap=new Map<Id,Account>([SELECT id, OwnerId,currencyisocode,Shipping_Country__c FROM Account WHERE  Id IN :accIds]);
    for (Case cs : trigger.new){
        
        Account acc = accMap.get(cs.AccountId);
        if(acc != NULL && (acc.Shipping_Country__c == 'GBR' || acc.Shipping_Country__c == 'IRL' )){
         //update currency from Account
            cs.currencyisocode=accMap.get(cs.AccountId).currencyisocode;
           //set the curreny to GBP
           cs.currencyisocode='GBP';
           cs.Reason='Other';
           
        }
    }
}
Reason for the Exception : 
In your test class you have not input Shipping_Accress__c value for the test account and also have not put the AccountId into your case.
So accMap.get(cs.AccountId) returns NULL for the cases who dont have Accounts and your code was trying to get value of Shipping_Country__c from the NULL. Thats why you were getting these error.
I have modifed your test class. But those are optional (if you want to test it properly) unless the Shipping_Country__c is a formula field. . This is your modified test class.
@isTest
private class Test_AssignWebCaseQueue {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        test.startTest();
        
        Account acc = new Account( Name ='Key West Services', Market_Segment__c='Finance Company', Customer_Type__c='Finance Company', Preferred_SP__c ='0017000000WitqP', Registered_for_Seminar__c=true );
      insert acc;
      
      acc.Name='Key West Services';
      acc.Market_Segment__c ='Finance Company';
      acc.Customer_Type__c='Finance Company';
      acc.Preferred_SP__c ='0017000000WitqP';
      
      acc.Shipping_Country__c = 'GBR';
      update acc;
      
      Case cse = new Case( Status='Open', Origin='Parts Support - Web', Subject='Parts', Web_Case_Type__c='Part Support' AccountId=acc.Id);
      
      insert cse;

      cse.Status = 'Open';
      cse.Origin = 'Parts Support - Web';
      cse.OwnerId='00G70000001JGIA';
      cse.Web_Case_Type__c='Part Support';

      update cse;
      
      cse.Status = 'Open';
      cse.Origin = 'Tech Support- Web';
      cse.OwnerId='00G70000001hJGH';
      cse.Web_Case_Type__c='Tech Support';
      update cse;

      test.stopTest();
    }
}

 
 

All Answers

Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Bob,
Please do this before touching your codes, which are working fine according to you,
Login to your production org -> Setup -> Apex Test Execution -> Options -> Check Disable Parallel Apex Testing check box.
Now try to deploy your codes again.
If it not works, let me know, then we will debug your code.
Thanks :)
BobBob
The checkbox is now checked in both my sandbox and production. In all test above there is a common reference to line 8 of my case currency trigger. 
Error message:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_ChangeCaseCurrency: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.Trigger_ChangeCaseCurrency: line 8, column 1:

Line 8:
if(accMap.get(cs.AccountId).Shipping_Country__c == 'GBR' || accMap.get(cs.AccountId).Shipping_Country__c == 'IRL' )
Prosenjit Sarkar 7Prosenjit Sarkar 7

Hi Bob,
There are errors in both Trigger_ChangeCaseCurrencyand Test_AssignWebCaseQueue.
In Trigger_ChangeCaseCurrency you have not null checked your if condition. The trigger should be like this.
trigger Trigger_ChangeCaseCurrency on Case (before update) {
    Set<Id> accIds=new Set<Id>();
    for (Case cs: trigger.new){
        accIds.add(cs.AccountId);
    }
    Map<Id,Account> accMap=new Map<Id,Account>([SELECT id, OwnerId,currencyisocode,Shipping_Country__c FROM Account WHERE  Id IN :accIds]);
    for (Case cs : trigger.new){
        
        Account acc = accMap.get(cs.AccountId);
        if(acc != NULL && (acc.Shipping_Country__c == 'GBR' || acc.Shipping_Country__c == 'IRL' )){
         //update currency from Account
            cs.currencyisocode=accMap.get(cs.AccountId).currencyisocode;
           //set the curreny to GBP
           cs.currencyisocode='GBP';
           cs.Reason='Other';
           
        }
    }
}
Reason for the Exception : 
In your test class you have not input Shipping_Accress__c value for the test account and also have not put the AccountId into your case.
So accMap.get(cs.AccountId) returns NULL for the cases who dont have Accounts and your code was trying to get value of Shipping_Country__c from the NULL. Thats why you were getting these error.
I have modifed your test class. But those are optional (if you want to test it properly) unless the Shipping_Country__c is a formula field. . This is your modified test class.
@isTest
private class Test_AssignWebCaseQueue {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        test.startTest();
        
        Account acc = new Account( Name ='Key West Services', Market_Segment__c='Finance Company', Customer_Type__c='Finance Company', Preferred_SP__c ='0017000000WitqP', Registered_for_Seminar__c=true );
      insert acc;
      
      acc.Name='Key West Services';
      acc.Market_Segment__c ='Finance Company';
      acc.Customer_Type__c='Finance Company';
      acc.Preferred_SP__c ='0017000000WitqP';
      
      acc.Shipping_Country__c = 'GBR';
      update acc;
      
      Case cse = new Case( Status='Open', Origin='Parts Support - Web', Subject='Parts', Web_Case_Type__c='Part Support' AccountId=acc.Id);
      
      insert cse;

      cse.Status = 'Open';
      cse.Origin = 'Parts Support - Web';
      cse.OwnerId='00G70000001JGIA';
      cse.Web_Case_Type__c='Part Support';

      update cse;
      
      cse.Status = 'Open';
      cse.Origin = 'Tech Support- Web';
      cse.OwnerId='00G70000001hJGH';
      cse.Web_Case_Type__c='Tech Support';
      update cse;

      test.stopTest();
    }
}

 
 
This was selected as the best answer
BobBob
Thank you so much for your help. I was trying to figure out how to add the condition  if(acc != NULL but couldn't figure out how to add it. Thank you for updating my test class too!
Prosenjit Sarkar 7Prosenjit Sarkar 7
Most welcome Bob :)