+ Start a Discussion
Amrin.Amrin. 

Urgent!!!!! How to call method from another class in my extention

Suppose there is a VF

 

<apex:page standardController="Acc" extensions="Acc_Ext">

    <apex:commandButton action={!Run} value="Run">

</apex:page>

 

In Acc_Ext I want to call a method from another class i.e Sub_Acc_Ext. And Sub_Acc_Ext has got method as Run()

How to call this Run() in my Acc_Ext class????

 

Please help!!!!!!!!!!!!!!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can't execute static methods from an instance - in your extension, change this:

 

ENT_NMTC_SubAllProcessor ae= new ENT_NMTC_SubAllProcessor();
ae.RunTest();

 to:

 

ENT_NMTC_SubAllProcessor.RunTest();

 

 

 

All Answers

bob_buzzardbob_buzzard

Does Acc_Ext contain an instance of Sub_Acc_Ext?  If it does, you can simply execute the method.

GsajeeshGsajeesh

Create a reference of Sub_Acc_ext in Acc_Ext class

 and call the run method using that reference

 

public Class Acc_Ext{

 

public Pagereference Run(){

 sub_Acc_Ext sab= new sub_Acc_Ext();

sab.run();

return null;

}

 

}

 

Hope this helps!

Amrin.Amrin.

Thanks Bob and Gsajeesh for ur reply... :)

 

Its still giving me error as 

 Method does not exist or incorrect signature: [sub_Acc_Ext].Run()

GsajeeshGsajeesh

Is run() in Sub_Acc_Ext class a private method by any chance?

bob_buzzardbob_buzzard

It would be easier to try to diagnose the problem if you can post your code.

Amrin.Amrin.

Nope its  public

Amrin.Amrin.

Hi Bob,

 

This is my real code.. 

Requirement is, On button click "Run Now " Action method should call method "RunTest" from ENT_NMTC_SubAllProcessor class. Which will further create a record for ENT_NMTC_SubAll__c  object. 

 

VF: 

<apex:page standardController="ENT_NMTC_QEI_Contribution__c" extensions="QEI_Contribution_Ext">
<apex:form >
<apex:sectionHeader title="NMTC QEI Contribution Detail"/>
<apex:pageblock title="QEI Contribution Detail" id="projectBlock">

<apex:pageblockbuttons >
<apex:commandButton action="{!Edit}" value="Edit" id="buttonEdit"/>
<apex:commandButton action="{!Delete}" value="Delete" id="buttonDelete"/>

</apex:pageblockbuttons>

<apex:messages style="Color:red;"/>

<apex:pageblocksection title="Information" id="newProjectBlock" columns="2">
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Name}" /><br/>

<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.NMTC_CDE__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Contribution_Date__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.QEI_Identifier__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.X8874A_filed__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.QEI_Registered_in_ATS__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Comments__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.SubAll_Test_Auto_Run__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Amount__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Leverage_Loan_vs_Equity__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.End_of_NMTC_Compliance_Period__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.X1st_Anniversary_Date__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.X6th_year_Anniversary_Date__c}" />
</apex:pageblocksection>
<apex:pageblocksection title="Contributor" columns="2">
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.NMTC_Fund__c}" />
</apex:pageblocksection>
<apex:pageblocksection title="Reservation" columns="2">
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.NMTC_QEI_Reservation__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Reservation_Amount_QEI__c}" />
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Reservation_Date__c}" />
</apex:pageblocksection>
<apex:pageblocksection title="Totals" columns="2">
<apex:outputField value="{!ENT_NMTC_QEI_Contribution__c.Total_Tax_Credits__c}" />
</apex:pageblocksection>


</apex:pageblock>
</apex:form>


<apex:relatedList list="NMTC_QEI_Disbursements__r" />
<apex:relatedList list="NMTC_QEI_Principle_Repayments__r" />
<apex:relatedList list="OpenActivities" />
<apex:relatedList list="ActivityHistories" />


