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
Chenjie LiChenjie Li 

apex trigger test class works in sandbox but not in production

Hi fellow developers:

I am working on an apex trigger that will assign cases to certain groups/users based on the requirements from my boss.

What is really bothering me now is that when i ran the test class in sandbox it worked perfectly but when trying to deploy in production it gives me the following error:

First exception on row 2 with id 5003a00000xQsOrAAK; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Stack Trace: Class.TestCaseTrigger.TestCaseStatusChange: line 101, column 1


the trigger code:
trigger CaseTrigger on Case (before update) {
    for(Case c:Trigger.New){
        // grab the version of this case before the update 
        Case oldcase = Trigger.oldMap.get(c.ID);
        // access the status before the update for this case 
        string old_status = oldcase.status;
        // access the status after the update for this case
        string new_status = c.status;
        if(old_status == 'Closed' && new_status != 'Closed'){
          // we only need to handle this scenario, for now
          boolean previous_onwer_isactive = [SELECT owner.isActive FROM case WHERE id = :oldcase.Id LIMIT 1].owner.isactive;
            if(previous_onwer_isactive==FALSE){
                // create the rules based on "Case Assignment Rule"
                string case_subject = [SELECT subject FROM case WHERE id = :c.id LIMIT 1].subject;
                string case_origin = [SELECT origin FROM case WHERE id = :c.id LIMIT 1].origin;
                string assigned_to_name; // a variable used to locate its corresponding ID later on
                string assigned_to_id;
                
                if((case_subject=='HCRC TR Files'||case_subject=='HCRC FY Files') && case_origin == 
                   'Email - Enrollment Process Intelligence'){
                       assigned_to_name = 'HCRC Files';
                }
                else if(case_origin == 'Email - Enrollment Process Intelligence'){
                    assigned_to_name = 'Enrollment Process Intelligence';
                }
                else if(case_origin == 'Email - Enrollment Reporting'){
                    assigned_to_name = 'Enrollment Reporting';
                }
                else if(case_origin == 'Email - Enrollment Support'){
                    assigned_to_name = 'Tim';
                }
                else if(case_origin =='Email - Elevate'){
                    assigned_to_name = 'Elevate';
                }
                else if(case_origin =='Email - Graduate Admission'){
                    assigned_to_name = 'Graduate Admission Case Queue';
                }
                else if(case_origin =='Email - Graduate Recruitment'){
                    assigned_to_name = 'Graduate Recruitment';
                }
                else if(case_origin =='Email - Graduate Visit'){
                    assigned_to_name = 'Graduate Visit Case Queue';
                }
                else if(case_origin =='Email - GR Admission Partner'){
                    assigned_to_name = 'GR Admission Partner';
                }               
                else if(case_origin =='Email - UG Admission Partner'){
                    assigned_to_name = 'UG Admission Partner';
                }
                else if(case_origin =='Olark Live Chat' || case_origin == 'Graduate SnapEngage Chat'){
                    assigned_to_name = 'Graduate Recruitment';
                }
                else if(case_origin =='Web'){
                    assigned_to_name = 'Enrollment Process Intelligence';
                }
                else if(case_origin =='Email - Undergraduate Admission'){
                    assigned_to_name = 'Undergraduate Admission';
                }
                else if(case_origin =='Email - Undergraduate International'){
                    assigned_to_name = 'Undergraduate International';
                }
                else if(case_origin =='Email - Undergraduate Visit'){
                    assigned_to_name = 'Undergraduate Visit';
                }                    
                else if(case_origin =='Form Assembly - Project Request'){
                    assigned_to_name = 'Enrollment Process Intelligence';
                }
                else if(case_origin =='Email - Undergraduate Transfer'){
                    assigned_to_name = 'Undergraduate Transfer';
                }                
                else if(case_origin =='Email-Academic Honesty'){
                    assigned_to_name = 'Academic Honesty';
                }                
                else{
                    c.addError('did not find a queue/person to assign to!');
                    break;
                }
                
                // only Tim is not a queue(Group object), so handled seperately
                if(assigned_to_name !='Tim'){
                    assigned_to_id = [SELECT id FROM group where name =:assigned_to_name LIMIT 1].id;
                }
                else{
                     assigned_to_id = '005j000000COiwwAAD';
                }
               
                c.OwnerId = assigned_to_id;

                                
            }
        }
        
        
    }
}

