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
shan876shan876 

List index out of bounds: 0 ?????

Hi all:
   I do not know where am I going wrong but I keep getting errors rarely but do get errors on this trigger i wrote 3 times already...
Code:
trigger tsk on Task (before insert,before update) {
    for(Task cs : trigger.new){ 
    Account[] acc = [select Id,
                    AccountNumber,
                    Territory__c from 
                    Account where 
                    Id = :cs.WhatId];
     Opportunity[] opp = [Select a.Id, 
                      a.AccountId 
                      From Opportunity a where
                      Id=:cs.WhatId]; 
     Case[] ca = [Select c.Id,c.AccountId
               from Case c where
               Id=:cs.WhatId];                                                       
    if (acc.isEmpty()){
        if(opp.isEmpty())
        {            
           Account actx = [Select Id,
                        AccountNumber,
                        Territory__c from 
                        Account where 
                        Id =:ca[0].AccountId];
           cs.Dealer_ID__c = actx.AccountNumber;
           cs.Territory__c = actx.Territory__c;  
         }
          if(ca.isEmpty())
          {          
            Account act = [Select Id,
                        AccountNumber,
                        Territory__c from 
                        Account where 
                        Id = :opp[0].AccountId];                                                         
             cs.Dealer_ID__c = act.AccountNumber;
             cs.Territory__c = act.Territory__c;
         } 
    }
    else if(cs.WhatId != null)
    {
    cs.Dealer_ID__c = acc[0].AccountNumber;
    cs.Territory__c = acc[0].Territory__c;
    }
    }
}

 Please let me know where am I going wrong...So far this is the new error:
Developer script exception from tsk : tsk: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0 Trigger.tsk: line 22, column 34
Thanks


Luke@TWSLuke@TWS
That would indicate that there is no Case for that Task
shan876shan876

But the task was created off the case??

How can I fix it?

Luke@TWSLuke@TWS
Put some debugs in so you can see what's going on

It would cause that error if cs.whatId is null


Message Edited by Luke@TWS on 10-15-2008 07:37 AM
arnt72arnt72
why does your query for the cases query c.ID and c.AccountID  instead of just ID and AccountID?