trigger Updaterocord on ABC (after update,before update){
if(Trigger.isBefore){
if(Trigger.isUpdate){
for(ABC dl: Trigger.new){
if(Trigger.isAfter)
{
if(Trigger.isUpdate){
Set<String> leanAccNo = new Set<String>();
Map<String,Decimal> loanAccNoEmicountMap = new Map<String,Decimal>();
for( Demand_List__c dl: Trigger.new){
if(dl.External_DL_ID__c != null && dl.External_DL_ID__c != ''){
loanAccNoEmicountMap.put(dl.External_DL_ID__c,dl.EMI_Count__c);
leanAccNo.add(dl.External_DL_ID__c);
}
}
for(Client_Repayment_History__c obj : [select Loan_Account_No__c,Total_no_of_Meetings_conducted__c,Total_no_of_Meetings_attended__c from Client_Repayment_History__c where Loan_Account_No__c in :leanAccNo]){
LoanAccountMap.put(obj.Loan_Account_No__c,obj.Id);
updateRpHistObjList.add(obj);
}
for( Demand_List__c dl: Trigger.new){
// Inital Status: Not Collected Final Status: Full Collection/ Partial Collection/Excess Collection
if(dl.RecordTypeId == SBLoanDLRecordTypeId &&(dl.Product_Code_Formula__c ==Productcodefor814 || dl.Product_Code_Formula__c ==Productcodefor815 || dl.Product_Code_Formula__c == Productcodefor819 || dl.Product_Code_Formula__c ==Productcodefor802 || dl.Product_Code_Formula__c ==Productcodefor875 || dl.Product_Code_Formula__c ==Productcodefor876 ) && (dl.Collection_Status__c != Trigger.oldMap.get(dl.Id).Collection_Status__c && Trigger.oldMap.get(dl.Id).Collection_Status__c == notCollectedStatus && (dl.Collection_Status__c == CollectedStatus || dl.Collection_Status__c == excessCollectedStatus || dl.Collection_Status__c == partialCollectedStatus )) && (dl.SMC_customer_attendance__c != null) ){
//EMI Count =1
if(dl.EMI_Count__c == 1){
//insert Client_Repayment_History__c
Client_Repayment_History__c rphistObj = new Client_Repayment_History__c();
rphistObj.Loan_Account_No__c = dl.External_DL_ID__c;
rphistObj.Loans__c = dl.Loans__c;
rphistObj.Client_Name__c = dl.Clients__c;
rphistObj.Total_no_of_Meetings_conducted__c = 1;
if(LoanAccountMap.containsKey(dl.External_DL_ID__c)){
rphistObj.Id = LoanAccountMap.get(dl.External_DL_ID__c);
}
if( dl.SMC_customer_attendance__c == 'Yes'){
rphistObj.Total_no_of_Meetings_attended__c =1;
}else if( dl.SMC_customer_attendance__c == 'No'){
rphistObj.Total_no_of_Meetings_attended__c =0;
upsertRpHistObjList.add(rphistObj);
}
}
else{
loanAcctNoMap.put(dl.External_DL_ID__c,dl.SMC_customer_attendance__c);
}
}
if(dl.RecordTypeId == SBLoanDLRecordTypeId && (dl.Collection_Status__c == CollectedStatus || dl.Collection_Status__c == CollectedStatus) && dl.PAR_Bucket__c != null && dl.Clients__c != null && dl.SMS_Partial_Collected_Flag__c == false &&dl.Clients__r.Zone__c !='Duplicate' && dl.Clients__r.Zone__c !='Invalid' && dl.OD_Amt__c != null){
odset.add(dl.Clients__c);
}
} // End of for loop
System.debug('loanAcctNoMap===>'+loanAcctNoMap);
if(!loanAcctNoMap.isEmpty()){
//updateRpHistObjList = [select Loan_Account_No__c,Total_no_of_Meetings_conducted__c,Total_no_of_Meetings_attended__c from Client_Repayment_History__c where Loan_Account_No__c in :loanAcctNoMap.keySet()];
for(Client_Repayment_History__c rphistObj : updateRpHistObjList){
if(!loanAccNoEmicountMap.isempty() && loanAccNoEmicountMap.containsKey(rphistObj.Loan_Account_No__c) && loanAccNoEmicountMap.get(rphistObj.Loan_Account_No__c) != 1){
rphistObj.Total_no_of_Meetings_conducted__c += 1;
if(loanAcctNoMap.get(rphistObj.Loan_Account_No__c) == 'Yes')
rphistObj.Total_no_of_Meetings_attended__c += 1;
upsertRpHistObjList.add(rphistObj);
}
}
}
if(!upsertRpHistObjList.isEmpty()){
system.debug('@@@@ Upsert Repayment History');
upsert upsertRpHistObjList ;
}
}// End of isUpdate
} // End of isAfter
}
}
}
Refrer map loanAccNoEmicountMap in above trigger
if(Trigger.isBefore){
if(Trigger.isUpdate){
for(ABC dl: Trigger.new){
if(Trigger.isAfter)
{
if(Trigger.isUpdate){
Set<String> leanAccNo = new Set<String>();
Map<String,Decimal> loanAccNoEmicountMap = new Map<String,Decimal>();
for( Demand_List__c dl: Trigger.new){
if(dl.External_DL_ID__c != null && dl.External_DL_ID__c != ''){
loanAccNoEmicountMap.put(dl.External_DL_ID__c,dl.EMI_Count__c);
leanAccNo.add(dl.External_DL_ID__c);
}
}
for(Client_Repayment_History__c obj : [select Loan_Account_No__c,Total_no_of_Meetings_conducted__c,Total_no_of_Meetings_attended__c from Client_Repayment_History__c where Loan_Account_No__c in :leanAccNo]){
LoanAccountMap.put(obj.Loan_Account_No__c,obj.Id);
updateRpHistObjList.add(obj);
}
for( Demand_List__c dl: Trigger.new){
// Inital Status: Not Collected Final Status: Full Collection/ Partial Collection/Excess Collection
if(dl.RecordTypeId == SBLoanDLRecordTypeId &&(dl.Product_Code_Formula__c ==Productcodefor814 || dl.Product_Code_Formula__c ==Productcodefor815 || dl.Product_Code_Formula__c == Productcodefor819 || dl.Product_Code_Formula__c ==Productcodefor802 || dl.Product_Code_Formula__c ==Productcodefor875 || dl.Product_Code_Formula__c ==Productcodefor876 ) && (dl.Collection_Status__c != Trigger.oldMap.get(dl.Id).Collection_Status__c && Trigger.oldMap.get(dl.Id).Collection_Status__c == notCollectedStatus && (dl.Collection_Status__c == CollectedStatus || dl.Collection_Status__c == excessCollectedStatus || dl.Collection_Status__c == partialCollectedStatus )) && (dl.SMC_customer_attendance__c != null) ){
//EMI Count =1
if(dl.EMI_Count__c == 1){
//insert Client_Repayment_History__c
Client_Repayment_History__c rphistObj = new Client_Repayment_History__c();
rphistObj.Loan_Account_No__c = dl.External_DL_ID__c;
rphistObj.Loans__c = dl.Loans__c;
rphistObj.Client_Name__c = dl.Clients__c;
rphistObj.Total_no_of_Meetings_conducted__c = 1;
if(LoanAccountMap.containsKey(dl.External_DL_ID__c)){
rphistObj.Id = LoanAccountMap.get(dl.External_DL_ID__c);
}
if( dl.SMC_customer_attendance__c == 'Yes'){
rphistObj.Total_no_of_Meetings_attended__c =1;
}else if( dl.SMC_customer_attendance__c == 'No'){
rphistObj.Total_no_of_Meetings_attended__c =0;
upsertRpHistObjList.add(rphistObj);
}
}
else{
loanAcctNoMap.put(dl.External_DL_ID__c,dl.SMC_customer_attendance__c);
}
}
if(dl.RecordTypeId == SBLoanDLRecordTypeId && (dl.Collection_Status__c == CollectedStatus || dl.Collection_Status__c == CollectedStatus) && dl.PAR_Bucket__c != null && dl.Clients__c != null && dl.SMS_Partial_Collected_Flag__c == false &&dl.Clients__r.Zone__c !='Duplicate' && dl.Clients__r.Zone__c !='Invalid' && dl.OD_Amt__c != null){
odset.add(dl.Clients__c);
}
} // End of for loop
System.debug('loanAcctNoMap===>'+loanAcctNoMap);
if(!loanAcctNoMap.isEmpty()){
//updateRpHistObjList = [select Loan_Account_No__c,Total_no_of_Meetings_conducted__c,Total_no_of_Meetings_attended__c from Client_Repayment_History__c where Loan_Account_No__c in :loanAcctNoMap.keySet()];
for(Client_Repayment_History__c rphistObj : updateRpHistObjList){
if(!loanAccNoEmicountMap.isempty() && loanAccNoEmicountMap.containsKey(rphistObj.Loan_Account_No__c) && loanAccNoEmicountMap.get(rphistObj.Loan_Account_No__c) != 1){
rphistObj.Total_no_of_Meetings_conducted__c += 1;
if(loanAcctNoMap.get(rphistObj.Loan_Account_No__c) == 'Yes')
rphistObj.Total_no_of_Meetings_attended__c += 1;
upsertRpHistObjList.add(rphistObj);
}
}
}
if(!upsertRpHistObjList.isEmpty()){
system.debug('@@@@ Upsert Repayment History');
upsert upsertRpHistObjList ;
}
}// End of isUpdate
} // End of isAfter
}
}
}
Refrer map loanAccNoEmicountMap in above trigger
No comments:
Post a Comment