0

Here is the error that drives me insane

Apex trigger SDocsSendToSpeaker caused an unexpected exception [..] SDocsSendToSpeaker: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.SDocsSendSpeaker: line 5, column 1

I have a similar apex trigger on another custom object that works perfectly fine and these two objects are pretty much identical, the logic is identical. drives me insane. not to mention I am not very good at this and am a beginner admin. Thank you for helping me out!

trigger SDocsSendToSpeaker on EOSummit__c(before update) {

for (EOSummit__c l : Trigger.new)
    if (l.Contract_Status__c == 'Send to speaker'){
   string coverletteTID = [select Template_ID__c from SDOCLocalTemplate__c where name = 'EmailForSpeakerContract'][0].Template_ID__c;
   string contractTID= [select Template_ID__c from SDOCLocalTemplate__c where name = 'Contract EO Summit'][0].Template_ID__c;
   SDOC.SDBatch.CreateSDocSync(UserInfo.getSessionId(), UserInfo.getUserName(), 'id='+l.id+'&Object=EOSummit__c&doclist='+coverletteTID +','+contractTID+'&oneclick=1&sendEmail=1');
   l.Contract_Status__c = 'Deployed';
}
}
3

The error is because no records were found for the following query:

string coverletteTID = [select Template_ID__c 
                 from SDOCLocalTemplate__c 
                 where name = 'EmailForSpeakerContract'][0].Template_ID__c;
  • Are you in a test method? If so did you create the records having the matching name
  • If not in a test, does a record exist with a name of EmailForSpeakerContract

try searching on the error System.ListException: List index out of bounds as this is an extremely common error with a litany of answers

https://salesforce.stackexchange.com/search?q=System.ListException%3A+List+index+out+of+bounds

It is best to properly check the size of returned results and validate that the data is ready to be worked on instead of blindly dereferencing or accessing lists. Not doing so is a recipe for trouble.

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.