function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Revati BhavsarRevati Bhavsar 

How to display the value from different apex class on a visualforce page with different controller?

I have created an apex class for EMI_calculation to estimate EMI for a particular loan applicant and it is stored in variable EMI. I want to display this
EMI value on visualforce page "Applicant" with different controller class after saving th applicant page.
Following is the EMI_Calculation class:
public with sharing class EMI_Calculation
{
  public CustBusinessPro__Applicant__c applicant{get;set;}
  public Decimal RateOfInterest{get;set;}
  public Decimal EMI{get;set;}
  
  
  public void EMICalculation(Decimal Principal, Decimal Period)
  {
    applicant = new CustBusinessPro__Applicant__c();
    RateOfInterest = 12.5/1200;
    System.debug('----------' + Principal);
    
    System.debug('----------' + Period);
    Period=Period*12.0;
    Integer myInt = Integer.valueOf(Period);
    Decimal nr = (1+RateOfInterest);
    
    Decimal powDec = nr.pow(myInt);
    EMI= (Principal*RateOfInterest*powDec)/(powDec-1);
    System.debug('-------------' + EMI + 'Rs');
    
  }
}

 
Mahesh DMahesh D
Hi Revati,

You can create an object to the above class and use it in your Visualforce page.

You can create a member variable of EMI_Calculation in your other controller and you can able to access all its member variable and methods.

Please do let me know if it helps you.

Regards,
Mahesh

 
Revati BhavsarRevati Bhavsar
I tried your solution but it's giving error on visualforce page when I'm trying to access the EMI value on page.
Mahesh DMahesh D
Hi Revati,

Please provide your VF and class files so that we will help you better.

Regards,
Mahesh
Revati BhavsarRevati Bhavsar
Attached are the controller class and vfpage files. Thanks and regards, Revati
Revati BhavsarRevati Bhavsar
public with sharing class ApplicantController {
public CustBusinessPro__Applicant__c applicant{get;set;}
PAN_Check p = new PAN_Check();
CIBILVerification c = new CIBILVerification();
EligibilityVerification el = new EligibilityVerification();
EMI_Calculation e = new EMI_Calculation();
De_Dup d = new De_Dup();
public List < CustBusinessPro__Criteria__c > criteriaList {get;set;}
public CustBusinessPro__Criteria__c Criteria {get;set;}
public String testWhere;

   public ApplicantController()
  {
    applicant = new Applicant__c();
  }

 
public void randomWithLimit(){
        
        Integer rand = Math.round(Math.random()*1000);
        if(rand > 680 && 1000 > rand)
        {
        
        applicant.CustBusinessPro__CIBIL_Score__c= rand;
        }
       
}

  public PageReference save() {
                 
           upsert applicant;
           p.PanCheck(applicant.CustBusinessPro__PAN__c,applicant.CustBusinessPro__Last_Name__c);
          c.CIBILCheck(applicant.CustBusinessPro__CIBIL_Score__c);
           el.EligibilityCheck(applicant.CustBusinessPro__Salary__c, applicant.CustBusinessPro__Type_of_Loan__c, applicant.CustBusinessPro__Loan_Amount__c);
           e.EMICalculation(applicant.CustBusinessPro__Loan_Amount__c,applicant.CustBusinessPro__Loan_Tenure__c);
             d.DeDup(applicant.CustBusinessPro__PAN__c,applicant.CustBusinessPro__Type_of_Loan__c);
           return new PageReference('/' + applicant.Id);
         
        
    }

}
 
<apex:pageBlockSection title=" 4. Loan Details" collapsible="true" columns="1">
             <div class="table-responsive">
            <table class="table table-hover">
            <tr>
                <td><apex:inputField required="true"  value="{!applicant.Type_of_Loan__c}" styleClass="form-control" /></td>
                <td><apex:inputField required="true"  value="{!applicant.Loan_Amount__c}" styleClass="form-control" /></td>
                <td><apex:inputField required="true"  value="{!applicant.Loan_Tenure__c}" styleClass="form-control" /></td>
            </tr>           
             </table>
             <apex:outputpanel id="amount">
              Loan status: <apex:outputText />
                <br/>
                <br/>
                EMI amount: <apex:outputText value="{!EMI}" rerender="{amount}"/>
               <br/>  
               </apex:outputpanel>
            </div>
            </apex:pageBlockSection>

 
