I am trying to update a Custom check-box field 'IsDepartment__c' in Parent Object 'Contact' that when child object/s 'DepartmentOfContact__c' is/are created or undeleted then that field will be checked and when deleted will be unchecked. but it is not working properly
public with sharing class DepartmentOfContactExistHandler {
private List<DepartmentOfContact__C> newList;
private List<DepartmentOfContact__C> oldList;
private Map<Id, DepartmentOfContact__C> oldMap;
private Map<Id, DepartmentOfContact__C> newMap;
public DepartmentOfContactExistHandler( List<DepartmentOfContact__C> newList, List<DepartmentOfContact__C> oldList, Map<Id, DepartmentOfContact__C> oldMap, Map<Id, DepartmentOfContact__C> newMap ){
this.newList = newList;
this.oldList = oldList;
this.newMap = newMap;
this.oldMap = oldMap;
}
public void ifDepartmentExistforContact(){
set<Id> conIds = new set<Id>();
for(DepartmentOfContact__C dep: newList){
conIds.add(dep.Contact__r.Id);
}
Map<ID, DepartmentOfContact__C> depofConfoContacts = new Map<ID, DepartmentOfContact__C>([select Id from DepartmentOfContact__C where Contact__r.Id IN :conIds]);
Map<ID, Contact> consToUpdate = new Map<ID, Contact>([select Id,IsDepartment__c,(select Id from DepartmentOfContact__r) from Contact where Id IN :conIds]);
for(Contact con : consToUpdate.values()){
Set<ID> depofConIds = new Set<ID>();
for (DepartmentOfContact__C depcon : depofConfoContacts.values()) {
if (depcon.Contact__r.Id == con.Id)
depofConIds.add(depcon.Contact__r.Id);
}
if (con.IsDepartment__c != false && depofConIds.size()== null){
con.IsDepartment__c = false;
}else if(con.IsDepartment__c == false && depofConIds.size()!= null){
con.IsDepartment__c = true;
}
}
if(consToUpdate.values()!= null){
update consToUpdate.values();
}
}
public void ifDepartmentdelforContact(){
set<Id> conIds = new set<Id>();
for(DepartmentOfContact__C dep: oldList){
conIds.add(dep.Contact__r.Id);
}
Map<ID, DepartmentOfContact__C> depofConfoContacts = new Map<ID, DepartmentOfContact__C>([select Id from DepartmentOfContact__C where Contact__r.Id IN :conIds]);
Map<ID, Contact> consToUpdate = new Map<ID, Contact>([select Id,IsDepartment__c from Contact where Id IN :conIds]);
for(Contact con : consToUpdate.values()){
Set<ID> depofConIds = new Set<ID>();
for (DepartmentOfContact__C depcon : depofConfoContacts.values()) {
if (depcon.Contact__r.Id == con.Id)
depofConIds.add(depcon.Contact__r.Id);
}
if (con.IsDepartment__c != false && depofConIds.size()== null){
con.IsDepartment__c = false;
}else if(con.IsDepartment__c == false && depofConIds.size()!= null){
con.IsDepartment__c = true;
}
}
if(consToUpdate.values()!= null){
update consToUpdate.values();
}
}
}
and my trigger is on child object
trigger DepertmentOfContact on DepartmentOfContact__c (after delete, after insert, after undelete,
after update) {
if(Trigger.isAfter && Trigger.isInsert){
DepartmentOfContactExistHandler dep1 = new DepartmentOfContactExistHandler(Trigger.new, Trigger.old, Trigger.oldMap, Trigger.newMap);
dep1.ifDepartmentExistforContact();
}
if(Trigger.isAfter && Trigger.isUpdate){
DepartmentOfContactExistHandler dep2 = new DepartmentOfContactExistHandler(Trigger.new, Trigger.old, Trigger.oldMap, Trigger.newMap);
dep2.ifDepartmentExistforContact();
}
if(Trigger.isAfter && Trigger.isDelete){
DepartmentOfContactExistHandler dep3 = new DepartmentOfContactExistHandler(Trigger.new, Trigger.old, Trigger.oldMap, Trigger.newMap);
dep3.ifDepartmentdelforContact();
}
if(Trigger.isAfter && Trigger.isUndelete){
DepartmentOfContactExistHandler dep4 = new DepartmentOfContactExistHandler(Trigger.new, Trigger.old, Trigger.oldMap, Trigger.newMap);
dep4.ifDepartmentExistforContact();
}
}