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
RajnisfRajnisf 

System.ListException: List index out of bounds..can sm1 help

trigger eee on Contact (before insert, after update) {
            
        boolean b1 = false;
        public list<Contact> allstatuses = new List<Contact>();
        public list<string> adds = new List<string>();
        public Set<id> cidd = new Set<id>();
        public List<string> allstats = new List<string>();

                        allstats.add('A');
                        allstats.add('B ');
                                         

 for(Contact c: Trigger.New) {
 
        cidd.add(c.AccountId);
    }
    

     
    
      allstatuses  = ([Select c.CrowdTorch_Contact_Status__c From Contact c where id in : cidd]);
     
         for(String c1 : allstats)
         
         {
                 
         for(contact cc : allstatuses)
     
         {
            
                       
         if(c1  == cc.CrowdTorch_Contact_Status__c)
         adds.add(c1);     
         b1 =  true;
         break;
         
        }
         if(b1 == true)
         {
         break;          
         }
       
      }
      public list<Account> a1 = new List<Account>([select a1.id, a1.CT_Account_Status__c from Account a1]);
      
        for(Account aa : a1)
        
        {
        
        aa.CT_Account_Status__c = adds.get[0]; //System.ListException: List index out of bounds
        
        }
   
   }

Best Answer chosen by Admin (Salesforce Developers) 
Raj.ax1558Raj.ax1558

Hello, 

 

try this code - 

 

for(Account aa : a1)
 {

if(adds.size() > 0)

{

     aa.CT_Account_Status__c = adds[0];

}

}

 

 

You have used list of string. No need of writing a gets. Directly you can get values.

 

If you find this as solution, Please marked as solution for others help in a same queary.

 

Thank You, 

Raj Jha

All Answers

Rahul SharmaRahul Sharma
This exception occurs, whenever you are referring to 0th record of a list which is not having any records.
You should check the size of list before referring to 0th index directly.
RajnisfRajnisf

i used

 

if(!adds.isEmpty())  in above code

 

 

now its not giving error...

 

but trigger is not working though.

 

i want to use Group by accountid in this query..but i dont want to use any aggregate function...how can i achieve this...

My requirement is i have to compare one field's value on each contact of Account and need to update the higher value on Account.

 

eg

Account
has 3 contacts

c1.field = 1

c2.field = 3

c3.field = 5

 

then

 

Account.field ahould be 5

 

 

allstatuses  = ([Select c.CrowdTorch_Contact_Status__c From Contact c where id in : cidd]);

Raj.ax1558Raj.ax1558

Hello, 

 

try this code - 

 

for(Account aa : a1)
 {

if(adds.size() > 0)

{

     aa.CT_Account_Status__c = adds[0];

}

}

 

 

You have used list of string. No need of writing a gets. Directly you can get values.

 

If you find this as solution, Please marked as solution for others help in a same queary.

 

Thank You, 

Raj Jha

This was selected as the best answer
izayizay

You can query all the contacts for an account using a query like this:

 

myAccounts = [SELECT Id, Name (SELECT Id, CrowdTorch_Contact_Status__c FROM Contact) From Account WHERE Id IN = :cidd];

 

Then you can loop through the accounts and their contacts like this:

 

for(Account a :myAccounts){

    for(Contact c :a.Contacts){

        //Get the contact status here

    }

}

 

Hope this helps!

RajnisfRajnisf

thanks a lot guys

it works.

error was in if(c1  == cc.CrowdTorch_Contact_Status__c)

instead use .equals