You need to sign in to do that
Don't have an account?
Blake Tanon
Simple Trigger: Update child records.
I feel this would be an easy trigger to make, I'm not just a programmer of any sorts. Here is what I need it to do:
When the parent record (contact) is updated or created, update all child records for partner_member__c. There won't be anymore than 4 child records in the largest case.
If anyone could help me with this I'd appreciate it!
Thanks,
Blake
trigger updateChild on Contact(after update){
update [select id from partner_member__c where Contact__c in :trigger.new];
}
Its could be that you upload Contact records from dataLoader so suppose you are goign to upload 50 contacts records then your partner_member__c records would be 50*4 approximately which is 200 .
Now if you makeing update statement inside the loop it will hit the limit .
So you have to change the approach .
here is the code would work fine !
Trigger updatepartnermember on Contact(after update,after insert ){
Map<Id,List<partner_member__c>> mapContactIdAndPartner = new Map<Id,List<partner_member__c>>();
for(partner_member__c obj :[select Id,Name from partner_member__c where Contact__c in : trigger.newmap.keyset()]){
if(mapContactIdAndPartner.get(obj.Contact__c) == null){
mapContactIdAndPartner.put(obj.Contact__c,new List<partner_member__c>());
}
mapContactIdAndPartner.get(obj.Contact__c).add(obj);
}
List<partner_member__c> lstToUpdate = new List<partner_member__c>();
for(Contact c : trigger.new){
List<partner_member__c> partner_member__lst = mapContactIdAndPartner.get(c.Id);
for(partner_member__c obj :partner_member__lst){
//your logic here
lstToUpdate.add(obj);
}
}
update lstToUpdate;
}
P.S. There is no loop in the trigger I gave. Take a look. It has only 1 line.
Got bored, so thought I'd pick a fight.
Having a closer look at your code, I would like to recommend a few things.
A simplified version (??? not as simple as the first one) which lets you do some field mappings, (which is almost always the case) is given below
I understand there is no for loop but trigger.new is a self list treated as loop.
Now suppose you are going to upload thoudsands of records then update statement will run the same number of records will hit the limit.
I'd have to disagree. Trigger.new is a list, but when you issue it in the where clause of a query, there will be one query issued which will use the ID values from trigger.new (as a collection). Also, even if you do a mass data load,
Apex triggers are executed in batches of generally size 200. So even if you were doing thousands of updates, each trigger instance will be processing only 200 records at a time.Apex triggers will process the entire volume of update in a single transaction. There will not be any splitting of volume of records. The volume split only happens with batch apex.
However, SFDC internally can manage performance by splitting the batch size of the trigger but that will be abstracted for the end user and apex developer
Sorry My mistake . you are absolutely right this will invoke each time only once .
Regards,
Sravan.
Since the trigger also works for update, what sometimes happens is that your trigger tg1 updates the child object, the child object trigger tg2 updates the parent and this will fire tg1 again. So it ends up that the apex triggers are executed multiple times when you dont really need that.
There are many techniques to solve trigger recursion. Do a cursory search and come back if you have problems.