<apex:form >
<apex:pageblock title="Sub All" >
<apex:pageblockbuttons >

<apex:commandButton action="{!RunNow}" value="Run Now">
<apex:param name="contribution" value="{!ENT_NMTC_QEI_Contribution__c.Id}" assignTo="{!contribution}"/>
<apex:param name="contributionDate" value="{!ENT_NMTC_QEI_Contribution__c.Contribution_Date__c}" assignTo="{!ContributionDate}"/>
</apex:commandButton>


</apex:pageblockbuttons>
</apex:pageblock>
</apex:form>
<apex:relatedList list="NMTC_SubAll__r" />

</apex:page>

 

Extention:

 

public class QEI_Contribution_Ext {

public QEI_Contribution_Ext(ApexPages.StandardController controller) {

}
public void RunNow(){
ENT_NMTC_SubAllProcessor ae= new ENT_NMTC_SubAllProcessor();
ae.RunTest();

}

}

 

Another Class:

public class ENT_NMTC_SubAllProcessor 
{

    public ENT_NMTC_SubAllProcessor(ApexPages.StandardController controller) {
  
    }

    //public Integer TILL_SIXTH_YEAR_PERCENT{get;set;}
    //public Integer SEVENTH_YEAR_PERCENT{get;set;}
    
    public ENT_NMTC_SubAllProcessor() 
    {
        //TILL_SIXTH_YEAR_PERCENT = 85;
        //SEVENTH_YEAR_PERCENT = 75;
    }
    
    // Given a contribution and Test Date, this method Runs the Sub All Business Test. As the result, it creates the custom object NMTC Sub All.
    public static ENT_NMTC_SubAll__c RunTest(ENT_NMTC_QEI_Contribution__c contribution, Date testDate)
    {
          Date contributionDate = contribution.Contribution_Date__c;
    
          ENT_NMTC_SubAll__c subAll = new ENT_NMTC_SubAll__c();
          
          // Assign test date
          subAll.Test_Date__c = testDate;
          // Assign Contribution
          subAll.NMTC_QEI_Contribution__c = contribution.Id;
          // Assign QEI Amount from the QEI contribution amount
          subAll.QEI_Amount__c = contribution.Amount__c;
          
          // BUSINESS RULE 1: If the test date is less than the first anniversary of Contribution Date then the Test Result is always PASS.
          if(testDate < contributionDate.addYears(1))
          {
                subAll.Passed__c = true;
                subAll.Invested__c = 100;
                
                subAll.Total_Disburstment__c = 0;
                subAll.Amount_Invested__c = 0;
                subAll.Total_Repayment__c = 0;
                //subAll.UnInvested_Amount__c = 0;
                
                return subAll;
          }
          
          //////////////////////////////////////
          // CALCULATIONS
          //////////////////////////////////////
          
          Decimal contributionAmount = contribution.Amount__c;
          Decimal toalDisbursement = 0;
          Decimal toalEffectiveRepayment = 0;
          Decimal totalInvested = 0;
          Decimal totalUnInvested = 0;
          Decimal investedPercent = 0;
          Boolean testPass = false;
          
          // Calculate Disbursement Amount
          List<ENT_NMTC_QEI_Disbursement__c> listDisursements = GetEffectiveDisbursement(contribution, testDate);
          if(listDisursements == null || (listDisursements !=null && listDisursements.size() ==0))
          {
              toalDisbursement = 0;
          }
          else
          {
              for(ENT_NMTC_QEI_Disbursement__c disb : listDisursements )
              {
                  toalDisbursement = toalDisbursement + disb.Amount__c;
              }
          } 
          
          // Calculate Effective Repayment Amount 
          List<ENT_NMTC_QEI_Principle_Repayment__c> listEffRepayment = GetEffectiveRepayments(contribution, testDate);
          if(listEffRepayment == null || (listEffRepayment !=null && listEffRepayment.size() ==0))
          {
              toalEffectiveRepayment = 0;
          }
          else
          {
              for(ENT_NMTC_QEI_Principle_Repayment__c repayment : listEffRepayment)
              {
                  toalEffectiveRepayment = toalEffectiveRepayment + repayment.Amount__c;
              }
          }
          
          totalInvested = toalDisbursement - toalEffectiveRepayment;
          //totalUnInvested = contributionAmount - totalInvested;
          investedPercent =  totalInvested/contributionAmount*100;
          
          subAll.Total_Disburstment__c = toalDisbursement ;
          subAll.Amount_Invested__c = totalInvested;
          subAll.Total_Repayment__c = toalEffectiveRepayment;
          //subAll.UnInvested_Amount__c = totalUnInvested;   
          
          // BUSINESS RULE 2: Between 1st Anniversay and till 6th Year, the Invested Rule of 85%
          if(testDate >= contributionDate.addYears(1) && testDate < contributionDate.addYears(6))
          {
              //if(investedPercent >= TILL_SIXTH_YEAR_PERCENT)
              if(investedPercent >= 85)
              {
                  testPass = true;
              }
          }
          
          if(testDate >= contributionDate.addYears(6))
          {
             //if(investedPercent >= SEVENTH_YEAR_PERCENT)
              if(investedPercent >= 75)
              {
                  testPass = true;
              }
          }
          
          subAll.Invested__c = investedPercent;
          subAll.Passed__c = testPass;
          
          insert subAll;   
         
          return subAll;
    }
    
