This is only the 2nd trigger I've attempted to write, and it's making me nuts. I have two custom objects: Latest Feedback (child of Feedback(Tasks)).
I want my trigger to write back one fields from the Latest Activity to its related Feedback and Status.
An After Insert trigger on Status that writes back to Tasks, and an After Update trigger on Feedback that waits for a related Latest Feedback to be created.
trigger change_feedback on Task (after insert, after update) { Set accountSet = new Set();
List<Task> tasks = [select Id,Whatid,FEEDBACK__C from Task where id in :trigger.newMap.keySet()];
List<Id> accIds = new List<Id>();
//variable to hold account id & task feedback
Map<Id,String> accntValMap = new Map<Id,String>();
//get task Account id & Feedback for tasks with Status "Comnpleted"
for(Task t : trigger.new){
if(t.Status == 'Completed') {
accIds.add(t.FEEDBACK__c);
//accntValMap.put(t.whatId,t.FEEDBACK__C);
}
}
//query the accounts that we need to update
List<Account> acctsToUpdate = [Select Id from Account where id in :accntValMap.keySet()];
//now use the map variable to update the account field
for(Account acc : acctsToUpdate){
acc.Feedback__c = accntValMap.get(acc.Id);
}
//update the accounts back to database
update acctsToUpdate;
}
I got this error
Error: Invalid Data. Review all error messages below to correct your data.Apex trigger change_feedback caused an unexpected exception, contact your administrator: change_feedback: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Task.Status: Trigger.change_feedback: line 11, column 1 * = Required InformationTask Information