0

I got error in test class.

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContractsTrigger: execution of AfterUpdate

caused by: System.ListException: List index out of bounds: 0

Class.ContractsTriggerMethods.createDisbursements: line 39, column 1 Trigger.ContractsTrigger: line 5, column 1: [] Class.ContractsTriggerTest.myUnitTest: line 10, column 1

Test class:

@isTest
private class ContractsTriggerTest {

    static testMethod void myUnitTest() {
      
      List<Collection__c> colls = new List<Collection__c>();
        
        Contracts__c contractRec = new Contracts__c(Full_Name__c='1234');
        
        insert contractRec;
        
        Collection__c coll1 = new Collection__c();
        coll1.Contract_ID__c = contractRec.Id;
        coll1.Payment_Amount__c = 10000;
        insert coll1;
        
        Collection__c coll2 = new Collection__c();
        coll2.Contract_ID__c = contractRec.Id;
        coll2.Payment_Amount__c = 10000;
        insert coll2;
        
        coll1.Amount_Received__c = 5000;
        colls.add(coll1);
        coll2.Amount_Received__c = 5000;
        colls.add(coll2);
        
        update colls;
        
        colls = new List<Collection__c>();
        
        coll1.Amount_Received__c = 6000;
        colls.add(coll1);
        coll2.Amount_Received__c = 6000;
        colls.add(coll2);
        
        update colls;
        
        Collection__c collection=new Collection__c();
        collection.Contract_ID__c=contractRec.Id;
        collection.Cheque_Number_Reference_Number_Sub__c='Test';
        collection.Method_of_Payment__c='Master';
        insert collection;
        
        Collection__c collection1=new Collection__c();
        collection1.Contract_ID__c=contractRec.Id;
        collection1.Cheque_Number_Reference_Number_Sub__c='Test';
        collection1.Method_of_Payment__c='Master';
        update collection1;
        
        Disbursement__c dis= new Disbursement__c();
        dis.Contract_ID__c=contractRec.Id;
        dis.Cheque_Number_Reference_Number_Sub__c=collection.Cheque_Number_Reference_Number_Sub__c;
        dis.Method_of_Payment__c=collection.Method_of_Payment__c;
        insert dis;
        
        Disbursement__c dis1= new Disbursement__c();
        dis1.Contract_ID__c=contractRec.Id;
        dis1.Cheque_Number_Reference_Number_Sub__c=collection1.Cheque_Number_Reference_Number_Sub__c;
        dis1.Method_of_Payment__c=collection1.Method_of_Payment__c;
        update dis1;
    }
}

Apex class:

