2

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();

       }

    }
2

If you have made the relationship a master-detail one (or can change it to one), you could accomplish this by adding a roll-up summary field to Contact that counts the number of DepartmentOfContact__c objects and then have a second checkbox formula field based off that that checks whether the count is greater than zero. There is a maximum of 10 of these roll-up summary fields per object at present.

One problem in your trigger code is that you are referencing relationship fields that are not populated in triggers. So instead of dep.Contact__r.Id use dep.Contact__c.

If you want to stick to the trigger approach, then something like this should work. It does less queries and doesn't contain doubly nested loops that become a performance problem when large numbers of objects are involved:

trigger DepartmentOfContact on DepartmentOfContact__c (after delete, after insert,
        after undelete, after update) {

    Set<Id> falseIds = new Set<Id>();
    Set<Id> trueIds = new Set<Id>();
    if (Trigger.isDelete || Trigger.isUpdate) {
        for (DepartmentOfContact__c d : Trigger.old) {
            if (d.Contact__c != null) falseIds.add(d.Contact__c);
        }
    }
    if (Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate) {
        for (DepartmentOfContact__c d : Trigger.new) {
            if (d.Contact__c != null) trueIds.add(d.Contact__c);
        }
    }
    if (falseIds.size() > 0 || trueIds.size() > 0) {
        // Query for Contacts that are in the wrong state and update them
        Contact[] contacts = [
                select Id
                from Contact
                where (Id in :falseIds and IsDepartment__c = true)
                or (Id in :trueIds and IsDepartment__c = false)
                ];
        for (Contact c : contacts) {
            c.IsDepartment__c = !falseIds.contains(c.Id);
        }
        update contacts;
    }
}

The Trigger.isUpdate is the awkward case when the Contact__c changes because the old and new Contact need updating. Code could be added to check that it is that specific field that is changing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.