0

We have a parent Account record "Test ABC" and child Accounts record "Test X" and "Test Y".

We have a Child object(Custom Object) Child__c. It is Master detail relation with AccountObject.

Requirement is : if the Account's child__c object's records 'Jan month' data is updated.it should updated the "Parent Account related child__c object 'Jan month' data".

For example:

Parent Account: Test ABC (Having Child__c) - Sum (Test X + Test Y)
    Child Account:   Test X (Having Child__c) - Sum (Test 1 + Test 2) 
                               Test 1 (Having Child__c)
                               Test 2 (Having Child__c)
                     Test Y (Having Child__c) Sum (Test 3 + Test 4) 
                               Test 3 (Having Child__c)
                               Test 4 (Having Child__c)

if we update the child record(Jan month field) of the Test X(Account) and Test Y(Account).

Sum X+Y child record data(Jan Month) = Test ABC child record(Jan Month).

Tried Code:

public class AccountDataHelper{

public static void updateChildRecords(Map<Id, Account_Financial_Data__c> newMap){

    List<Account_Financial_Data__c> assetTagList = new List<Account_Financial_Data__c>();
    Map<Id, Account_Financial_Data__c> assetTagMap = new Map<Id, Account_Financial_Data__c>();

    for(Account_Financial_Data__c  assetTag : newMap.values()){

       if(assetTag.Account_Reference__c!= null){
          assetTagMap.put(assetTag.Id, assetTag); 
       }   
    }


        assetTagList = [SELECT Id, 
                               X1_del__c,
                               Account_Reference__c
                        FROM Account_Financial_Data__c 
                        WHERE Account_Reference__c IN: assetTagMap.keySet()];

        for(Account_Financial_Data__c  assetTag : assetTagList){
            if(assetTagMap.containsKey(assetTag.Account_Reference__c))

               assetTag.X1_del__c = assetTagMap.get(assetTag.Account_Reference__c).X1_del__c;
               system.debug('@@@@@@@@@@@@@@'+assetTag.X1_del__c);
        }


           update assetTagList;

}
  • If your two object are in a Master-Detail relationship, is there a reason why you're trying to use a trigger here instead of a rollup summary field? Also, the relationship you're describing is confusing. Unless you are using a junction object, if Child__c is the child (or in other words, the 'detail' of the master-detail relationship), then Child__c can only point to one Account record. If Child__c is actually the parent (or 'Master') record in the relationship, that is extremely confusing, and you should probably edit your question to change that so it doesn't confuse other people. – Derek F Aug 14 '17 at 13:17
  • Updated the requirement with clear explanation. Please check once. Thanks – Vivek Aug 16 '17 at 7:57

Your Answer

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

Browse other questions tagged or ask your own question.