0

I am working on a trigger which will count the child records to parent basing on some picklist value conditions. If picklist field values= "son, Daughter, Husband, Wife, Partner" then I need to count which is working perfectly but on update if I change the picklist vales from other values to "son, Daughter, Husband, Wife, Partner" then count is not incrementing and from "son, Daughter, Husband, Wife, Partner" to other values then count is not decrementing. Can anyone helps me out regarding this issue. My code is below.

trigger ContactCount on hed__Relationship__c(after insert,after update,after delete){

    Map<Id, Decimal> conMap = new Map<Id, Decimal> ();    
    Map<Id,List<hed__Relationship__c>> mspconIdNdhedRel =new  Map<Id,List<hed__Relationship__c>>();
    set<ID> relationshipIdSet=new set<id>();    
    if(trigger.isafter){     
        if(trigger.isInsert){
            for(hed__Relationship__c con1:trigger.new){            
                if((con1.hed__Type__c=='Son' || con1.hed__Type__c=='Daughter' || con1.hed__Type__c=='Husband' || con1.hed__Type__c=='Wife' || con1.hed__Type__c=='Partner')  && trigger.isinsert){
                    relationshipIdSet.add(con1.hed__Contact__c);
                }
            }
        }  
        if(trigger.isUpdate){
            for(hed__Relationship__c con1:trigger.new){
                if(con1.hed__Type__c!=null && trigger.newmap.get(con1.id).hed__Type__c!=trigger.oldmap.get(con1.id).hed__Type__c){
                      relationshipIdSet.add(con1.hed__Contact__c);               
                } 
            }
        }                


        if(trigger.isDelete){
            for(hed__Relationship__c con1:trigger.old){
                if((con1.hed__Type__c=='Son' || con1.hed__Type__c=='Daughter' || con1.hed__Type__c=='Husband' || con1.hed__Type__c=='Wife' || con1.hed__Type__c=='Partner') && trigger.isdelete){
                    relationshipIdSet.add(con1.hed__Contact__c);
                }
            }
        }


        for(hed__Relationship__c hedRel: [Select id,hed__Contact__c from hed__Relationship__c where hed__Contact__c in : relationshipIdSet and (hed__Type__c='Son' OR hed__Type__c='Daughter' OR hed__Type__c='Husband' OR hed__Type__c='Wife' OR hed__Type__c='Partner')] ){
            if(!mspconIdNdhedRel.containsKey(hedRel.hed__Contact__c)){
                mspconIdNdhedRel.put(hedRel.hed__Contact__c,new List<hed__Relationship__c>{});
            }
            mspconIdNdhedRel.get(hedRel.hed__Contact__c).add(hedRel);
        }

        system.debug('mspconIdNdhedRel-='+mspconIdNdhedRel);
        List<Contact> a1=new list<Contact>();    

        for(Contact con:[select id,Number_Of_Dependents__c  from Contact where ID in:mspconIdNdhedRel.keyset()]){    
            List<hed__Relationship__c> lst = mspconIdNdhedRel.get(con.id);

            system.debug('-='+lst.size() );
            con.Number_Of_Dependents__c =lst.size();   
            a1.add(con);            
        }    
        IF(a1.SIZE()>0){   
            update a1;   
        }
    }

}
  • The count on contact record will get affected only when you insert or delete hed__Relationship__c record. Updating record will not have any impact on count. – Reshma Jan 3 at 9:01
  • I want it to work on update to, if I cahnge picklist values from other values to these values("son, Daughter, Husband, Wife, Partner") then count should incriment and at same time if I change from "son, Daughter, Husband, Wife, Partner" to other values then count should decrement. – user61203 Jan 3 at 9:05
  • 1
    While this is a different specific issue, the answers you received on your previous question also apply here. You can either write complex logic to add and remove from the count based on these changes, or you can write queries to perform the totalling for you, or you can just use DLRS. – David Reed Jan 3 at 12:46

Your Answer

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