Mahesh DMahesh D
Hi Revati,

You didn't copy the full VF page and also what error you are getting.

Regards,
Mahesh
Revati BhavsarRevati Bhavsar
<apex:page controller="ApplicantController" showHeader="false" sidebar="false" docType="html-5.0" tabStyle="Applicant__c"  >
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"/>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>-->

<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>

  <div style="width:50%;margin-left:25%;">
  
  
<body>
    <apex:outputPanel styleClass="navbar navbar-inverse navbar-fixed-top" layout="block" html-role="navigation">
      <apex:outputPanel styleClass="container" layout="block">
        <apex:outputPanel styleClass="navbar-header" layout="block">
       
          
          <a class="navbar-brand" href="#"></a>
        </apex:outputPanel>
     
  <style>
.myFormStyle{
      background-color: #e6f7ff;

 }
 </style>
  <apex:form styleClass="myFormStyle">

   <strong><apex:pagemessages /></strong>  

 <!-- <style>
  .form-group.required .control-label:before {
content:"*";
color:red;
font-size:2em;
}
</style>-->

<br/>
<br/>


<h1>Applicant Detail Form</h1>

     <apex:pageBlock rendered="{!$User.UITheme == 'Theme3' && $User.UIThemeDisplayed == 'Theme3'}">
     <apex:pageBlockSection title="  1. Demographic Details" collapsible="true" columns="1">
             <div class="table-responsive">
            <table class="table table-hover" background="transparent">
            <tr> 
                             
                <td>
                <apex:inputField required="true" value="{!applicant.First_Name__c}" html-placeholder="First Name" styleClass="form-control" />
               
                </td>
             
                <td>
                <apex:inputField required="true" value="{!applicant.Last_Name__c}" html-placeholder="Last Name" styleClass="form-control"/>
                </td>

                <td><apex:inputField required="true" value="{!applicant.AccountNo__c}" html-placeholder="Enter your Account Number" styleClass="form-control"/></td>
               
                <td><apex:inputField required="true" value="{!applicant.Email__c}" html-placeholder="Enter your email-id" styleClass="form-control"/></td>

                <td><apex:inputField required="true" value="{!applicant.PAN__c}" html-placeholder="Enter your Permanent Account Number" styleClass="form-control"/></td>

                <td><apex:inputField required="false" value="{!applicant.Aadhar_ID__c}" html-placeholder="Enter your Aadhar Card Number" styleClass="form-control"/></td>

                <td><apex:inputField required="false" value="{!applicant.Passport_Number__c}" html-placeholder="Enter your Passport Number" styleClass="form-control"/></td>

                <td><apex:inputField required="true"  value="{!applicant.PhoneNo__c}" html-placeholder="Enter your Mobile Number" styleClass="form-control"/></td>

                <script>
  (function() {
    var windowOnload = window.onload;
    window.onload = function() {
      if (windowOnload) windowOnload();
      var select = document.getElementById('calYearPicker');
      if (!select) return;

      select.innerHTML = '';
      var startYear = new Date().getFullYear() - 90;
      for (var year = startYear; year < startYear + 100; year++) {
        select.options[select.options.length] = new Option(year, year);
      }
    }
  }());
