You need to sign in to do that
Don't have an account?
Irene 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.
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.
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.
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); ?
And James, the problem was the trigger wasn't active! So simple and so foolish of me not to notice that ... thank you both!