You need to sign in to do that
Don't have an account?
defray user
emiTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.emiTrigger: line 36, column 1
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){
listIds.add(e.Loan_Id__c);//getting EMI ID(s)
}
//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.Outstanding_Principal__c-= Principal_Component_of_EMI;//decrementing outstading balance
myloan.paid_amount__c += e.amount__c; //incrementing paid amount
}
if(parentLoans.size()>0){
upsert parentLoans.values(); // error occurring here
}
}
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){
listIds.add(e.Loan_Id__c);//getting EMI ID(s)
}
//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.Outstanding_Principal__c-= Principal_Component_of_EMI;//decrementing outstading balance
myloan.paid_amount__c += e.amount__c; //incrementing paid amount
}
if(parentLoans.size()>0){
upsert parentLoans.values(); // error occurring here
}
}
Hope that your day is off to an amazing start. We've assessed your code and hope it helps. Please test and report back to us when you get the chance. Hope this helps and may God bless you abundantly.
Best Regards,
Anthony McDougald