+ Start a Discussion
abhinav gangradeabhinav gangrade 

Apex code issue

Account and Review have look-up relationship. Please suggest solution to my situation. This works when every account have only or none review record. When account have more than one review then this will fail, How to merge two or more review records into one review record and update it to assign it to account as below.

Suppose one account have three review record, then these fields Subject__c, X20_Group__c, Current_Provider__c, DRAD__c, PVR__c, Service_Contract_Participation__c need to be union into one record and do similar operation as below.

//Account list to update
list<Account> accountListToUpdate = new list<Account>();

//Get the review records and put it in accountListToUpdate
for(Review__c reviewForVar : [select id , Account_Name__c , Subject__c, X20_Group__c, Current_Provider__c, DRAD__c, PVR__c, Service_Contract_Participation__c from Review__c where Account_Name__c!= null]){
    
    //Ensure the subject field is not empty
    if(reviewForVar.Subject__c != null && reviewForVar.Subject__c != ''){
               Account tempAcc = new Account();
                tempAcc.id = reviewForVar.Account_Name__c;
                tempAcc.Subject__c = reviewForVar.Subject__c;
tempAcc.X20Group__c = reviewForVar.X20_Group__c;
        tempAcc.Current_Provider__c = reviewForVar.Current_Provider__c;
        tempAcc.DRAD__c = reviewForVar.DRAD__c;
        tempAcc.PVR__c = reviewForVar.PVR__c;
tempAcc.Service_Contract_Participation__c = reviewForVar.Service_Contract_Participation__c;
                accountListToUpdate.add(tempAcc);
    }
}

//Update the list
update accountListToUpdate;
sandeep sankhlasandeep sankhla
Hi Abhinav,

Please use this trigger and handler it will work in bulk also, I have only added 3 fields, you can add more

Trigger

trigger trigerOnReview on Review__c (after insert) {
    
    reviewHandler objreview = new reviewHandler();
    
    objreview.onAfterInsert(trigger.new);

}

====================
Class

public with sharing class reviewHandler {
    
    map<String, Review__c> mapAccIdToReview = new map<String, Review__c>();
    
    public void onAfterInsert(list<Review__c> lstReviews)
    {
        
        
        for(Review__c objReview : lstReviews)
        {
            
            if(mapAccIdToReview.containskey(objReview.Account__c))
            {
                Review__c objRR = new Review__c(Current_Provider__c = 0, DRAD__c = 0, PVR__c = 0);
                
                objRR.DRAD__c = mapAccIdToReview.get(objReview.Account__c).DRAD__c + objReview.DRAD__c;
                objRR.PVR__c = mapAccIdToReview.get(objReview.Account__c).PVR__c +objReview.PVR__c;
                objRR.Current_Provider__c = mapAccIdToReview.get(objReview.Account__c).Current_Provider__c + objReview.Current_Provider__c;
                
                mapAccIdToReview.put(objReview.Account__c, objRR);
                
            }
            else 
            {
                
                Review__c objR = new Review__c(Current_Provider__c = 0, DRAD__c = 0, PVR__c = 0);
                objR.DRAD__c = objReview.DRAD__c;
                objR.PVR__c = objReview.PVR__c;
                objR.Current_Provider__c = objReview.Current_Provider__c;
                mapAccIdToReview.put(objReview.Account__c, objR);
            }
        }    
        
        list<Account> lstAcc = new list<Account>();
        
        for(Account objAccount : [Select Id, Name, DRAD__c, PVR__c, Current_Provider__c from Account where Id IN :mapAccIdToReview.keyset() ])
        {
            objAccount.DRAD__c = mapAccIdToReview.get(objAccount.Id).DRAD__c + (objAccount.DRAD__c != null ? objAccount.DRAD__c : 0);
            objAccount.PVR__c = mapAccIdToReview.get(objAccount.Id).PVR__c + (objAccount.PVR__c != null ? objAccount.PVR__c : 0);
            objAccount.Current_Provider__c = mapAccIdToReview.get(objAccount.Id).Current_Provider__c + (objAccount.Current_Provider__c != null ? objAccount.Current_Provider__c : 0);
            lstAcc.add(objAccount);
        }
        
        if(!lstAcc.isEmpty())
         update lstAcc;
        
    }

}

Please mark this as best answer if this solves your query..