You need to sign in to do that
Don't have an account?
gulfklto
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;
}
}
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...
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:
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;