I have a child object having Master-Detail relationship with contact
object. There is a checkbox Primary__c on both of the object. I need to see if that checkbox
is checked for more than one record and if it is I need make another checkbox = true
on every record.
For ex. Contact A
have the primary__c checkbox = true
(checked) and the child object
address__c also have the checkbox = true
, when this is the scenario I need to update another checkbox
popup__c on all the records that are related to the contact
and also the contact
itself. I tried to write after insert, update and delete trigger and was not able to achieve it. Relationship name is addresses__r
. Please help me as in how this can be done.
trigger AlternateAddressPopupAlert on Alternate_Address__c (after insert, after update,after delete){
map<Id,Alternate_address__c> mapOfAllFamily = new map<Id,Alternate_address__c>();
set<Id> setOfparentconact = new set<Id>();
if(trigger.isInsert || trigger.isUpdate){
for(Alternate_Address__c altObj : trigger.new){
if((trigger.isInsert || trigger.isUpdate) && altObj.checkbox__c!=trigger.oldmap.get(altObj.Id).checkbox__c){
if(altObj.contact__c != null){
setOfparentconact.add(altObj.contact__c);
}else{
}
}
system.debug('Set of primary contact----'+setOfparentconact);
}
}else if(trigger.isDelete){
for(Alternate_Address__c altObj : trigger.old){
if(altObj.contact__c != null){
setOfparentconact.add(altObj.contact__c);
}
}
}
if(!setOfparentconact.isEmpty()){
for(Contact cntObj : [SELECT id,checkbox__c,(SELECT id,checkbox__c from Contacts__r) from Contact WHERE id in : setOfParentContacts]){
set<Id> setOfOwners = new set<Id>();
if(cntObj.Contacts__r != null && cntObj.Contacts__r.Size() > 0 ){
for(Contact ChildCntObj : cntObj.Contacts__r){
setOfOwners.add(ChildCntObj.OwnerId);
}
}
setOfOwners.add(cntObj.OwnerId);
mapOfAllFamily.put(cntObj.Id,setOfOwners);
}}
I haven't got very far as I am unable to decide how to proceed with this.
Best Answer
I'm not entirely certain I understand the relationships, but let me see if I can point you in the right direction. I'm going to make the assumption that you want to do this afterinsert and afterupdate and before Delete since an after Delete makes no sense. I'm also understanding that the checkbox field is named primary__c on both the objects Contact and it's child Alternate_Address__c. There's a third Object named Address__c that's also a child of Contact with a checkbox field named popup__c.
You mention "needing to see if that checkbox is checked for more than one record, and if it is, needing to make another checkbox = true on every record." Its unclear if this refers to it being checked on more than one related record causing you to need to check it all on other records or something else. If so, you'll need to clarify that requirement as that can be a lot of work to make happen. You might say, that's a big ask and beyond the scope of what I intend to answer.
Hopefully this shows you the kind of approach using MAPS that you'll want to take. Get your fields sorted out along with your queries and use the Maps to determine which records are a match between your object for updating or manipulation. Since I don't know your schema, the exact fields to use, or precisely what you're trying to do, I can't really help you any further but this should point you in the right direction to follow.
Edit
Regardless of the relationship names, you'll need to follow the kind of pattern I've outlined above. What you'll need to do in addition is once you have a list of all children, for each parent record, you'll need to iterate through the list of child records and count the number of children to see how many have the
checkbox = true
. If the number is more than one (you can break if you want to), you add that list to a map for updating.To help you with that, instead of keeping a
map<ParentId,ChildId>
(or vice-versa), you'll want to keep either a map> or map> plus amap<ChildId,ChildsObject>
. so you can pull everything out that needs to be updated when it's needed.Based on your comments, it sounds as though your entry criteria would need to be altered to something like below for the afterUpdate part of your trigger. Note that an AfterInsert trigger doesn't have a trigger.old, so best to do these in separate blocks :
That should give you a sorted map of all the child records that have more than 1 checkbox checked. If I've used the wrong field name, you'll be able to correct that. At least you can see the pattern now and apply it to all the other parts of your trigger.