 • defray user
• NEWBIE
• 10 Points
• Member since 2019

• Chatter
Feed
• 0
• 0
• 0
Likes Given
• 2
Questions
• 0
Replies
trigger emiTrigger on EMI_Payment__c (after insert) {
Map<ID, Loans__c> parentLoans = new Map<ID, Loans__c>();
List<id> listIds = new List<id>();
Double Principal_Outstanding_at_the_end_of_the_month;

for(EMI_Payment__c e : Trigger.new){
}
//getting parent record ID and data with above ID
parentLoans = new Map<Id,Loans__c>([SELECT id,paid_amount__c,Loan_Amount__c,TenureYrs__c,
Interest_Rate__c,Outstanding_Principal__c,(SELECT id,amount__c from
EMI_Payment__r) from Loans__c  where ID IN : listIds ]);
//updating paid amount with EMI
for(EMI_Payment__c e:Trigger.new){
Loans__c myloan = parentLoans.get(e.Loan_Id__c);

Double  principal = myloan.Loan_Amount__c;
Decimal tenure = myloan.TenureYrs__c*12;
Decimal interest = myloan.Interest_Rate__c/12/100;
Decimal pr = 1 + interest;
Double EMI =  principal * interest * Math.pow(pr.doubleValue(),tenure.doubleValue())/(Math.pow(pr.doubleValue(),tenure.doubleValue())-1);

Double Interest_Component_of_EMI = interest * myloan.Outstanding_Principal__c;
Double Principal_Component_of_EMI = Math.round(EMI - Interest_Component_of_EMI);
Principal_Outstanding_at_the_end_of_the_month = myloan.Outstanding_Principal__c - Principal_Component_of_EMI;

myloan.paid_amount__c += e.amount__c; //incrementing paid amount
}

if(parentLoans.size()>0){
upsert parentLoans.values();  // error occurring here
}
}
• • December 18, 2019
• Like
• 0
trigger emiTrigger on EMI_Payment__c (after insert) {
Set<id> loanid = new Set<id>();
List<Loans__c> loanslist = new List<Loans__c>();
for(EMI_Payment__c e : Trigger.new){
}
for(Loans__c loan : [select id,Name,EMI__c,paid_amount__c,(select id,amount__c from EMI_Payment__r) from Loans__c where id IN : loanid]){
for(EMI_Payment__c e:loan.EMI_Payment__r){
//ading Emi amount to old paid amount , its giving worng value
loan.paid_amount__c = loan.paid_amount__c +e.amount__c;
system.debug('updated for...'+loan.paid_amount__c+'...>'+ e.amount__c);
}
}
update loanslist;
system.debug('updated ...');
}

==================================================
Loans__c  ==> parent object
EMI_Payment__c  ==> child object
trying to update   loan.paid_amount__c with new EMI payment amount but its coming up with wrong value but if I debug for same value its showing correct in logs

Example : Assume 45281 is a initial value of loan.paid_amount__c
and next if I give 45281 as input  for e.amount__c
expected to happen -
loan.paid_amount__c = loan.paid_amount__c +e.amount__c;
45281 = 45281 + 45281; ==> 96502
but its coming up with ==> 144,753

I am not understanding why ?

• • December 13, 2019
• Like
• 0