+ Start a Discussion
NishanNishan 

Help with List index out of bounds error

Hi everyone,

                     I have a code to update a certain object called Field_Executive based on another object called inventory. It is working fine when the lookup field to FE is filled. Here is the code

trigger FEupdate on Inventory__c (before update) 
 {
   for(Inventory__c inv : trigger.new)
    {    
         Inventory__c oldValue = Trigger.oldMap.get(inv.ID);
         Decimal noOfItems = oldValue.Remaining_Stock__c - inv.Remaining_Stock__c;
         
       Id feId = inv.Field_Executive__c;
       List<Field_Executive__c> fe = [Select Id, Item1__c, Item2__c, Item3__c, No1__c, No2__c, No3__c
                                      From Field_Executive__c where Id =:feId ];  
     
     if( noOfItems != 0 && !fe.isEmpty() )
      {
       String itemname = inv.Name;
       
       if(fe[0].Item1__c != itemname && fe[0].Item2__c != itemname && fe[0].Item3__c != itemname )
          {
             if(fe[0].Item1__c == null)
              {
               fe[0].Item1__c = itemname;
               fe[0].No1__c = noOfItems;
              }

             else if(fe[0].Item2__c == null)
              {
               fe[0].Item2__c = itemname;
               fe[0].No2__c = noOfItems;
              }

             else if(fe[0].Item3__c == null)
              {
               fe[0].Item3__c = itemname;
               fe[0].No3__c = noOfItems;
              }
                            
          }
       
       
       else if(fe[0].Item1__c == itemname)
         {
          fe[0].No1__c = fe[0].No1__c + noOfItems;
         }

       else if(fe[0].Item2__c == itemname)
         {
          fe[0].No2__c = fe[0].No2__c + noOfItems;
         }   
         
       else if(fe[0].Item3__c == itemname)
         {
          fe[0].No3__c = fe[0].No3__c + noOfItems;
         }               
      
          update fe[0];
     }     
      
  }
  
    
}

 When the lookup field is null, I am not able to change the Remaining_Stock__field.  Can someone please suggest a solution. I tried  !isEmpty function but still no progress

bob_buzzardbob_buzzard

I'm struggling to understand what you are trying to achieve here.  If the lookup field is not present you won't be able to update the number of items, as you need to update the number on the related Field_Executive__c object.  If the lookup is null, you don't know what Field_Executive__c object is related and thus you can't update it.

 

That said, I can't see where your code would throw a list index out of bounds exception, as you have checked the size of the list before entering the code block that accesses the first element.

souvik9086souvik9086

Try it like this

 

trigger FEupdate on Inventory__c (before update)
{
for(Inventory__c inv : trigger.new)
{
Inventory__c oldValue = Trigger.oldMap.get(inv.ID);
Decimal noOfItems = oldValue.Remaining_Stock__c - inv.Remaining_Stock__c;

Id feId = inv.Field_Executive__c;
List<Field_Executive__c> fe = new List<Field_Executive__c>();
fe = [Select Id, Item1__c, Item2__c, Item3__c, No1__c, No2__c, No3__c
From Field_Executive__c where Id =:feId ];

if( noOfItems != 0 && !fe.isEmpty() )
{
String itemname = inv.Name;

if(fe[0].Item1__c != itemname && fe[0].Item2__c != itemname && fe[0].Item3__c != itemname )
{
if(fe[0].Item1__c == null)
{
fe[0].Item1__c = itemname;
fe[0].No1__c = noOfItems;
}

else if(fe[0].Item2__c == null)
{
fe[0].Item2__c = itemname;
fe[0].No2__c = noOfItems;
}

else if(fe[0].Item3__c == null)
{
fe[0].Item3__c = itemname;
fe[0].No3__c = noOfItems;
}

}


else if(fe[0].Item1__c == itemname)
{
fe[0].No1__c = fe[0].No1__c + noOfItems;
}

else if(fe[0].Item2__c == itemname)
{
fe[0].No2__c = fe[0].No2__c + noOfItems;
}

else if(fe[0].Item3__c == itemname)
{
fe[0].No3__c = fe[0].No3__c + noOfItems;
}

update fe[0];
}

}


}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

NishanNishan

I need to update Remaining_Stock in two situations

 

1) When the item is issued to a Field Executive which is the function of this trigger.

 

2) When new stock arrives (where I don't need this trigger to execute because the lookup field to FE will be Null when I add new stock)

 

 

            So from your answer I guess I used isEmpty() function correctly ?

bob_buzzardbob_buzzard

You did use it correctly.  The code that accesses the list should only be executed when there is at least one value in the list.

NishanNishan

Hi,

     It is solved. There was another apex trigger with the same problem and the compiler was pointing to it. Sorry for the screw up.