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
Irene SlessIrene Sless 

Update parent record from child related list

I would like to update the parent record of a master-child relationship when adding a new child (or editing an existing one to update), where the master record is a standard object (Opportunity) and the child record is a custom object (Memo).

I found this code : 
https://help.salesforce.com/apex/HTViewSolution?id=000005280&language=en_US
but it does nothing - doesn't throw any errors, but doesn't do the update either. Can anyone tell me why not, or whether this is the code to use or should it be done differently? This is what I have now, based on the SF code in the article from the link above:

trigger updateOpportunityWithMemoDate on Memo__c (after insert, before update) {
  
     List<Opportunity> opps = new List<Opportunity>();
     Opportunity oppty;

     for(Memo__c memo: Trigger.new)
      {
           oppty = new Opportunity(Id=memo.Opportunity__c);
           oppty.Last_Memo_Date__c = memo.CreatedDate;   //this is a custom DateTime field in Memo
           oppty.Last_Memo__c = memo.Subject__c;             //this is a custom text field
           oppty.Most_Recent_Memo__c = memo.id;              //this is a custom lookup field
           opps.add(oppty);
       }
      
      if(opps.size() > 0)
      Update opps;

}

}

I would like to also do the same when updating/adding an Opportunity Team Member, since this object doesn't have the ability like for example Product, where one can use roll-up summaries etc, so the only way to update something on the Opportunity parent seems to be via a trigger.
James LoghryJames Loghry
Your code looks good, in general.  It does look lke you have another "}", but that could be an artifiact of copy / paste.

Have you looked at the trigger to see if it is active?  If your trigger is not active (Open it up in the Salesforce UI, or check the trigger's meta-xml file in Eclipse or MavensMate), then your logic wouldn't fire.

Another option to see if it's working is to create one or  more unit tests to run and validate that your trigger is working properly.
Geoffrey J FlynnGeoffrey J Flynn
+1 for James, I see nothing wrong with this except the extra } at the end.
You are testing code just by adding a new child record to an existing Opportunity?
What does a System.debug show if you put it right under opps.add(oppy); ?
Irene SlessIrene Sless
Sorry, the extra } is just a copy/paste glitch - it's not in my actual trigger! :)

And James, the problem was the trigger wasn't active! So simple and so foolish of me not to notice that ... thank you both!