You need to sign in to do that
Don't have an account?

Test Trigger Not Working: Trigger Updates Account Field Based on Task
I've created a trigger on a task to udpate a custom Account field (In_Touch_Date__c). Although the trigger compiles, it does not seem to be working properly. What I am trying to do is have the Account In_Touch_Date__c field update to the most recent Activity Date of the Account. I have successfully created similar triggers on the Contact and Opportunity levels.
Here is the current trigger code:
trigger UpdateInTouchAccount on Task (after insert, after update) { Set<String> whatIds = new Set<String>(); for (Task t : Trigger.new) { whatIds.add(t.WhatId); } List<Account> acct = [SELECT Id, In_Touch_Date__c FROM Account WHERE Id =: whatIds]; Map<String, Task> taskMap = new Map<String, Task>(); for (Task t : Trigger.new){ if (!(t.Type.equals('Eloqua Activity'))) { if (t.Status.equals('Completed')) { taskMap.put(t.WhatId, t); } } } for (Account a : acct) { if (taskMap.containsKey(a.Id)) { a.In_Touch_Date__c = taskMap.get(a.Id).ActivityDate; } } update acct; }
All Answers
give this a try:
When I try your code I get the following error when testing:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateInTouchAccount: execution of AfterInsert
Any ideas?
Hi Clara,
I tried the simple code you posted and fixed it to match my custom variables and it didn't give any errors and deployed to the server successfully. But then when I tried to updated an Account Task to have the subject='hey', I received the following error and it would not let me save the Task.
Error Message
Review all error messages below to correct your data.
Apex trigger UpdateInTouchAccount caused an unexpected exception, contact your administrator: UpdateInTouchAccount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.UpdateInTouchAccount: line 14, column 1
Also make sure to have a value in the "Related To" field:
Ah I see. So that would be the issue. All of our Tasks are "Related To" an "Opportunity". So instead, what I'm trying to do just add onto the trigger that we have working for our Opportunities.
Below is the code for our opportunity trigger:
The lines I modified to try and make the trigger also update the Account field are:and
However, now there seems to be an issue with the a.In_Touch_Date__c. I received the following error when update a Task's Activity Date:
Apex trigger UpdateOppLastContactDate caused an unexpected exception, contact your administrator: UpdateOppLastContactDate: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateOppLastContactDate: line 35, column 1
Let me know if you have any suggestions. Cheers