trigger test class:
@isTest
public class TestCaseTrigger {
    @isTest static void TestCaseStatusChange(){
        // set up the data
        // create an user(owner)
        
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];

        System.runAs (thisUser){
        
        User test_user = new User(ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
                          LastName = 'James',
                          Email = 'cli112@gmail.com',
                          Username = 'newlbj@gmail.com',
                          CompanyName = 'TEST',
                          Title = 'title',
                          Alias = 'alias',
                          TimeZoneSidKey = 'America/Los_Angeles',
                          EmailEncodingKey = 'UTF-8',
                          LanguageLocaleKey = 'en_US',
                          LocaleSidKey = 'en_US',
                          isactive =  FALSE
                         );
        
        
        insert test_user;
            
        // create a case
        string test_user_id = [SELECT id from user where username='newlbj@gmail.com' limit 1].id;
        // 

        List<Case> testCases = new List<Case>();
        Case test_case1 = New Case(origin='Email - Enrollment Process Intelligence', subject = 'HCRC TR Files',
         status='Closed', IIT_Project_Name__c = 'Test Case1',ownerid = test_user_id);
        testCases.add(test_case1);
        Case test_case2 = New Case(origin='Email - Enrollment Process Intelligence', subject = 'different subject', 
            status='Closed', IIT_Project_Name__c = 'Test Case2',ownerid = test_user_id);
        testCases.add(test_case2);
        Case test_case3 = New Case(origin='Email - Enrollment Reporting', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case3',ownerid = test_user_id);
        testCases.add(test_case3);
        //Case test_case4 = New Case(origin='Email - Enrollment Support', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case4',ownerid = test_user_id);
        testCases.add(test_case4);
        Case test_case5 = New Case(origin='Email - Elevate', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case5',ownerid = test_user_id);
        testCases.add(test_case5);
        Case test_case6 = New Case(origin='Email - Graduate Admission', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case6',ownerid = test_user_id);
        testCases.add(test_case6);
        Case test_case7 = New Case(origin='Email - Graduate Recruitment', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case7',ownerid = test_user_id);
        testCases.add(test_case7);
        Case test_case8 = New Case(origin='Email - Graduate Visit', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case8',ownerid = test_user_id);
        testCases.add(test_case8);
        Case test_case9 = New Case(origin='Email - GR Admission Partner', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case9',ownerid = test_user_id);
        testCases.add(test_case9);
        Case test_case10 = New Case(origin='Email - UG Admission Partner', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case10',ownerid = test_user_id);
        testCases.add(test_case10);
        Case test_case11 = New Case(origin='Olark Live Chat', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case11',ownerid = test_user_id);
        testCases.add(test_case11);
        Case test_case12 = New Case(origin='Web', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case12',ownerid = test_user_id);
        testCases.add(test_case12);
        Case test_case13 = New Case(origin='Email - Undergraduate Admission', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case13',ownerid = test_user_id);
        testCases.add(test_case13);
        Case test_case14 = New Case(origin='Email - Undergraduate International', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case14',ownerid = test_user_id);
        testCases.add(test_case14);
        Case test_case15 = New Case(origin='Email - Undergraduate Visit', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case15',ownerid = test_user_id);
        testCases.add(test_case15);
        Case test_case16 = New Case(origin='Form Assembly - Project Request', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case16',ownerid = test_user_id);
        testCases.add(test_case16);
        Case test_case17 = New Case(origin='Email - Undergraduate Transfer', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case17',ownerid = test_user_id);
        testCases.add(test_case17);
        Case test_case18 = New Case(origin='Email-Academic Honesty', subject = 'HCRC TR Files', 
            status='Closed', IIT_Project_Name__c = 'Test Case18',ownerid = test_user_id);
        testCases.add(test_case18);

        insert testCases;
            
        
        // select all the test cases and then update them to status as "In Progress"
        List<Case> newstatusCases = [SELECT origin, subject, Status, ownerid FROM case WHERE IIT_Project_Name__c LIKE 'Test Case%'];
            
        for(Case c : newstatusCases){
            c.Status = 'In Progress';
        }
            
        system.debug('Size of newstatusCases: ');
        system.debug(newstatusCases.size());

        update newstatusCases;
        
            
        Test.startTest();

        // test1 : 'Email - Enrollment Process Intelligence' --> 'HCRC Files'
        Case inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case1' LIMIT 1];
        string test1_owner_id = inserted_test_case.OwnerId;
        
        test2 : 'Email - Enrollment Process Intelligence' --> 'Enrollment Process Intelligence'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case2' LIMIT 1];
        string test2_owner_id = inserted_test_case.OwnerId;
        
        // test3 : 'Email - Enrollment Reporting' --> 'Enrollment Reporting'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case3' LIMIT 1];
        string test3_owner_id = inserted_test_case.OwnerId;
            
            
        test4 : 'Email - Enrollment Support' --> 'Tim'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case4' LIMIT 1];
        string test4_owner_id = inserted_test_case.OwnerId;
        
        // test5 : 'Email - Elevate' --> 'Email - Elevate'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case5' LIMIT 1];
        string test5_owner_id = inserted_test_case.OwnerId;
        
        // test6 : 'Email - Graduate Admission' --> 'Graduate Admission Case Queue'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case6' LIMIT 1];
        string test6_owner_id = inserted_test_case.OwnerId;

        // test7 : 'Email - Graduate Recruitment' --> 'Graduate Recruitment'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case7' LIMIT 1];
        string test7_owner_id = inserted_test_case.OwnerId;

        // test8 : 'Email - Graduate Visit' --> 'Graduate Visit Case Queue'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case8' LIMIT 1];
        string test8_owner_id = inserted_test_case.OwnerId;            
 
        // test9 : 'Email - GR Admission Partner' --> 'GR Admission Partner'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case9' LIMIT 1];
        string test9_owner_id = inserted_test_case.OwnerId; 
            
        // test10 : 'Email - UG Admission Partner' --> 'UG Admission Partner'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case10' LIMIT 1];
        string test10_owner_id = inserted_test_case.OwnerId;

        // test11 : 'Olark Live Chat' --> 'Graduate Recruitment'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case11' LIMIT 1];
        string test11_owner_id = inserted_test_case.OwnerId;

        // test12 : 'Web' --> 'Enrollment Process Intelligence'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case12' LIMIT 1];
        string test12_owner_id = inserted_test_case.OwnerId;            

        // test13 : 'Email - Undergraduate Admission' --> 'Undergraduate Admission'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case13' LIMIT 1];
        string test13_owner_id = inserted_test_case.OwnerId;            

        // test14 : 'Email - Undergraduate International' --> 'Undergraduate International'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case14' LIMIT 1];
        string test14_owner_id = inserted_test_case.OwnerId;            

        // test15 : 'Email - Undergraduate Visit' --> 'Undergraduate Visit'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case15' LIMIT 1];
        string test15_owner_id = inserted_test_case.OwnerId;            
        
        // test16 : 'Form Assembly - Project Request' --> 'Enrollment Process Intelligence'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case16' LIMIT 1];
        string test16_owner_id = inserted_test_case.OwnerId;              
            
        // test17 : 'Email - Undergraduate Transfer' --> 'Undergraduate Transfer'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case17' LIMIT 1];
        string test17_owner_id = inserted_test_case.OwnerId;              
            
        // test18 : 'Email-Academic Honesty' --> 'Academic Honesty'
        inserted_test_case = [SELECT origin, subject, status, ownerid FROM case WHERE IIT_Project_Name__c = 'Test Case18' LIMIT 1];
        string test18_owner_id = inserted_test_case.OwnerId;            
            
        
            
        Test.stopTest();

            
            
            
            
            
        // Tests validations

        string test1_correct_owner_id = [SELECT id FROM group where name = 'HCRC Files' LIMIT 1].id;
        System.assertEquals(test1_owner_id,test1_correct_owner_id);

        string test2_correct_owner_id = [SELECT id FROM group where name = 'Enrollment Process Intelligence' LIMIT 1].id;
        System.assertEquals(test2_owner_id,test2_correct_owner_id);
            
        string test3_correct_owner_id = [SELECT id FROM group where name = 'Enrollment Reporting' LIMIT 1].id;
        System.assertEquals(test3_owner_id,test3_correct_owner_id);
            
        string test4_correct_owner_id = '005j000000COiwwAAD';
        System.assertEquals(test4_owner_id,test4_correct_owner_id);

        string test5_correct_owner_id = [SELECT id FROM group where name = 'Elevate' LIMIT 1].id;
        System.assertEquals(test5_owner_id,test5_correct_owner_id);
        
        string test6_correct_owner_id = [SELECT id FROM group where name = 'Graduate Admission Case Queue' LIMIT 1].id;
        System.assertEquals(test6_owner_id,test6_correct_owner_id);

        string test7_correct_owner_id = [SELECT id FROM group where name = 'Graduate Recruitment' LIMIT 1].id;
        System.assertEquals(test7_owner_id,test7_correct_owner_id);

        string test8_correct_owner_id = [SELECT id FROM group where name = 'Graduate Visit Case Queue' LIMIT 1].id;
        System.assertEquals(test8_owner_id,test8_correct_owner_id);

        string test9_correct_owner_id = [SELECT id FROM group where name = 'GR Admission Partner' LIMIT 1].id;
        System.assertEquals(test9_owner_id,test9_correct_owner_id);

        string test10_correct_owner_id = [SELECT id FROM group where name = 'UG Admission Partner' LIMIT 1].id;
        System.assertEquals(test10_owner_id,test10_correct_owner_id);            

        string test11_correct_owner_id = [SELECT id FROM group where name = 'Graduate Recruitment' LIMIT 1].id;
        System.assertEquals(test11_owner_id,test11_correct_owner_id);            
            
        string test12_correct_owner_id = [SELECT id FROM group where name = 'Enrollment Process Intelligence' LIMIT 1].id;
        System.assertEquals(test12_owner_id,test12_correct_owner_id);
        
        string test13_correct_owner_id = [SELECT id FROM group where name = 'Undergraduate Admission' LIMIT 1].id;
        System.assertEquals(test13_owner_id,test13_correct_owner_id); 
            
        string test14_correct_owner_id = [SELECT id FROM group where name = 'Undergraduate International' LIMIT 1].id;
        System.assertEquals(test14_owner_id,test14_correct_owner_id);              
            
        string test15_correct_owner_id = [SELECT id FROM group where name = 'Undergraduate Visit' LIMIT 1].id;
        System.assertEquals(test15_owner_id,test15_correct_owner_id);              
        
        string test16_correct_owner_id = [SELECT id FROM group where name = 'Enrollment Process Intelligence' LIMIT 1].id;
        System.assertEquals(test16_owner_id,test16_correct_owner_id);              
            
        string test17_correct_owner_id = [SELECT id FROM group where name = 'Undergraduate Transfer' LIMIT 1].id;
        System.assertEquals(test17_owner_id,test17_correct_owner_id);              
            
        string test18_correct_owner_id = [SELECT id FROM group where name = 'Academic Honesty' LIMIT 1].id;
        System.assertEquals(test18_owner_id,test18_correct_owner_id);  
             
            // new_case.OwnerId;
    }
    }
}

 
Best Answer chosen by Chenjie Li
Andrew GAndrew G
Hi
I second Chenjie's observations regarding the hard coded Id and the placement of SOQL within loops.

To fix both try something like:
User tim = [SELECT Id FROM User WHERE FirstName = 'Tim' AND LastName = 'SomelastName'];

List<String> caseIds = new List<String>();
for(Case c : Trigger.new) {
  caseIds.add(c.Id);
}

Map<Id,Boolean> caseOwnerStatusMap = new Map<Id,Boolean>();
for(Case c : [SELECT Id, Owner.IsActive FROM Case WHERE Id IN :caseIds]){
 caseOwnerStatusMap.put(c.Id, c.Owner.IsActive)
}

Map<String, Id> groupNameIdMap = new Map<String,Id>();
for (Group g : [SELECT id, Name FROM Group ]){
    groupNameIdMap.put(g.Name, g.Id);
}


for(Case c:Trigger.new){
    // grab the version of this case before the update
    Case oldcase = Trigger.oldMap.get(c.Id);
    // access the status before the update for this case
    String old_status = oldcase.Status;
    // access the status after the update for this case
    String new_status = c.Status;
    if(old_status == 'Closed' && new_status != 'Closed'){
        // we only need to handle this scenario, for now

        if(caseOwnerStatusMap.get(c.Id) == false){
            // create the rules based on "Case Assignment Rule"
            String case_subject = c.Subject;
            String case_origin = c.Origin;
            String assigned_to_name; // a variable used to locate its corresponding ID later on
            String assigned_to_id;
/*............. the other code which seems long winded .............*/

             
 // only Tim is not a queue(Group object), so handled seperately
                if(assigned_to_name !='Tim'){
                    assigned_to_id = groupNameIdMap.get(assigned_to_Name);
                }
                else{
                     assigned_to_id = tim.Id;
                }
Other notes would be you seem to want to be a bit long winded in that you are writing things to strings for testing when the field could be tested directly - example 
// access the status before the update for this case
    String old_status = oldcase.Status;
    // access the status after the update for this case
    String new_status = c.Status;
    if(old_status == 'Closed' && new_status != 'Closed'){

////why not a single line....

if(oldCase.Status == 'Closed' && c.Status!='Closed') {

Also, now if we use the Map created in the suggest code above, we could do:
else if(case_origin == 'Email - Enrollment Process Intelligence'){
    c.OwnerId = groupNameIdMap.get('Enrollment Process Intelligence');

//instead of 

else if(case_origin == 'Email - Enrollment Process Intelligence'){
  assigned_to_name = 'Enrollment Process Intelligence';
//and then the second step of 
 // only Tim is not a queue(Group object), so handled seperately 
if(assigned_to_name !='Tim'){ 
    assigned_to_id = groupNameIdMap.get(assigned_to_Name); 
} else{ 
    assigned_to_id = tim.Id; 
}


Additionally, when setting up a lot of test data, i would consider is something like the following helps simplify how you are creating your test data:
Looping is "cleaner" to read when setting up test data, especially when dealing with multiple test records of the same object.
@isTest
public class TestCaseTrigger {
    @isTest static void TestCaseStatusChange(){
        // set up the data
        // create an user(owner)

        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];

        System.runAs (thisUser){

            User test_user = new User(ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
                    LastName = 'James',
                    Email = 'cli112@gmail.com',
                    Username = 'newlbj@gmail.com',
                    CompanyName = 'TEST',
                    Title = 'title',
                    Alias = 'alias',
                    TimeZoneSidKey = 'America/Los_Angeles',
                    EmailEncodingKey = 'UTF-8',
                    LanguageLocaleKey = 'en_US',
                    LocaleSidKey = 'en_US',
                    IsActive =  FALSE
            );
            insert test_user;

            List<String> caseOrigin = new List<String>();
            caseOrigin.add('Email - Enrollment Process Intelligence');
            caseOrigin.add('Email - Elevate');
            caseOrigin.add('Email - Enrollment Reporting');
            caseOrigin.add('Email - Enrollment Support');
            caseOrigin.add('Email - GR Admission Partner');
            caseOrigin.add('Email - Graduate Admission');
            caseOrigin.add('Email - Graduate Recruitment');
            caseOrigin.add('Email - Graduate Visit');
            caseOrigin.add('Email - UG Admission Partner');
            caseOrigin.add('Email - Undergraduate International');
            caseOrigin.add('Email - Undergraduate Transfer');
            caseOrigin.add('Email - Undergraduate Visit');
            caseOrigin.add('Email');
            caseOrigin.add('Email-Academic Honesty');
            caseOrigin.add('Form Assembly - Project Request');
            caseOrigin.add('Olark Live Chat');
            caseOrigin.add('Web');

            List<Case> testCases = new List<Case>();
            for(Integer i = 0; i < caseOrigin.size(); i++) {
                Case testCase = new Case( Subject = 'HCRC TR Files',
                        IIT_Project_Name__c = 'Test Case'+i,
                        Origin = caseOrigin[i],
                        Status = 'Closed',
                        OwnerId = test_user.Id);
                testCases.add(testCase);
            }

            insert testCases;

            // select all the test cases and then update them to status as "In Progress"
            List<Case> newstatusCases = [SELECT Origin, Subject, Status, OwnerId FROM Case WHERE IIT_Project_Name__c LIKE 'Test Case%' ORDER BY Origin];

            for(Case c : newstatusCases){
                c.Status = 'In Progress';
            }

            System.debug('Size of newstatusCases: ');
            System.debug(newstatusCases.size());

            update newstatusCases;

With the test data, if we continually call the inserted data in an expected sorted ORDER, then we can make some assumptions regarding the asserts we will perform.
Example

List<Case> newstatusCases = [SELECT Origin, Subject, Status, OwnerId FROM Case WHERE IIT_Project_Name__c LIKE 'Test Case%' ORDER BY Origin];

System.assertEquals(caseOrigin[0], newStatusCases[0].Origin);
System.assertEquals(groupNameIdMap.get('Email - Enrollment Process Intelligence'),newStatusCases[0].OwnerId);


Hope the above helps / provides some illumination.


Cheers
Andrew

All Answers

Chenjie LiChenjie Li
BTW i was thinking maybe it was because i hardcoded one of the scenarios, which is case4, but it turns out that even if I comment this case out, it still gives me the same error
Prafull G.Prafull G.
Hi Chenjie - The issue is with trigger code. Quick observations, Hard coded user id at line 84 causing issue.
Additionally, trigger code is not optimized as it have SOQL inside for loop with is not good practice. Please refer link (https://developer.salesforce.com/page/Apex_Code_Best_Practices)and let us know if you have any further issue/question.


 
Andrew GAndrew G
Hi
I second Chenjie's observations regarding the hard coded Id and the placement of SOQL within loops.

To fix both try something like:
User tim = [SELECT Id FROM User WHERE FirstName = 'Tim' AND LastName = 'SomelastName'];

List<String> caseIds = new List<String>();
for(Case c : Trigger.new) {
  caseIds.add(c.Id);
}

Map<Id,Boolean> caseOwnerStatusMap = new Map<Id,Boolean>();
for(Case c : [SELECT Id, Owner.IsActive FROM Case WHERE Id IN :caseIds]){
 caseOwnerStatusMap.put(c.Id, c.Owner.IsActive)
}

Map<String, Id> groupNameIdMap = new Map<String,Id>();
for (Group g : [SELECT id, Name FROM Group ]){
    groupNameIdMap.put(g.Name, g.Id);
}


for(Case c:Trigger.new){
    // grab the version of this case before the update
    Case oldcase = Trigger.oldMap.get(c.Id);
    // access the status before the update for this case
    String old_status = oldcase.Status;
    // access the status after the update for this case
    String new_status = c.Status;
    if(old_status == 'Closed' && new_status != 'Closed'){
        // we only need to handle this scenario, for now

        if(caseOwnerStatusMap.get(c.Id) == false){
            // create the rules based on "Case Assignment Rule"
            String case_subject = c.Subject;
            String case_origin = c.Origin;
            String assigned_to_name; // a variable used to locate its corresponding ID later on
            String assigned_to_id;
/*............. the other code which seems long winded .............*/

             
 // only Tim is not a queue(Group object), so handled seperately
                if(assigned_to_name !='Tim'){
                    assigned_to_id = groupNameIdMap.get(assigned_to_Name);
                }
                else{
                     assigned_to_id = tim.Id;
                }
Other notes would be you seem to want to be a bit long winded in that you are writing things to strings for testing when the field could be tested directly - example 
// access the status before the update for this case
    String old_status = oldcase.Status;
    // access the status after the update for this case
    String new_status = c.Status;
    if(old_status == 'Closed' && new_status != 'Closed'){

////why not a single line....

if(oldCase.Status == 'Closed' && c.Status!='Closed') {

Also, now if we use the Map created in the suggest code above, we could do:
else if(case_origin == 'Email - Enrollment Process Intelligence'){
    c.OwnerId = groupNameIdMap.get('Enrollment Process Intelligence');

//instead of 

else if(case_origin == 'Email - Enrollment Process Intelligence'){
  assigned_to_name = 'Enrollment Process Intelligence';
//and then the second step of 
 // only Tim is not a queue(Group object), so handled seperately 
if(assigned_to_name !='Tim'){ 
    assigned_to_id = groupNameIdMap.get(assigned_to_Name); 
} else{ 
    assigned_to_id = tim.Id; 
}


Additionally, when setting up a lot of test data, i would consider is something like the following helps simplify how you are creating your test data:
Looping is "cleaner" to read when setting up test data, especially when dealing with multiple test records of the same object.
@isTest
public class TestCaseTrigger {
    @isTest static void TestCaseStatusChange(){
        // set up the data
        // create an user(owner)

        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];

        System.runAs (thisUser){

            User test_user = new User(ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
                    LastName = 'James',
                    Email = 'cli112@gmail.com',
                    Username = 'newlbj@gmail.com',
                    CompanyName = 'TEST',
                    Title = 'title',
                    Alias = 'alias',
                    TimeZoneSidKey = 'America/Los_Angeles',
                    EmailEncodingKey = 'UTF-8',
                    LanguageLocaleKey = 'en_US',
                    LocaleSidKey = 'en_US',
                    IsActive =  FALSE
            );
            insert test_user;

            List<String> caseOrigin = new List<String>();
            caseOrigin.add('Email - Enrollment Process Intelligence');
            caseOrigin.add('Email - Elevate');
            caseOrigin.add('Email - Enrollment Reporting');
            caseOrigin.add('Email - Enrollment Support');
            caseOrigin.add('Email - GR Admission Partner');
            caseOrigin.add('Email - Graduate Admission');
            caseOrigin.add('Email - Graduate Recruitment');
            caseOrigin.add('Email - Graduate Visit');
            caseOrigin.add('Email - UG Admission Partner');
            caseOrigin.add('Email - Undergraduate International');
            caseOrigin.add('Email - Undergraduate Transfer');
            caseOrigin.add('Email - Undergraduate Visit');
            caseOrigin.add('Email');
            caseOrigin.add('Email-Academic Honesty');
            caseOrigin.add('Form Assembly - Project Request');
            caseOrigin.add('Olark Live Chat');
            caseOrigin.add('Web');

            List<Case> testCases = new List<Case>();
            for(Integer i = 0; i < caseOrigin.size(); i++) {
                Case testCase = new Case( Subject = 'HCRC TR Files',
                        IIT_Project_Name__c = 'Test Case'+i,
                        Origin = caseOrigin[i],
                        Status = 'Closed',
                        OwnerId = test_user.Id);
                testCases.add(testCase);
            }

            insert testCases;

            // select all the test cases and then update them to status as "In Progress"
            List<Case> newstatusCases = [SELECT Origin, Subject, Status, OwnerId FROM Case WHERE IIT_Project_Name__c LIKE 'Test Case%' ORDER BY Origin];

            for(Case c : newstatusCases){
                c.Status = 'In Progress';
            }

            System.debug('Size of newstatusCases: ');
            System.debug(newstatusCases.size());

            update newstatusCases;

With the test data, if we continually call the inserted data in an expected sorted ORDER, then we can make some assumptions regarding the asserts we will perform.
Example

List<Case> newstatusCases = [SELECT Origin, Subject, Status, OwnerId FROM Case WHERE IIT_Project_Name__c LIKE 'Test Case%' ORDER BY Origin];

System.assertEquals(caseOrigin[0], newStatusCases[0].Origin);
System.assertEquals(groupNameIdMap.get('Email - Enrollment Process Intelligence'),newStatusCases[0].OwnerId);


Hope the above helps / provides some illumination.


Cheers
Andrew

This was selected as the best answer
Chenjie LiChenjie Li
Hi Prafull, thanks for your advice, but after replacing hardcoded part with SOQL version, i still got the same error...
Chenjie LiChenjie Li
Hi Andrew, thanks for your advice, but after replacing hardcoded part with SOQL version, i still got the same error.. What's so bizarre to me is that it works fine in sandbox. I think I didnt hardcode anything in my triggertest besides create user and cases....
Chenjie LiChenjie Li
nvm, I figured it out.... I didnt specify the type of the group, after add "type='Queue' " in SOQL it worked!