0

I am getting an error of

List index out of bounds :0

Please guide me on how to fix it as I have checked the list as null in the code but I am still getting the error.

trigger PAM_AssignOwnerfromOrganization on Process_Adherence_Monitoring__c (before insert) {

    Set<id> accountId = new Set<id>();
    system.debug('accountId-->'+accountId);

    List<Contact> conList = new List<Contact>();

    For(Process_Adherence_Monitoring__c pam : Trigger.new)
    {
      if(pam.Organization_Name__c!= Null)
      accountId.add(pam.Organization_Name__c);   
    }

    conList = [select id,Name,Community_Profile__c,Inactive_Contact__c FROM Contact WHERE AccountId IN :accountId AND Community_Profile__c INCLUDES  ('PAM') AND Inactive_Contact__c = False];
    List<User> userlist = new List<User>();

    userlist = [Select Id FROM User Where ContactId != Null AND ContactId =: conList[0].id];


    system.debug('conList-->'+conList);
    system.debug('userlist-->'+userlist);

      for(Process_Adherence_Monitoring__c pam : Trigger.new)
      {
        if(!userlist.isEmpty()){
          pam.OwnerId = userlist[0].Id; 
        }
        else
        {
            pam.OwnerId = UserInfo.getUserId(); 
        }  
      }  
}
  • Error on which line? – d_k Oct 19 '16 at 8:12
  • You have a problem here - ContactId =: conList[0].id – Raul Oct 19 '16 at 8:13
  • Error Line - no 17! @d_k – manchesterunited Oct 19 '16 at 8:14
  • What is the issue? @RahulSharma – manchesterunited Oct 19 '16 at 8:14
  • Your query of contact isn't getting any record – d_k Oct 19 '16 at 8:15
0

Please check whether the list has any value before using a value in a particular index of the list

if(conList.size()>0)
{
userlist = [Select Id FROM User Where ContactId != Null AND ContactId  = :conList[0].id];
}
0

You need to just just this 2 Lines of code.

if(conList.size() > 0 && conList != null) {
     userlist = [Select Id FROM User Where ContactId != Null AND ContactId =:         conList[0].id];
}

Further when you will going to access any List or Map please check with ".size() > 0 && != null". It'll never throw this type of error.

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.