You need to sign in to do that
Don't have an account?
Hermann Ouré
Trigger to update Date/time custom field
Hello,
I have created a trigger to update a custom date/time field (called First_Aircall_Logged__c).
Basically when a (aircall) task is created on a Case. The trigger should update the field First_Aircall_Logged__c with the task CreatedDate.
But by trigger is not working.
A actually have an error
System.ListException: List index out of bounds: 0
Trigger.AircallTaskTrigger: line 17, column 1: []
17: c.First_Aircall_Logged__c = c.tasks[0].CreatedDate;
How can I fix the trigger
Thank you
I have created a trigger to update a custom date/time field (called First_Aircall_Logged__c).
Basically when a (aircall) task is created on a Case. The trigger should update the field First_Aircall_Logged__c with the task CreatedDate.
But by trigger is not working.
A actually have an error
System.ListException: List index out of bounds: 0
Trigger.AircallTaskTrigger: line 17, column 1: []
17: c.First_Aircall_Logged__c = c.tasks[0].CreatedDate;
How can I fix the trigger
Thank you
trigger AircallTaskTrigger on Task (after insert, after update) { Set<Id> caseIds = new Set<Id>(); for(Task t: Trigger.New) { caseIds.add(t.WhatId); } //List<Case> toBeUpdateCase = [SELECT Id, First_Aircall_Logged__c, (SELECT Id, CreatedDate FROM Tasks) FROM Case WHERE Id IN: caseIds]; Map<Id, Case> caseAircallTask = new Map<Id, Case> ([SELECT Id, First_Aircall_Logged__c, (SELECT Id FROM Tasks WHERE CallDisposition LIKE '%aircall%') FROM Case WHERE Id IN: caseIds]); if(caseAircallTask.size() > 0) { for(Case c :caseAircallTask.values()) { System.debug('Cases with Aircall Task ' + caseAircallTask); c.First_Aircall_Logged__c = c.tasks[0].CreatedDate; } } update caseAircallTask.values(); }
Please find the updated code below:
Let me know if you need any other help.
Thanks,
Abhishek Bansal.
Just you have to perform the empty before the line 17. No need for multiple for loops. Before update case record also do the null check.
Thanks,
Maharajan.C
Thank you for your answers but at the end I found the answer to my question.
here is what I have done. The code is working fine but in term of code efficiency maybe my code can be optimized.
If you have any suggestion to optimise my code please share.
You can use the below trigger code: Note: Since you are using the CreatedDate which can not be changed so I am not sure why you are using after update in your trigger. You can remove that as well.
Thanks,
Abhishek Bansal.