0

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.

4
  • You should use Feedback__c instead of Feedback as I believe feedback is a custom field
    – manjit5190
    Sep 29, 2015 at 13:00
  • event feedback is not custom field
    – Sakthi S
    Sep 29, 2015 at 13:29
  • i want to update latest feedback based on feedback field of task and event....
    – Sakthi S
    Sep 29, 2015 at 14:02
  • 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
    – Sakthi S
    Oct 1, 2015 at 5:35

1 Answer 1

1

Not all Events and Task are going to be related to Account, so the first thing you need to do is collect the WhatIds, not run a query. You've made the assumption that all of the WhatIds will be Account Ids. They could be related to Opportunity, Campaigns, or something else.

As I understand your code, Feeback__c is a field on Accounts and also a custom field on Event as well as Task. If it's not, you'll want to query the custom object that's perhaps a lookup or a Detail record related to Activities once you know you have a set of WhatIds that isn't empty which are all Account Ids that will have Feedback.

Below is some example code using Event that I believe will show you how to do what you want.

trigger change_feedbackOnEvent on Event  (after insert, after update)  {

     Set<Id>Wids = new set<id>();
     Map<Id,String>accntValMap = new Map<Id,String>();

     // collect What Ids
     // We know that Account Ids start with '001'
     for(Event e : trigger.new){
        if(string.valueOf(e.WhatId).startsWith('001') == true && string.isEmpty(e.FEEDBACK__C) == false){
           Wids.add(e.WhatId);
           accntValMap.put(e.whatId,e.FEEDBACK__C);
        }
     }

     List<Account>AcctsUp = new List<Account>();    

     for(Id aId : WIds ){
        Account a = new Account(Id = aId);
        a.Feedback__c = accntValMap.get(aId);      

        AcctsUp.add(a);
    }

    //update the accounts back to database
    update AcctsUp;

}

This pattern should work for both Events and Tasks once you adjust the If test condition.

17
  • i got this error Error: Compile Error: Method does not exist or incorrect signature: isEmpty(String) at line 37 column 66
    – Sakthi S
    Sep 30, 2015 at 5:48
  • 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.
    – Sakthi S
    Sep 30, 2015 at 5:57
  • Have corrected code. Should have been string.isEmpty(e.FEEDBACK__C), assuming the Feedback is in the event object.
    – crmprogdev
    Sep 30, 2015 at 15:02
  • But is not working same error i getting
    – Sakthi S
    Oct 1, 2015 at 5:14
  • What kind of field is FEEDBACK__c? Is it a rich text area field of some kind instead of a regular text field? If so, you may need to do something else as it behaves differently. Otherwise, you may need to put Boolean Result = String.isEmpty(e.FEEDBACK__c); before on the line ahead of your If statement, then change the related part of your if statement clause to && Result == false).
    – crmprogdev
    Oct 1, 2015 at 12:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .