10

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 : Trigger.new {
        listIds.add(childObj.Opportunity);
    }

    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;
}

Thanks

  • Welcome to the forum. Have you taken a look at salesforce.com/us/developer/docs/apexcode/Content/…? 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? – Doug B Jan 2 '14 at 12:30
  • 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. – Guillermo Jan 2 '14 at 12:37
  • 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 trigger.new, 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? – Doug B Jan 2 '14 at 13:11
  • 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'. – Guillermo Jan 2 '14 at 15:30
10

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 : Trigger.new {
    listIds.add(childObj.Opportunity);
  }

  //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 Sep 19 '14 at 15:51
  • @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) – Doug B Oct 3 '14 at 9:16
  • The List listIDs should be a SET. That way, you get a more efficient query without any duplicates in the IDs. – DavidSchach Jan 3 '17 at 19:03
1

Late to the party, but...

I wouldn't use a trigger unless absolutely necessary. This is a great use-case for Lightning Process Builder. It can update the parent record with a value from the child record. And without code!

  • Wish I could figure out how to do this. No matter how I setup the Process, the list of fields Process Builder offers me to update are from the Object (child) that I'm writing it from, after I've told it the Field I want to update is "x" on the Parent (under the "Record Type" box). Very weird. Seems so straightforward. – AMM Oct 27 '16 at 21:06
  • 1
    This looks like you are not correctly referencing the object you want to update. Usually you go to "add action" -> "update records" -> "select a related record" and then you select the related record (so not the line item with the little arrow that would bring you to the related records' records). After finishing the dialogue, you select the fields in the "main windows", not the pop-up where you selected the object to update. Does this help? – Jannis Lietzau Dec 7 '16 at 12:49
1

I modified your code based on the description you provided. Here's the code I came up with. I believe this might work.

trigger UpdateAmount on Quote__c (after insert, after update) {

    List<Opportunity> parentObjList = new List<Opportunity>();
    List<Id> listIds = new List<Id>();
    List<Quote__c> newQuotes = new Quote[]{};

    for (Quote__c childObj : Trigger.new {
        listIds.add(childObj.Opportunity);
        newQuotes.put(childObj.Opportunity, Quote__c);
    }

   parentObjList = [SELECT id,Name FROM Opportunity WHERE ID IN :listIds];

   for (Opportunity opp : parentObjList){

       opp.Amount = newQuotes.get(opp.Id).Total_List_Price__c;

       update parentObjectList;
    }
}
  • 1
    Never use DML expressions in For Loop. It is not a standard way to use of DML expression. – Shantanu Mahajan Aug 18 '16 at 9:54
0

I encountered a scenario just like this one, creating a custom opportunity product solution that needs to interact with the standard opportunity. The way I addressed this is by creating a custom roll-up field in the opportunity that adds all the amount of the child Quote__c (in your case) of the specific Opportunity. Based on your requirement I think this can be a solution if you just want to record every Quote__c amount on the Opportunity, but I’m not pretty sure if you have complex logic going on in the updating of the parent opportunity.

  • Hello Booz, I thinks this will not solve my issue because what I need is not to sum/minimum/maximum value of all Quote_c child object. I just need to update the Amount Opportunity value with the last created/updated quote (child) value. If you share some generic code I could adapt it to my case. Thanks in advance! – Guillermo Jan 2 '14 at 16:02
0

Simple and Easy

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

Opportunity --> Child Object

Contact --> Parent Object

There is a lookup between these two object 'testtarun__c'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

trigger tarunopp on Opportunity (after update,after insert) {

list<id> opplist = new list<id>();

>for(opportunity opp:trigger.new)
    {
  >> opplist.add(opp.testtarun__c);

  >>  }

list<contact> conlist = new list<contact>();

conlist = [select id,phone, (Select id,testtarun__c from Opportunities ) from contact where ID IN : opplist];

for(contact con:conlist)


> {
>>      con.title='sohil sharma';
>>      con.Date__c=system.now();
>>      update con;     
> }

}
  • While this will sometimes work, there are some things to watch out for: 1. opplist should be a SET, not a list, as you may have duplicates 2. Code is more difficult to manage than configuration and requires a sandbox, so in general, declarative tools are better. – DavidSchach Jan 3 '17 at 18:27
0
trigger Triggerupdate on person__c (after  update) { 
    List<Human__c> hum = new List<Human__c>();    
    List<Id> listIds = new List<Id>();

    for (person__c parentObj : Trigger.new) {
        listIds.add(parentObj.id);
    }

    hum = [SELECT id from Human__c  WHERE id__c IN :listIDs];

    for (Human__c opp : hum){
        for (person__c per: Trigger.new){
            opp.Age__c = per.Age__c;
        }
    }

    update hum;
}
  • In the above code I have a lookup between human__c (Child ) and person__cc(parent) so my requirement is when I change common field Age__c in person__c it should update the value in the field of Age__c (Human__c).The above trigger is the solution for that by using after update event – user46190 Jun 17 '17 at 19:03

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.