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
TerminusbotTerminusbot 

Trigger Deploy Fail: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: List Index out of bounds: 0

I have created a trigger on a custom object and a test class to migrate it. I have over 95% code coverage and when I deploy to Production I get this error. 
 
10:38:50:047 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateNewOpp: execution of AfterInsert

10:38:47:000 FATAL_ERROR Trigger.CreateNewOpp: line 38, column 1: []

It can't find an ID I am querying for even though I know it exists in Production as well as Sandbox. Here is my trigger. I have set the line 38 in the trigger to BOLD and Underline so you can see it.  
 
trigger CreateNewOpp on Policy__c (after insert) {

System.debug('Create New Opp Fired');   

//List of Opportunities to Create and Associate Policy    
List<Opportunity> createOpps = new List<Opportunity>();

    for (Policy__c polInfo : Trigger.new) {
      
      //Create Opportunity 
      Opportunity createOpp = new Opportunity();


      //Policy Number 
      String polNumber = polInfo.Name;
   
      if (polNumber.containsIgnoreCase('APP')){
           System.debug('Found New Policy Opportunity' + polInfo.Name);
           
           //Find Owner and Type of Business Account 
           List<Account> acctId = new List<Account>([Select OwnerId, 
                                                            RecordTypeId 
                                                       From Account 
                                                      Where Id =:polInfo.account__c LIMIT 1]); 
           
           List<RecordType> lookupRecId = new List<RecordType>([Select Id,
                                                                       Name 
                                                                  From RecordType 
                                                                 Where Id=:acctId.get(0).RecordTypeId LIMIT 1]);
          
          List<RecordType> recTypeList = new List<RecordType>();
           

           if (lookupRecId.get(0).Name == 'Individual') {
              recTypeList = [Select Id 
                               From RecordType 
                              Where Name = 'Commercial Lines' LIMIT 1];
              System.debug('Inside IF Record Type' + recTypeList.get(0).Id);
               
           } else {
               recTypeList = [Select Id 
                                From RecordType 
                               Where Name = 'Personal Lines' LIMIT 1];
              System.debug('Inside IF Record Type' + recTypeList.get(0).Id);  
           }
              System.debug('Outside IF Record Type' + recTypeList.get(0).Id);  
              System.debug('Opporunity Account' + polInfo.account__c);

           //Set New Opportunity Values 
           createOpp.Name            = 'New ' + polInfo.Coverage_Detail__c + ' Opportunity'; 
           createOpp.AccountId       = polInfo.account__c; 
           createOpp.RecordTypeId    = recTypeList.get(0).Id;
           createOpp.StageName       = 'Marketing';
           createOpp.CloseDate       = polInfo.Effective_Date__c;
           createOpp.Amount          = polInfo.EstPremAmt__c;
           createOpp.RefSagittaID__c = polInfo.SagittaID__c;
           createOpp.OwnerId         = acctId.get(0).OwnerId; 
           createOpps.add(createOpp);
           
       }


        if (createOpps.size()>=1){
           
            for (Opportunity opp : createOpps) {
                try {
                    System.debug('Create these Opps' + createOpps);
                    insert opp;
        	    } catch(DmlException e) {
                    System.debug('The following exception has occurred during update: ' + e.getMessage());
        	    }
        	}
        }
          

    }
    
}

Here is my test class. I have set (seeAllData=true). 
 
@isTest(seeAllData=true) 
private class TestNewOpp {
	
	@isTest static void test_method_one() {

		DateTime dT = System.now();
		Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

		//Lookup RecordType Id
		RecordType typeOfRecord = [Select Id
							         From RecordType
						            Where Name = 'Individual' LIMIT 1];

		//Lookup House User
		User acctOwner = [Select Id
				            From User
					       Where Sagitta_User_Code__c = 'HO' LIMIT 1];

		
		// Create Account With Parameters
		// list Accounts to Update 
		Account myAccount      = new Account(); 
		myAccount.Name         = 'Test Account';
		myAccount.Email__c     = 'testing@rampartinsurance.com';
		myAccount.Phone        = '5555555555';
		myAccount.RecordTypeId = typeOfRecord.Id;  
		myAccount.OwnerId      = acctOwner.Id; 
		myAccount.Preferred_Method_of_Contact__c = 'Email';
 		insert myAccount; 



		Account latestAccount = [Select Id, 
										RecordTypeId
								   From Account 
								  Where Id=: myAccount.Id LIMIT 1];


		//Create Coverage for Test
		Coverages__c testCov = new Coverages__c(); 
		testCov.Name = 'TEST1';
		testCov.Description__c = 'Testing Coverage Detail';
		insert testCov;

		Coverages__c latestCov = [Select Id 
								    From Coverages__c 
								   Where Id =: testCov.Id LIMIT 1];

		

		//Create Policy
		Policy__c myPolicy  = new Policy__c();
		myPolicy.Name       = 'APP12345';
		myPolicy.Account__c = latestAccount.Id;
		myPolicy.EstPremAmt__c = 10000;
		myPolicy.Effective_Date__c = myDate;
		myPolicy.SagittaID__c =  '10000';
		myPolicy.Coverages__c = latestCov.Id;
		insert myPolicy;




	}
	
	
	
}

 
Best Answer chosen by Terminusbot
Amit Chaudhary 8Amit Chaudhary 8
Please deploy "Commercial Lines" record type also in production

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please deploy "Commercial Lines" record type also in production
This was selected as the best answer
TerminusbotTerminusbot
The record types were 'Commercial Insurance" in Production rather than 'Commercial Lines' which is in Sandbox(weird). Thanks for reminding me to double check the record types. I glanced at it but didn't see the difference the first time.