0

I am Getting this type of error:

Error:Apex trigger FieldUpdate caused an unexpected exception, contact your administrator: FieldUpdate: execution of AfterUpdate caused by: System.ListException: List index out of bounds: 14: Trigger.FieldUpdate: line 17, column 1

My Code:

trigger FieldUpdate on Account (before insert, after update) 
{  
Integer nextSize;
List<String> pickListValuesIntoList  = new  List<String>();
List <Schema.PicklistEntry> pickListValues = Account.Status_c__c.getDescribe().getPicklistValues();
for(Account Acc: trigger.new) 
{   
    for(Schema.PicklistEntry pi: pickListValues)
    {  
        pickListValuesIntoList.add(pi.getLabel());  
    }
    system.debug('pickListValuesIntoList:'+pickListValuesIntoList); 
    if(Acc.Status_c__c!=trigger.oldmap.get(Acc.id).Status_c__c && trigger.oldmap.get(Acc.id).Status_c__c!=null )
    {
        for(Integer nextSize=1;nextSize<pickListValues.size();nextSize++)
        {
            if(Acc.Status_c__c==pickListValuesIntoList[nextSize+1])
            {
                if(trigger.oldmap.get(acc.id).Status_c__c==pickListValuesIntoList[nextSize])
                {
                    Acc.Status_c__c=pickListValuesIntoList[nextSize+1];
                }
                else
                {
                    Acc.Status_c__c.adderror('Account status cant change'); 
                }
            }
        }
    }
}
}

Help me on this how to reslove it!

Thanks.

  • 2
    why you have kept " for(Schema.PicklistEntry pi: pickListValues) " for loop inside the " for(Account Acc: trigger.new) " for loop . any specific Reason ? – NITHESH K Aug 29 '17 at 7:38
  • 2
    And one more thing is you have used "before insert" in trigger , Before insert does contain trigger.oldmap. Look at where to used old and new map, developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – NITHESH K Aug 29 '17 at 7:42
  • @NITHESHK to use Adderror method i have done like that. – salesforce Aug 29 '17 at 7:43
  • 2
    @ NITHESH K perfect comment , we should not use like that, for(Schema.PicklistEntry pi: pickListValues) " for loop inside the " for(Account Acc: trigger.new) " for loop , trigger.oldMap is not used in before insert trigger, make sure to modify the code – SFDC Aug 29 '17 at 7:46
  • 2
    @salesforce also add little brief description about your code, what you want to achieve in this code and when you want (while inserting or updating) ? – NITHESH K Aug 29 '17 at 7:48
1

Simplify your code as below, But i have no idea about your logic going to do.

trigger FieldUpdate on Account (before insert, after update) {  
    Integer nextSize;
    List<String> pickListValuesIntoList  = new  List<String>();
    List <Schema.PicklistEntry> pickListValues = Account.Status_c__c.getDescribe().getPicklistValues();
    for(Schema.PicklistEntry pi: pickListValues) 
        pickListValuesIntoList.add(pi.getLabel());  
    if (Trigger.isInsert) {
        // your insert trigger.
    }
    if (Trigger.isUpdate) {              
        for(Account Acc: trigger.new) {           
            if(Acc.Status_c__c!=trigger.oldmap.get(Acc.id).Status_c__c && trigger.oldmap.get(Acc.id).Status_c__c!=null ) {                
                for(Integer nextSize=0;nextSize<pickListValues.size();nextSize++)   {                    
                    if(Acc.Status_c__c==pickListValuesIntoList[nextSize])  {
                        if(trigger.oldmap.get(acc.id).Status_c__c==pickListValuesIntoList[nextSize])
                        { Acc.Status_c__c=pickListValuesIntoList[nextSize];}
                        else
                        {  Acc.Status_c__c.adderror('Account status cant change'); }
                    }
                }
            }
        }
    }
}
  • My trigger fuctinalty is selecting picklist values one by one.now List index out of bounds: have been sloved.but picklist values are not going in series like a->b->c->d. it is saving first value only – salesforce Aug 29 '17 at 8:23
  • 1
    { Acc.Status_c__c=pickListValuesIntoList[nextSize]; Break ;} Just add break statement – NITHESH K Aug 29 '17 at 9:03
  • Break statement is worked for you ? – NITHESH K Aug 29 '17 at 9:13
  • 1
    No bro i have done like this for(Integer nextSize=0;nextSize<pickListValues.size()-1;nextSize++). – salesforce Aug 29 '17 at 10:00
  • 1
    Okay.... Still i am wearied why -1 is need..Any way work fine right, Good. – NITHESH K Aug 29 '17 at 10:03
1

On the specific error, this code loops right up to the last element in the array and then indexes one element beyond the end in [nextSize+1]:

for(Integer nextSize=1;nextSize<pickListValues.size();nextSize++)
{
    if(Acc.Status_c__c==pickListValuesIntoList[nextSize+1])

so you need to rethink that logic.

(As people have commented, there are other problems. Also maps could be used to avoid the loop inside a loop which can cause performance/governor limit problems.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.