1

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

0

I guess you are getting the error because you have not added Status field in the query and you are trying to use it.

Please add status field in the query.

List < Task > tasks = [select Id, Status, Whatid,FEEDBACK__C from Task where id in :trigger.newMap.keySet()];

EDIT

I believe you do not require the query as you will have the status values in trigger.new;

This should work:

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;
}
  • nothing will happen – Sakthi S Sep 29 '15 at 10:03
  • You have commented the line number 13 in your code hence there is nothing in the accntValMap and the query on account is returning no value hence, nothing will happen. Please check the updated code. – manjit5190 Sep 29 '15 at 10:16
  • No error... But nothing will updated.i Just want to show the last feedback in latest feedback – Sakthi S Sep 29 '15 at 11:48
0

hii SakthiFirst Name S

Since in your code you are not putting any values in the map and you are trying to update the value on account object using the value of map so for that reason your field on object is not updating. So try to change your code by uncommenting the map value and providing value to it.

accntValMap.put(t.whatId,t.FEEDBACK__C);

Your code would then work as required.

Thanks.

  • No error... But nothing will updated.i Just want to show the last feedback in latest feedback – Sakthi S Sep 29 '15 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.