public with sharing class ContractsTriggerMethods {

   private static boolean updatedRec = true;
   public static boolean updatedOnce()
    {
        if(updatedRec)
        {
            updatedRec = false;
            return true;
        }
        else
        {
            return updatedRec;
        }
    } 
   public static void createDisbursements(List<Contracts__c> newRecs, Map<Id,Contracts__c> oldMap){
     
     
     
     List<Disbursement__c> disbursements = new List<Disbursement__c>();
     
     Contracts__c oldRec;
     
     Disbursement__c disburse;
     
     Decimal changedValue = 0,differenceAmount =0;
     
     Date receivedDate;
     
     Map<Id,Integer> childCountMap = new Map<Id,Integer>();
     
     Map<Id,Date> amountReceivedDateByContract = new Map<Id,Date>();
     map<id, Collection__c> contactIdCollectionRec = new map<id, Collection__c>();
     for(Contracts__c con: [SELECT id,(SELECT id FROM Disbursements__r),(SELECT id, Amount_Received_Date__c,Method_of_Payment__c,Cheque_Number_Reference_Number_Sub__c From Collections__r WHERE Amount_Received_Date__c != null ORDER BY Amount_Received_Date__c DESC) FROM Contracts__c WHERE Id IN: newRecs]){
       childCountMap.put(con.Id,con.Disbursements__r.size());
       if(!con.Collections__r.isEmpty())
         amountReceivedDateByContract.put(con.Id,con.Collections__r[0].Amount_Received_Date__c);

         contactIdCollectionRec.put(con.id, con.Collections__r[0]);
         
     }
     
     system.debug('newRecs'+newRecs);
     for(Contracts__c con: newRecs){
       
       Map<String,Schema.RecordTypeInfo> ContractRecordTypeInfo = Schema.SObjectType.Contracts__c.getRecordTypeInfosByName(); 
           Id recordTypeId = ContractRecordTypeInfo.get('Upgrade').getRecordTypeId();              
        Id recordTypeId1 = ContractRecordTypeInfo.get('Original').getRecordTypeId();  
        
       oldRec = oldMap.get(con.id);
         system.debug('contractRTnew'+con.RecordTypeId);
         system.debug('contractdeficitamountnew'+con.Deficit_Amount__c);
         
       if(oldRec.Amount_Received_per__c < con.Amount_Received_per__c  && con.RecordTypeId == recordTypeId1 && con.Deficit_Amount__c == 0 && (con.Amount_Received_per__c >50  || (con.Amount_Received_per__c == 50 && childCountMap.get(con.Id) == 0)))
       {
       system.debug('contractRTnew'+con.RecordTypeId);
         system.debug('contractdeficitamountnew'+con.Deficit_Amount__c);
         
         try
         
         {
           
           Decimal multiplicant = Decimal.valueOf(Label.Disbursement_Investment_Return);
           
           if(childCountMap.get(con.Id) == 0)
             differenceAmount = con.Amount_Received__c;
           else
             differenceAmount = (con.Amount_Received__c - oldRec.Amount_Received__c);
             
           changedValue = (differenceAmount * (multiplicant/100));
           
           receivedDate = amountReceivedDateByContract.get(con.Id) != null? amountReceivedDateByContract.get(con.Id):null;
           
           disburse = new Disbursement__c(
                            Amount_Payable__c = changedValue,
                            Contract_ID__c = con.Id,
                            Method_of_Payment_Collections__c   = contactIdCollectionRec.get(con.id).Method_of_Payment__c,
                            Cheque_Number_Reference_Number__c   = contactIdCollectionRec.get(con.id).Cheque_Number_Reference_Number_Sub__c,
                            Payment_Amount__c = differenceAmount);
       
           system.debug('----disburse : '+disburse);
           if(receivedDate != null)
             disburse.Disbursement_Date__c = receivedDate.addDays(14);
           
           disbursements.add(disburse);
           
         }catch(TypeException e){
           con.addError('Incorrect value of Investment return % Please contact your system administrator');
         }
       }  
         else if(con.RecordTypeId == recordTypeId && con.Deficit_Amount__c == 0)
       {
          system.debug('contractRTelseif'+con.RecordType);
          system.debug('contractdeficitamountelseif'+con.Deficit_Amount__c);
         try
         
         {
           
           Decimal multiplicant = Decimal.valueOf(Label.Disbursement_Investment_Return);
           
           if(childCountMap.get(con.Id) == 0)
             differenceAmount = con.Amount_Received__c;
           else
             differenceAmount = (con.Amount_Received__c - oldRec.Amount_Received__c);
             
           changedValue = (differenceAmount * (multiplicant/100));
           
           receivedDate = amountReceivedDateByContract.get(con.Id) != null? amountReceivedDateByContract.get(con.Id):null;
           
           disburse = new Disbursement__c(Amount_Payable__c = changedValue,Contract_ID__c = con.Id);
           
           if(receivedDate != null)
             disburse.Disbursement_Date__c = receivedDate.addDays(14);
            disbursements.add(disburse);
           
         }catch(TypeException e){
           con.addError('Incorrect value of Investment return % Please contact your system administrator');
         }
       }  
       }
    
    
     if(!disbursements.isEmpty())
      Insert disbursements;
   }
}
2
  • The error you have posted is incomplete I believe. There should be a stack trace after that which will point to the actual error. Please post the full error details. Also check if you are missing any mandatory details.
    – manjit5190
    Oct 12, 2015 at 7:25
  • I have Posted all error manjit.
    – user24524
    Oct 12, 2015 at 7:27

1 Answer 1

3

This does not correspond to the line number 39 you have included, but this code is wrong:

   if(!con.Collections__r.isEmpty())
     amountReceivedDateByContract.put(con.Id,con.Collections__r[0].Amount_Received_Date__c);

     contactIdCollectionRec.put(con.id, con.Collections__r[0]);

in that the second reference to Collections__r[0] is not within the if(!con.Collections__r.isEmpty()) guard and so is a potential source of the "System.ListException: List index out of bounds: 0" error.

Perhaps all that is needed is the addition of {} to include it.

Logic that references the first element of a collection will only work if you can guarantee there is never more than one element. At the very least a comment should be added to explain why the logic is sufficient.

1
  • Nice catch. it easy to miss.
    – manjit5190
    Oct 12, 2015 at 7:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .