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(Event)).
I want my trigger to write back one fields from the Latest Activity to its related Feedback, Status and Feedback is not Null.
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<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 "Completed"
for(Task t : trigger.new){
if(t.Status == 'Completed') {
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;
}
An After Insert trigger on Feedback that writes back to Event, and an After Update trigger on Feedback that waits for a related Latest Feedback to be created.
trigger change_feedbackOnEvent on Event (after insert, after update) {
Set<String> accountSet = new Set<String>();
List<Event> events = [select Id,Whatid,FEEDBACK__C from Event where
id in :trigger.newMap.keySet()];
List<string> accIds = new List<string>();
//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(Event e : trigger.new){
if(e.Feedback != '') {
accIds.add(e.whatId);
accntValMap.put(e.whatId,e.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;
}
Both triggers are working fine but after created event that showing the task feedback in latest feedback field(custom field). I want the latest feedback of event.