You need to sign in to do that
Don't have an account?
john harvey 2
How to update siblings records and parent record in a same trigger
Hi all,
I have written a code to update all the siblings records and parent records of the record getting updated. I want to use MAP in below trigger if it is possible. I want to optimize it using MAP. Can anyone please help me? Thank you so much.
trigger Addressflag on Address__c (After update, After Insert) { if(Trigger.isUpdate || Trigger.isInsert){ List<ID> ConIds = new List<ID>(); Boolean needsUpdate = FALSE; for (Address__c Adds: trigger.new) { ConIds.add(Adds.contact__c); } system.debug('parent con ids'+ConIds); contact con = [select id, checkbox__c, recordTypeId from contact where Id IN :conIds]; List<Address__c> otherAlt = new List<Address__c>([select id, contact__c, checkbox__c, contact__r.recordTypeId from Address__c where contact__c in: conIDs]); for (Address__c adds: trigger.new) { for (integer i=0; i < otherAlt.size(); i++) { if ((otherAlt[i].contact__c==adds.contact__c) && (otherAlt[i].id != adds.id)) { otherAlt[i].checkbox__c = FALSE; con.checkbox__c=FALSE; needsUpdate = TRUE; } } } if(checkrecursive.runOnce()) { if (needsUpdate) { update otherAlt; update con; } } } }Contact has master detail relationship with address. What I am trying to do is whenever the checkbox on any of the address is checked I am unchecking the same checkbox from all other siblings and parent record. I am acheving it through above trigger but I am concerned about the time when 1000s f records will get inserted at same time.
I have observed your code. I just want to advise somthing,
- When you are listing some Ids please Set instead of List.
- Always use trigger handlers while writing triggers.(this is a best practice).
- In your code i don't think you need any Map or to write inner for loops.
I am attaching a sample code for your problem. let me know any problem you have found.Trigger,
Trigger Handler,
Please save the trigger handler first as an apex class. Then save your trigger.
Thanks,
Prosenjit
All Answers
I have observed your code. I just want to advise somthing,
- When you are listing some Ids please Set instead of List.
- Always use trigger handlers while writing triggers.(this is a best practice).
- In your code i don't think you need any Map or to write inner for loops.
I am attaching a sample code for your problem. let me know any problem you have found.Trigger,
Trigger Handler,
Please save the trigger handler first as an apex class. Then save your trigger.
Thanks,
Prosenjit
I believe the trigger above will break when you insert/update two addresses (checked) from two different contacts at the same time (say, use a test class or data loader to prove my point0.
To answer your question. If you are concerned about the data load, you can use a scheduled apex. The only problem is the changes will not happen right away. There are also some tricks to run the schedule from the trigger when the process is approaching its limit.
If your are satisfied with my answer, please make it as best answer :)
Thanks
Prosenjit