0

I have a use case where in for a Case Record I am supposed to fetch value from the Parent Account of the Account set on the Case. One of the problems here is that we can't use the parent record's Id in before insert trigger for which we have to query the parent record's separately in a query.

I was successful in getting the Ids of the Parent as well as Grand Parents in a Map. The problem I am facing is that I am unable to retrieve these values from the map, since when I am passing these directly as a key to map, they still hold null value.

Because instOfCase.Account.ParentId holds null , I cannot retrieve values from Map

public static void AutoPopulateLaborMileageCharge(List<Case> listCase){


    Map<Id,Account> mapParentAccount=new Map<Id,Account>();
    Set<ID> setChildAccount=new Set<ID>();
    Set<ID> setParAccIds = new Set<ID>();

        System.debug('@@##Inside AutoPopulateLaborMileageCharge of Case');
            for(Case instOfCase:listCase){
                setChildAccount.add(instOfCase.AccountId);
                System.debug('@@##Child Accounts'+setChildAccount);
            } //End of for

            for(Account a:[Select ParentId from Account where Id IN: setChildAccount ]){
                setParAccIds.add(a.ParentId);
                System.debug('@@##Parent Accounts'+setParAccIds);
            } //End of for

            for(Account instAccount:[select Id,Dvp2_Labor_Amount__c,Dvp2_Mileage_Amount__c from Account where Id IN:setParAccIds]){
                mapParentAccount.put(instAccount.id,instAccount);
                System.debug('value of mapParentAccount'+mapParentAccount);
            } //End of for

            for(Case instOfCase:listCase){

                System.debug('@@##Account Parent Id'+instOfCase.Account.ParentId);
                if(instOfCase.Account.ParentId!=null && mapParentAccount.containsKey(instOfCase.Account.ParentId)){
                    instOfCase.Dvp2_Account_Hourly_Labor_Rate__c = mapParentAccount.containsKey(instOfCase.Account.ParentId)?mapParentAccount.get(instOfCase.Account.ParentId).Dvp2_Labor_Amount__c:null;
                    instOfCase.Dvp2_Account_Hourly_Mileage_Rate__c = mapParentAccount.containsKey(instOfCase.Account.ParentId)?mapParentAccount.get(instOfCase.Account.ParentId).Dvp2_Mileage_Amount__c:null;
                } 

                else if (instOfCase.Account.ParentId==null){
                    instOfCase.Dvp2_Account_Hourly_Labor_Rate__c=200;
                    instOfCase.Dvp2_Account_Hourly_Mileage_Rate__c=200;
                }

            } //End of for

 }
1

Here you need one more map to make a pair of Parent and child map

Map<Id,Id> parChildMap= new Map<Id,Id>();
for(Account a:[Select ParentId from Account where Id IN: setChildAccount ]){
    setParAccIds.add(a.ParentId);
    parChildMap.put(a.Id, a.ParentId);
    System.debug('@@##Parent Accounts'+setParAccIds);
} 

Then use this map in your final loop

for(Case instOfCase:listCase){
   Id parAccId = parChildMap.get(instOfCase.AccountId)
    if(parAccId !=null && mapParentAccount.containsKey(parAccId )){

So now you have all the values which you need.

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.