    private static List<ENT_NMTC_QEI_Disbursement__c> GetEffectiveDisbursement(ENT_NMTC_QEI_Contribution__c contribution, Date subAllTestDate)
    {
       List<ENT_NMTC_QEI_Disbursement__c> listDisursements = [SELECT Name, Amount__c, Comments__c,  Funded_Entity__c,
                                                              NMTC_Product__c, NMTC_QEI_Contribution__c, Request_Date__c, 
                                                              Status__c, Transaction_Date__c  
                             FROM ENT_NMTC_QEI_Disbursement__c
                             WHERE 
                                   NMTC_QEI_Contribution__c =: contribution.Id
                                   //AND Transaction_Date__c <= subAllTestDate
                             ];
                             
      return listDisursements;                        
    }
    
    private static List<ENT_NMTC_QEI_Principle_Repayment__c> GetEffectiveRepayments(ENT_NMTC_QEI_Contribution__c contribution, Date subAllTestDate)
    {
        List<ENT_NMTC_QEI_Principle_Repayment__c> listRepayment = [SELECT Name, Amount__c, Comments__c, Funded_Entity__c,
                                                                  NMTC_Product__c, NMTC_QEI_Contribution__c, Request_Date__c, 
                                                                  Status__c, Transaction_Date__c, X1st_Anniversary_Date__c  
                                 FROM ENT_NMTC_QEI_Principle_Repayment__c
                                 WHERE NMTC_QEI_Contribution__c =: contribution.Id 
                                   //  AND X1st_Anniversary_Date__c <= subAllTestDate
                             ];
                                     
        return listRepayment;                                 
    }
}

bob_buzzardbob_buzzard

You can't execute static methods from an instance - in your extension, change this:

 

ENT_NMTC_SubAllProcessor ae= new ENT_NMTC_SubAllProcessor();
ae.RunTest();

 to:

 

ENT_NMTC_SubAllProcessor.RunTest();

 

 

 

This was selected as the best answer
Amrin.Amrin.
Hi Bob, Thank you so much !!!!!!!!! it worked for me. Below is the code i changed it to public class QEI_Contribution_Ext { ENT_NMTC_QEI_Contribution__c contribution; public QEI_Contribution_Ext(ApexPages.StandardController controller) { contribution = (ENT_NMTC_QEI_Contribution__c)controller.getRecord(); } public void RunNow() { ENT_NMTC_SubAll__c subAll = ENT_NMTC_SubAllProcessor.RunTest(contribution, date.today()); } }