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

Trigger Error



Scenario : I have custom object called SFDC_Project__c which has look up to cases through Case__c field.

When i create case and through same detail page through related list if i click New Project, i want to copy value of Trigger_Value field  from cases to project Cases_Trigger_Value__c after clicking save.


I have written trigger on project:


trigger SetPrimaryContact on SFDC_Project__c (before insert) 

for (SFDC_Project__c o :
List<Case> contactRole = new List<Case>();
contactRole = [select ID, Trigger_Value__c from Case where Id = limit 1];
o.Cases_Trigger_Value__c = contactRole[0].Trigger_Value__c;

insert o;


  Once i save i get error :

 Error: Invalid Data. Review all error messages below to correct your data.
Apex trigger SetPrimaryContact caused an unexpected exception, contact your administrator: SetPrimaryContact: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0: Trigger.SetPrimaryContact: line 8, column 47


Any help will be appriciated .


Thanks & Regards,


Message Edited by Abhijit on 09-21-2009 12:34 PM

From your error log, it looks like there's another trigger that is causing the error:

Apex trigger SetPrimaryContact caused an unexpected exception

Additionally,  because your SetCaseValue trigger runs before insert, you do not need the insert o; DML statement.

Ohh Sorry the name of trigger is SetPrimaryContact by mistake i renamed it . I am getting that error when i am assigning value to Project field .
The error "List index out of bounds: 0:" means that the index you have set doesn't exist.  In this case it means that your query for contactRole isn't returning any records