Trigger to update parent object value with child value

  • In my company we are using Oppotunity standard object and we have created a custom Quote object. The opportunity object is the parent and Quote is the child. How has to be the trigger in order to update the Amount Opportunity value with the Amount Quote Value?

    I´m new with Apex and it will be very usefull has some APEX code to do this.

    I have reuse some trigger example and it has to be something similar to:

    trigger UpdateAmount on Quote__c (after insert) {
        List<Opportunity> parentObjList = new List<Opportunity>();
        List<Id> listIds = new List<Id>();
        for (Quote__c childObj : {
        parentObjList = [SELECT id,Name,(SELECT ID, Total_List_Price__c FROM Quote__r) FROM Opportunity WHERE ID IN :listIds];
        for (Opportunity opp : parentObjList){
            for (Quote__c quote: Opportunity.Quote__r){
                opp.Amount = quote.Total_List_Price__c;
        update parentObjectList;


    Welcome to the forum. Have you taken a look at You'll get more help if you can show what you've done to help yourself and where you are still having problems. You probably want to have a Trigger on your custom object Quote__c. Would you want it to update Opportunity if the Quote__c was inserted? updated? deleted? What would you expect to happen if there are several quotes associated with the same Opportunity? Are you going to sum those values?

    I have already review Apex Workbook but this is my first trigger and I don´t really know how to apply it to my issue. We have to create this Quote__c custom object because we are using another app to generate the quotes. The problem is that the Amount value of the opportunity is empty and we need to update it with the value of the last Quote__c object related to this specific opportunity. The trigger must to be fired every when a new Quote__c is created or updated.

    So, sounds like your starting point is a trigger on Quote__c. Create that first and identify what events you want it to fire for (update and insert). Your first lines of code will probably want to build up a set of the Opportunity IDs used by the Quotes in the, so that you can query and update the relevant Opportunities. You'll also need to think about what you mean by "last Quote__c" is it the latest created date or the latest updated?

    Hello Doug, I have added some APEX code. Could you please review it and say to me if it´s ok? When I test it on Developer Console an error appears related to 'Quote__r'.

  • Doug B

    Doug B Correct answer

    8 years ago

    Just looking at the code you have there a few things stand out. I haven't tested this but I've added comments which might help you along

    trigger UpdateAmount on Quote__c (after insert, after update) { //You want it on update too, right?
      Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); //Making it a map instead of list for easier lookup
      List<Id> listIds = new List<Id>();
      for (Quote__c childObj : {
      //Populate the map. Also make sure you select the field you want to update, amount
      //The child relationship is more likely called Quotes__r (not Quote__r) but check
      //You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest
      parentOpps = new Map<Id, Opportunity>([SELECT id, amount Name,(SELECT ID, Total_List_Price__c FROM Quotes__r) FROM Opportunity WHERE ID IN :listIds]);
      for (Quote__c quote: Trigger:new){
         Opportunity myParentOpp = parentOpps.get(quote.Opportunity__c);
         myParentOpp.Amount = quote.Total_List_Price__c;
      update parentOpps.values();

    I would like to know what does. listIds.add(childObj.Opportunity); do??

    @dominicseb, if you have a new question you should really raise it as such. ListIDs is a variable declared as a List. The add method on a list is used to add a value to that list. So the instruction listids.add(childObj.Opportunity) means add the value childObj.Opportunity (this is an ID) to the list called listIDs (this is a list of IDs)

    The List listIDs should be a SET. That way, you get a more efficient query without any duplicates in the IDs.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM