Update the parent object using trigger - relationship master detail
Trigger :
Trigger :
trigger LoanPaymentTransactionTrigger on loan__Loan_Payment_Transaction__c (after insert, after update, after delete) {
if(Trigger.isAfter){
if(Trigger.isInsert || Trigger.isUpdate){
LoanPaymentTransaction_TriggerHandler.onAfterUpsertProcess(Trigger.new);
}
if(Trigger.isDelete){
LoanPaymentTransaction_TriggerHandler.onAfterDeleteProcess(Trigger.old);
}
}
}
Trigger Handler:
public class LoanPaymentTransaction_TriggerHandler {
public static void onAfterDeleteProcess(List<loan__Loan_Payment_Transaction__c> lstOldData){
Map<Id, Map<String, Integer>> mapCounts = new Map<Id, Map<String, Integer>>();
Map<String, Integer> mapRCount = new Map<String, Integer>();
Set<Id> setMasterIDs = new Set<Id>();
Map<Id, loan__Loan_Account__c> mapMasterRecords = new Map<Id, loan__Loan_Account__c>();
List<loan__Loan_Account__c> updateloanAcc = new List<loan__Loan_Account__c>();
for(loan__Loan_Payment_Transaction__c payT: lstOldData ){
if(payT.loan__Reversal_Reason__c != null){
setMasterIDs.add(payT.loan__Loan_Account__c); // Master Obj Ids
}
}
if(setMasterIDs.size() > 0){
List<loan__Loan_Account__c> lstLoanAccounts = queryMasters(setMasterIDs);
for(loan__Loan_Account__c lAcc: lstLoanAccounts){
mapMasterRecords.put(lAcc.Id, lAcc);
}
if(lstLoanAccounts.size() > 0){
for(loan__Loan_Account__c loanAcc: lstLoanAccounts){
for(loan__Loan_Payment_Transaction__c payT: loanAcc.loan__Loan_Payment_Transactions__r){
if(mapCounts.containsKey(loanAcc.Id)){
Map<String, Integer> innerMap = mapCounts.get(loanAcc.Id);
if(innerMap.containsKey(payT.loan__Reversal_Reason__c)){
Integer existingCount = innerMap.get(payT.loan__Reversal_Reason__c);
existingCount++; // Increasing current count
innerMap.put(payT.loan__Reversal_Reason__c, existingCount);
}
else{
innerMap.put(payT.loan__Reversal_Reason__c, 1); // adding current count
}
mapCounts.put(loanAcc.Id, innerMap);
}
else{
Map<String, Integer> innerMp = new Map<String, Integer>();
innerMp.put(payT.loan__Reversal_Reason__c, 1);
mapCounts.put(loanAcc.Id, innerMp);
}
/*
if(mapRCount.containsKey(payT.loan__Reversal_Reason__c)){
Integer existingCount = mapRCount.get(payT.loan__Reversal_Reason__c);
existingCount++; // Increasing current count
mapRCount.put(payT.loan__Reversal_Reason__c, existingCount);
}
else{
mapRCount.put(payT.loan__Reversal_Reason__c, 1); // adding current count
}
*/
}
}
// Update value to master
for(Id masterId : mapCounts.keySet()){
Map<String, Integer> inncounts = mapCounts.get(masterId);
loan__Loan_Account__c loanAccforUpdate = mapMasterRecords.get(masterId);
system.debug('loanAccforUpdate ==>>>'+loanAccforUpdate);
String formingDesc = '';
for(String cnt : inncounts.keySet()){
if(cnt != null)
formingDesc += cnt + ' - ' + inncounts.get(cnt) + '; ';
}
system.debug(' formingDesc ===========>>'+formingDesc+' ==>'+loanAccforUpdate);
loanAccforUpdate.Reason_Code_Snapshot__c = formingDesc;
system.debug(' loanAccforUpdate.Reason_Code_Snapshot__c ===========>>'+loanAccforUpdate.Reason_Code_Snapshot__c);
updateloanAcc.add(loanAccforUpdate);
}
update updateloanAcc;
}
}
}
public static void onAfterUpsertProcess(List<loan__Loan_Payment_Transaction__c> lstNewData){
Map<Id, Map<String, Integer>> mapCounts = new Map<Id, Map<String, Integer>>();
Map<String, Integer> mapRCount = new Map<String, Integer>();
Set<Id> setMasterIDs = new Set<Id>();
Map<Id, loan__Loan_Account__c> mapMasterRecords = new Map<Id, loan__Loan_Account__c>();
List<loan__Loan_Account__c> updateloanAcc = new List<loan__Loan_Account__c>();
system.debug('inside trigger');
for(loan__Loan_Payment_Transaction__c payT: lstNewData ){
// if(payT.loan__Reversal_Reason__c != null){
setMasterIDs.add(payT.loan__Loan_Account__c); // Master Obj Ids
// }
}
if(setMasterIDs.size() > 0){
List<loan__Loan_Account__c> lstLoanAccounts = queryMasters(setMasterIDs);
for(loan__Loan_Account__c lAcc: lstLoanAccounts){
mapMasterRecords.put(lAcc.Id, lAcc);
}
if(lstLoanAccounts.size() > 0){
for(loan__Loan_Account__c loanAcc: lstLoanAccounts){
for(loan__Loan_Payment_Transaction__c payT: loanAcc.loan__Loan_Payment_Transactions__r){
system.debug('inside lpt loop');
if(mapCounts.containsKey(loanAcc.Id)){
Map<String, Integer> innerMap = mapCounts.get(loanAcc.Id);
if(innerMap.containsKey(payT.loan__Reversal_Reason__c)){
Integer existingCount = innerMap.get(payT.loan__Reversal_Reason__c);
existingCount++; // Increasing current count
innerMap.put(payT.loan__Reversal_Reason__c, existingCount);
system.debug('blankreversalreasontest1');
}
else{
//innerMap.put(payT.loan__Reversal_Reason__c, 1); // adding current count // commenting for test - Annand
innerMap.put(payT.loan__Reversal_Reason__c, 1);
system.debug('blankreversalreason'+innerMap);
}
mapCounts.put(loanAcc.Id, innerMap);
}
else{
Map<String, Integer> innerMp = new Map<String, Integer>();
innerMp.put(payT.loan__Reversal_Reason__c, 1);
mapCounts.put(loanAcc.Id, innerMp);
system.debug('blankreversalreasontest2');
}
/*
if(mapRCount.containsKey(payT.loan__Reversal_Reason__c)){
Integer existingCount = mapRCount.get(payT.loan__Reversal_Reason__c);
existingCount++; // Increasing current count
mapRCount.put(payT.loan__Reversal_Reason__c, existingCount);
}
else{
mapRCount.put(payT.loan__Reversal_Reason__c, 1); // adding current count
}
*/
}
}
// Update value to master
for(Id masterId : mapCounts.keySet()){
Map<String, Integer> inncounts = mapCounts.get(masterId);
loan__Loan_Account__c loanAccforUpdate = mapMasterRecords.get(masterId);
system.debug('loanAccforUpdate ==>>>'+loanAccforUpdate);
String formingDesc = '';
for(String cnt : inncounts.keySet()){
if(cnt != null)
formingDesc += cnt + ' - ' + inncounts.get(cnt) + '; ';
}
system.debug(' formingDesc ===========>>'+formingDesc+' ==>'+loanAccforUpdate);
loanAccforUpdate.Reason_Code_Snapshot__c = formingDesc;
system.debug(' loanAccforUpdate.Reason_Code_Snapshot__c ===========>>'+loanAccforUpdate.Reason_Code_Snapshot__c);
updateloanAcc.add(loanAccforUpdate);
}
update updateloanAcc;
}
}
}
private static List<loan__Loan_Account__c> queryMasters(Set<Id> rIds){
List<loan__Loan_Account__c> lstloanAcc = [SELECT Reason_Code_Snapshot__c, Id, (SELECT loan__Reversal_Reason__c,loan__Loan_Account__c FROM loan__Loan_Payment_Transactions__r) FROM loan__Loan_Account__c WHERE Id IN : rIds];
return lstloanAcc;
}
}
Out Put: