1

I am trying to update the status field of all the related child (Recipe Sheet Items) for a given parent (Recipe Sheet) when the status is updated to 'Void' and I am trying to make this trigger handle bulk. I get this error when I try to save it.

"Error: Compile Error: Method does not exist or incorrect signature: [LIST].keySet() at line 5 column 20"

Can someone please help me write this code correctly?

trigger SyncStatus on Recipe_Sheet__c(after update) {

If(Trigger.isUpdate){

     Set<ID> ids = Trigger.new.keySet();
     list<Recipe_Sheet__c> updatedParents = [SELECT Id, 
                 (SELECT Id, Recipe_Sheet__c, Status__c 
                  from Recipe_Sheet_Items__r ) FROM Recipe_Sheet__c
                 WHERE Id in :ids];
         List<Recipe_Sheet_Item__c> childrenToUpdate = new List<Recipe_Sheet_Item__c>();

         //Then loop through each parent object in 'updated parent
         for ( Recipe_Sheet__c p : updatedParents) 
         { 
        if(p.Status__c=='Void'){

                //and loop thru each kid in the child set}
               for(Recipe_Sheet_Item__c kid : p.Recipe_Sheet_Items__r) 
               { 
                         ///update logic from above
                          if( kid.Status__c !=  p.Status__c)
{
       kid.Status__c =  p.Status__c;
      childrenToUpdate.add(kid);
}
                }
        }}
       if( !childrenToUpdate.isEmpty)
       {
            update childrenToUpdate;
       }
}}

2 Answers 2

5

Trigger.New is a list of sObjects. If you want a set of keys you want to use trigger.newMap. This is a map of the objects in trigger.new with the ID as the key and the object as the value. So you are trying to call keyset(), which is a map method, on a list.

From the docs

Trigger.new - Returns a list of the new versions of the sObject records.

Trigger.newMap - A map of IDs to the new versions of the sObject records.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

2
  • Thanks. Now I receive this error: "Compile Error: Initial term of field expression must be a concrete SObject: LIST<Recipe_Sheet_Item__c> at line 28 column 12" It is referring to the line " if(!childrenToUpdate.isEmpty)", any thoughts here?
    – Grace
    Commented Jul 8, 2014 at 15:02
  • add parentheses. .isEmpty() Commented Jul 8, 2014 at 15:03
-1

Trigger.new is a List. List do not have keySet() Method

1
  • Trigger.new doesn't return a list, it is a list.
    – martin
    Commented Oct 16, 2015 at 6:15

You must log in to answer this question.

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