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
sakthi s 3sakthi s 3 

Update the custom field on Account based on Task and Event

I have two stage ie task and event. My Custom field is "Latest Feedback" once customer create task means i fetch the feedback from task based on the "status" is completed and if the customer is reach event means same as above. My trigger is working fine but is not updated the event feedback it is show only task feedback.

This is my two trigger for update latest feedback on Account Page.

 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.
Amit K AAmit K A
hi 

please correct ur this piece of code 

//get task Account id & Feedback for tasks with Status "Comnpleted"
         for(Event e : trigger.new){
            if(e.Feedback != '') {         // I thing you are checking for status not for feedback, use String functions for any String comparision 
              accIds.add(e.whatId);
            accntValMap.put(e.whatId,e.FEEDBACK__C);     
            }    
         }
 
sakthi s 3sakthi s 3
Hi Amit,

I changed the "If statement"
if( String.isBlank(e.Feedback__c) && e.Subject=='Appointment')
But it does not update the custom field.
The scenerio is in Account i create the custom field called as"Latest Feedback".
There is two condition 1.Task creation 2.Event creation.

I write the after insert the feedback into the task and the Status is "Completed" need to update the custom field based on feedback column string.It's work fine.

But in my Event creation not working fine.i change the value there is no error but it not update the value.i will attach the event field in event. help to update the custom field..... My event page details