</script>
               
                <td><apex:inputField required="true"  value="{!applicant.DOB__c}" type="date" showDatePicker="false" styleClass="form-control" /></td>

                <td><apex:inputField required="true" value="{!applicant.Age__c}" html-placeholder="Enter your current Age" styleClass="form-control"/></td>

                <td><apex:inputField required="true"  value="{!applicant.Gender__c}" styleClass="form-control"/></td>

                <td><apex:inputField required="true"  value="{!applicant.Marital_Status__c}" styleClass="form-control"/></td>


                <td><apex:inputField required="true"  value="{!applicant.Employment_Status__c}" styleClass="form-control" /></td>

            </tr>
            </table>
           </div>
             </apex:pageBlockSection>
            <apex:pageBlockSection title="  2. Address" collapsible="true" columns="1">
             <div class="grid">
            <table class="table table-hover">
            <tr>

                <td><apex:inputField required="true"  value="{!applicant.Address__c}" styleClass="form-control" html-placeholder="Enter your Residential Address"/></td>
                
                 <td><apex:inputField required="true"  value="{!applicant.City__c}" html-placeholder="Enter City you currently live in" styleClass="form-control"/></td>

                <td><apex:inputField required="true"  value="{!applicant.State__c}" html-placeholder="Enter state" styleClass="form-control"/ ></td>
                 <td><apex:inputField required="true"  value="{!applicant.Country__c}" html-placeholder="Enter Country you currently live in " styleClass="form-control"/></td>
                 <td><apex:inputField required="true"  value="{!applicant.Postal_Code__c}" html-placeholder="Enter valid Postal Code" styleClass="form-control"/></td>
                
                <td><apex:inputField required="false" value="{!applicant.Work_Address__c}" styleClass="form-control" html-placeholder="Enter Address of your Work Place"/></td>


            </tr>
             </table>
            </div>
             </apex:pageBlockSection>

            <apex:pageBlockSection title="  3. Financial Details" collapsible="true" columns="1">
             <div class="grid">
            <table class="table table-hover">
             <tr>


                <td><apex:inputField required="false" value="{!applicant.JobProfile__c}" html-placeholder="Enter your Job Profile/Designation" styleClass="form-control" /></td>

         
                <td><apex:inputField required="true"  value="{!applicant.Salary__c}" html-placeholder="Enter Salary Per Annum" styleClass="form-control"/></td>

   
                <td><apex:inputCheckbox value="{!applicant.Click_if_spouse_is_working__c}"/></td>

   

                <!--<th>If yes, his/her salary?(per annum)</th>-->

                <td><apex:inputField value="{!applicant.If_yes_his_her_monthly_income__c}" html-placeholder="Salary of spouse per annum" styleClass="form-control"/></td>

            </tr>
            
            </table>
            </div>
            </apex:pageBlockSection>

            <apex:pageBlockSection title=" 4. Loan Details" collapsible="true" columns="1">
             <div class="table-responsive">
            <table class="table table-hover">
            
            <!--<button onClick ="myfunction()"> Get my CIBIL Score </button>
            <p id="demo"></p>
            <script>
            function randomRange(680,1000):Number 
{
    return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
           
            <tr>
                <td><apex:inputField required="true"  value="{!applicant.Type_of_Loan__c}" styleClass="form-control" /></td>
                <td><apex:inputField required="true"  value="{!applicant.Loan_Amount__c}" styleClass="form-control" /></td>
                <td><apex:inputField required="true"  value="{!applicant.Loan_Tenure__c}" styleClass="form-control" /></td>

            </tr>
                    
                      
             </table>
             <apex:outputpanel id="amount">

              Loan status: <apex:outputText />
                <br/>
                
                <br/>

                EMI amount: <apex:outputText value="{!EMI}"/>
               <br/>  
               
               </apex:outputpanel>
   
            </div>
            </apex:pageBlockSection>
       
            </apex:pageBlock> 

        <div align="center" draggable="false">
      
        <apex:commandButton action="{!save}" value="Save" style="float:centre" styleClass="btn btn-default" rerender="amount"/>
        
        </div> 

 </apex:form>
</div>

</apex:page>

 
Revati BhavsarRevati Bhavsar
Now the page is getting saved but EMI value is not being rendered. I added following lines to my controller
 public String getEMI() {
    EMI_Calculation e = new EMI_Calculation();

        return String.ValueOf(e.EMI);
    }