+ Start a Discussion
Catie LeisingCatie Leising 

Creating NPV and IRR fields using Apex

I am working on setting up some fields on an opportunity to capture Net Present Value (NPV) and IRR and I believe that code is the best way to do that.  I am very new to coding and I was hoping someone might have existing code I could modify to work for our situation.  Does anyone have code that I might be able to see and/or know of someone who might be able to help?  
Best Answer chosen by Catie Leising
AmulAmul
Controller Class:

public class PMTFunctionCntrl {

    public PageReference calculate() {
    
     try{
     
       if(inputRate==null || inputAmount==null || inputperiod==null)
       {
         return null;
       }
       double amount = double.valueof(inputAmount);
       double Rate = double.valueof(inputRate);
       double period = double.valueof(inputperiod);
       double result1= math.pow(1+(Rate/100),period);
       double xResult1= (result1*(Rate/100));
       double result2= (math.pow((1+(Rate/100)),period)-1);
       double finalResult1 = (amount*(xResult1/result2));
       amtResult=finalResult1 ;
     
     }catch(exception ex){
       return null;
     }  
        return null;
    }
    
    public double inputAmount{get;set;}
    public double inputperiod{get;set;}
    public double inputRate{get;set;}
    public double amtResult{get;set;}

}

All Answers

AmulAmul
VF Page for PMT:


<apex:page controller="PMTFunctionCntrl" docType="HTML-5.0"> <apex:form id="changeStatusForm"> <apex:pageBlock > <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Calculate" action="{!calculate}" reRender="result"/> </apex:pageBlockButtons> <apex:pageBlockSection columns="2"> <apex:outputText value="Rate"></apex:outputText> <apex:input value="{!inputRate}" id="theTextInputRate" type="number"/> <apex:outputText value="Period"></apex:outputText> <apex:input value="{!inputperiod}" id="theTextInputPrd" type="number"/> <apex:outputText value="Amount" ></apex:outputText> <apex:input value="{!inputAmount}" id="theTextInputAmt" type="number"/> </apex:pageBlockSection> <apex:pageBlockSection columns="2" id="result"> <apex:outputText value="Amortization Amount"></apex:outputText> <apex:outputText value="{!amtResult}"></apex:outputText> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>



 
AmulAmul
Controller Class:

public class PMTFunctionCntrl {

    public PageReference calculate() {
    
     try{
     
       if(inputRate==null || inputAmount==null || inputperiod==null)
       {
         return null;
       }
       double amount = double.valueof(inputAmount);
       double Rate = double.valueof(inputRate);
       double period = double.valueof(inputperiod);
       double result1= math.pow(1+(Rate/100),period);
       double xResult1= (result1*(Rate/100));
       double result2= (math.pow((1+(Rate/100)),period)-1);
       double finalResult1 = (amount*(xResult1/result2));
       amtResult=finalResult1 ;
     
     }catch(exception ex){
       return null;
     }  
        return null;
    }
    
    public double inputAmount{get;set;}
    public double inputperiod{get;set;}
    public double inputRate{get;set;}
    public double amtResult{get;set;}

}
This was selected as the best answer