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
-d-g--d-g- 

Trigger on Task insert Failure - List Exception when using some accounts

So, I am getting this:

Error: Invalid Data.
TasksOutlookTaskUpdate: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0: Trigger.TasksOutlookTaskUpdate: line 7, column 1 ...

... when I try to use certain accounts/contacts/opportunities.  It seems like accounts/contacts/opportunities created before a certain hour today, show up in the query making the trigger function; but after a certain time today, there is nothing but failure.  I've tried to mess around with it, but have come up with nothing ... any help would be appreciated!

Here's the code:

trigger TasksOutlookTaskUpdate on Task (before insert, before update) {
    String TaskType = trigger.new[0].Type;
    Id WhoIDCheck = trigger.new[0].WhoId;
    Id WhatIDCheck = trigger.new[0].WhatId;
    List<Task> RefName = [SELECT What.Type, What.Name FROM Task WHERE WhatId = :WhatIDCheck];
    List<Task> WhoNum = [SELECT Who.Type FROM Task WHERE WhoId = :WhoIDCheck];
    If ( WhoNum[0].Who.Type == 'Contact' ) {
        List<Contact> ContData = [SELECT Name, Phone, Email FROM Contact WHERE Id = :WhoIDCheck];
        String TaskID = trigger.new[0].Id;
        string ForOutlook = 'Type: ' + TaskType + '\n' + 'Related To: ' + RefName[0].What.Type + ': '
        + RefName[0].What.Name + '\n'
        + 'Contact: ' + ContData[0].Name + '\n' + 'Phone: ' + ContData[0].Phone + '\n' + 'Email: ' + ContData[0].Email + '\n';
        // + 'Link to Activity: https://na6.salesforce.com/' + TaskID
        trigger.new[0].TMP_Outlook_Reference__c = ForOutlook;
    } else if ( WhoNum[0].Who.Type == 'Lead' ) {
        List<Lead> LeadData = [SELECT Name, Phone, Email FROM Lead WHERE Id = :WhoIDCheck];
        String TaskID = trigger.new[0].Id;
        string ForOutlook = 'Type: ' + TaskType + '\n' + 'Related To: ' + RefName[0].What.Type + ': '
        + RefName[0].What.Name + '\n'
        + 'Contact: ' + LeadData[0].Name + '\n' + 'Phone: ' + LeadData[0].Phone + '\n' + 'Email: ' + LeadData[0].Email + '\n';
        // + 'Link to Activity: https://na6.salesforce.com/' + TaskID;
        trigger.new[0].TMP_Outlook_Reference__c = ForOutlook;
    }
}

thanks!

Best Answer chosen by Admin (Salesforce Developers) 
RishiKaliaRishiKalia

Hi -d-g-,

 

The trigger fired for event After insert will make the values in context variable ie trigger.new read only. If you want to use following code ie:

 

"trigger.new[0].TMP_Outlook_Reference__c = ForOutlook;"

 

then you should change the event of trigger to "before insert". Or if you want to use after insert event then you have to get id from trigger.new and hit soql to get required values and apply DML operation on that variable.

 

As a example :

 

trigger TasksOutlookTaskUpdate on Task (after insert){

     Task RefName = [SELECT id, Custom_Text_Field__c FROM Task WHERE id =: trigger.new[0].id];

     RefName.Custom_Text_Field__c = 'SAMPLE VALUE';

     update RefName;
   }

I hope this will provide you some info on how to update value using after insert trigger.

 

Thanks

 

 

 

All Answers

sfdcfoxsfdcfox

This would happen on contacts or leads with no prior activity history. This effect is caused due to the fact that on "before insert", the tasks in the trigger can't be queried yet, so if there's no other matching task, it will cause an empty list exception (list index 0 out of bounds).

 

Instead, consider a recursive after-insert update so that you can properly query the tasks, or maybe you could even just use a formula field.

-d-g--d-g-

Thank you sfdcfox! A formula field won't be able to do the job in this particular case.

I am completely inept at the "after insert" world of triggers.  I have tried to make a trigger in just such a way, but when I go to fill in a field with a new value, I get a message that the field is read only.

Could you give me an example of changing the value of a field in an "after insert" trigger that would be the equivalent of this:

"trigger.new[0].TMP_Outlook_Reference__c = ForOutlook;"

thanks!

MoUsmanMoUsman

Hi -d-g-,

Please try this code here in your code list is empty so it is showing "Error: List Exception: List index out of bounds: 0:" so what I have done her before doing any opration I have checked list is not Empty, I believe it will help you, but your code is not bulkified Salesforce recommend bulkification in best practice guide.

trigger TasksOutlookTaskUpdate on Task (before insert, before update) {
    String TaskType = trigger.new[0].Type;
    Id WhoIDCheck = trigger.new[0].WhoId;
    Id WhatIDCheck = trigger.new[0].WhatId;
    List<Task> RefName = [SELECT What.Type, What.Name FROM Task WHERE WhatId = :WhatIDCheck];
    List<Task> WhoNum = [SELECT Who.Type FROM Task WHERE WhoId = :WhoIDCheck];
if(!WhoNum.isEmpty()){
If ( WhoNum[0].Who.Type == 'Contact' ) {
List<Contact> ContData = [SELECT Name, Phone, Email FROM Contact WHERE Id = :WhoIDCheck];
String TaskID = trigger.new[0].Id;
string ForOutlook = 'Type: ' + TaskType + '\n' + 'Related To: ' + RefName[0].What.Type + ': '
+ RefName[0].What.Name + '\n'
+ 'Contact: ' + ContData[0].Name + '\n' + 'Phone: ' + ContData[0].Phone + '\n' + 'Email: ' + ContData[0].Email + '\n';
// + 'Link to Activity: https://na6.salesforce.com/' + TaskID
trigger.new[0].TMP_Outlook_Reference__c = ForOutlook;
} else if ( WhoNum[0].Who.Type == 'Lead' ) {
List<Lead> LeadData = [SELECT Name, Phone, Email FROM Lead WHERE Id = :WhoIDCheck];
String TaskID = trigger.new[0].Id;
string ForOutlook = 'Type: ' + TaskType + '\n' + 'Related To: ' + RefName[0].What.Type + ': '
+ RefName[0].What.Name + '\n'
+ 'Contact: ' + LeadData[0].Name + '\n' + 'Phone: ' + LeadData[0].Phone + '\n' + 'Email: ' + LeadData[0].Email + '\n';
// + 'Link to Activity: https://na6.salesforce.com/' + TaskID;
trigger.new[0].TMP_Outlook_Reference__c = ForOutlook;
}
}
}
 
Regards 
Usman
RishiKaliaRishiKalia

Hi -d-g-,

 

The trigger fired for event After insert will make the values in context variable ie trigger.new read only. If you want to use following code ie:

 

"trigger.new[0].TMP_Outlook_Reference__c = ForOutlook;"

 

then you should change the event of trigger to "before insert". Or if you want to use after insert event then you have to get id from trigger.new and hit soql to get required values and apply DML operation on that variable.

 

As a example :

 

trigger TasksOutlookTaskUpdate on Task (after insert){

     Task RefName = [SELECT id, Custom_Text_Field__c FROM Task WHERE id =: trigger.new[0].id];

     RefName.Custom_Text_Field__c = 'SAMPLE VALUE';

     update RefName;
   }

I hope this will provide you some info on how to update value using after insert trigger.

 

Thanks

 

 

 

This was selected as the best answer
-d-g--d-g-

RishiKalia, you rock! You cured my 'after insert' woes.  Thanks!

 

It actually made for a much simpler trigger, using that format.

 

Thanks!