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
gulfkltogulfklto 

Execute trigger when child object is inserted or updated

Hello,

I have a trigger that updates a field on the opportunity creating a sort of rollup field based on a list of child related custom objects (called standard products), it gives me in one only field the list of product code and quantity I have in multiple standard products  list.

 

It works fine, bu I have only one problem, this trigger is activated if I create or edit an opportunity. I would like it to be executed when I edit o add a Standard product record.

 

How could I do it?

 

Here below the trigger code:

 

trigger ProductList on Opportunity (before insert, before update) {
for (Opportunity opp : Trigger.new)
{
Id OppId;
OppId = opp.Id;
String products_and_quantities;
products_and_quantities ='';
Powertrain_Products__c[] ThisOppProduct = [select Product_type__c, Quantity__c From Powertrain_Products__c WHERE Opportunity_del__c = :OppId ];
for(Powertrain_Products__c o: ThisOppProduct)
{
products_and_quantities += '[' +o.Product_type__c + ' - ' + o.Quantity__c +'] ';
//o.update(o.products_and_quantities__c);
}
opp.products_and_quantities__c = products_and_quantities;
//update opp;

}
}

 

BritishBoyinDCBritishBoyinDC

You could add some code to the child object that queries for its parent, and then executes similar code to this, though you would likely need to create a map to avoid triggering governor limits, that would look something like this.

 

Or, depending on the relationship between the Opty and the Standard Product, you could use the new cross object workflow feature to update a field on the parent when the child record is updated, which would then trigger this code...

gulfkltogulfklto

More or less I did it, but it is not so clear to me how to use Maps.

I understood that I could have problems while doing a batch insert of objects (Correct?)

 

Could you be so kind to correct the code I wrote with the Maps?

 

Here the code:

 

trigger UpdateOppProd on Powertrain_Products__c (after insert, after update) {
string oId = Trigger.new[0].Opportunity__c; //this is the opportunity object lookup ID
String products_and_quantities; //
products_and_quantities ='';
Opportunity oppty = [Select Id,products_and_quantities__c from Opportunity where Id = :oId limit 1];
Powertrain_Products__c[] ThisOppProduct = [select Product_type__c, Quantity__c From Powertrain_Products__c WHERE Opportunity__c = :oId ];
for(Powertrain_Products__c o: ThisOppProduct)
{
products_and_quantities += '[' +o.Product_type__c + ' - ' + o.Quantity__c +'] ';

oppty.products_and_quantities__c = products_and_quantities;

update oppty;
}
}

 

BritishBoyinDCBritishBoyinDC

It looks like the code you have written assumes you will only ever update one Powertrain Product at one time - which is normally a dangerous assumption - if you were 100% confident that were true, you wouldn't need a map.

 

I haven't checked the syntax for errors, but it would look something like this...the one area I can't be sure about is the query highlighted in blue, but if you are using eclispe, you can use the schema explorer to test it.

 

You should take some time to look through the docs about Maps, and make sure you understand what this is doing - it is often the only way to make a trigger work in bulk... 

 

 

trigger UpdateOppProd on Powertrain_Products__c (after insert, after undelete, after update) { Set<String> OptyIds = new Set<String>(); List<Opportunity> updOptys = new List<Opportunity>(); for(Powertrain_Products__c pt : trigger.new){ if(!OptyIds.contains(pt.Opportunity__c)) OptyIds.add(pt.Opportunity__c); } Opportunity[] Opportunities = new List< Opportunity >(); Opportunities = [Select Id,products_and_quantities__c,(select Product_type__c, Quantity__c From Powertrain_Products__r) from Opportunity where Id in :OptyIds]; Map<Id, Powertrain_Products__c []> OptyPTP = new Map<Id, Powertrain_Products__c[]>(); for (Opportunity o : Opportunities ) { OptyPTP.put(o.Id, o.Powertrain_Products__r); } Powertrain_Products__c [] PTP = new List<Powertrain_Products__c >(); for (Opportunity o : Opportunities) { PTP = OptyPTP.get(o.Id); for(Powertrain_Products__c p : PTP){ o.products_and_quantities += '[' + p.Product_type__c + ' - ' + p.Quantity__c + '] '; } updOptys.add(o); } update updOptys;