0

There is a lookup relationship between two object CountParent__c and CountChild__c.

CountParent__c object contains a custom field called NoOfChildRecords__c which will count the no. of CountChild__c records associated with it.

enter image description here

Required scenario is,
If user edited a CountChild__c record and changes the value of lookup field from P2-->P1 then NoOfChildRecords__c should be increased by 1 in P1 and decreased by 1 in P2.

all this happening on before update trigger.

if (Trigger.isUpdate && Trigger.isBefore) {
    List < CountParent__c > incCount;
    Set < id > triggerIds = Trigger.oldMap.keySet();
    List < CountChild__c > fetchChild = [select Name, CountParent__r.name from CountChild__c where Id IN: triggerIds];
    system.debug('old====' + fetchChild);
    Set < id > triggerIds1 = Trigger.newMap.keySet();
    List < CountChild__c > fetchChild1 = [select Name, CountParent__r.name from CountChild__c where Id IN: triggerIds1];
    system.debug('new====' + fetchChild1);
    List < CountParent__c > fetchParent = [select Name, NoOfChildRecords__c from CountParent__c];
    for (CountChild__c a: fetchChild1) {
        for (CountParent__c b: fetchParent) {
            if (b.Name == a.CountParent__r.Name) {
                incCount = new List < CountParent__c > ();
                b.NoOfChildRecords__c = b.NoOfChildRecords__c + 1;
                incCount.add(b);
            }
        }
    }
    update incCount;
}

this is the code i was trying but seems wrong.

thanks!

  • 1
    an alternative, no-code, solution is package Declarative Lookup Rollup Summaries that will do this for you – cropredy Jul 7 '16 at 17:20
  • Hai Cooolranjan. please close this question. – MSCB Jul 15 '16 at 6:44
0

This code may help to you

if (Trigger.isUpdate && Trigger.isAfter) {

    Set<id> parentIds = new Set<Id>();
    List<CountParent__c> parentList = new List<CountParent__c>();
    Map<Id,Integer> parentIdAndCountMap = new Map<Id,Integer>();

    for(CountChild__c child : trigger.new) {

        if(child.CountParent__c != null && Trigger.oldMap.get(child.Id).CountParent__c != child.CountParent__c) {

            parentIds.add(child.CountParent__c);
        }
    }

    if(parentIds.size() > 0) {
        for(CountChild__c c : [SELECT Id, Name, CountParent__c FROM CountChild__c WHERE CountParent__c IN :parentIds]) {
            if(!parentIdAndCountMap.containsKey(c.CountParent__c))
                parentIdAndCountMap.put(c.CountParent__c, 0);
            parentIdAndCountMap.put(c.CountParent__c, parentIdAndCountMap.get(c.CountParent__c)+1);
        }
    }

    if(parentIdAndCountMap.size() > 0) {
        for(Id pId : parentIdAndCountMap.KeySet()) {
            CountParent__c prnt = new CountParent__c();
            prnt.Id = pId;
            prnt.NoOfChildRecords__c = parentIdAndCountMap.get(pId);
            parentList.add(prnt);
        }
    } 

    if(parentList.size() > 0) {
        update parentList;
    }    
}
  • 2
    IMO if you want to update parent field you should do it in after update. – Mr.Frodo Jul 7 '16 at 11:18

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.