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
aam1raam1r 

Call Visualforce Page Method from Apex Class

Call Visualforce Page Method from Apex Class

Hi everyone,
Is it possible to call a VF page from another apex class?  I currently have a vf page that generates a document and emails it to the contact on an opportunity.  This is executed via an action button.
I have created a lightning component that enlists filtered opportunities and I want to click a button to send the above generated documents, per opportunity to their linked contacts.  
The VF page has the following variables:
<apex:variable var="CompanyName" value="{!CompanyName}" />
<apex:variable var="OppId" value="{!OppId}" />
<apex:variable var="year" value="{!year}" />
<apex:variable var="emailStr" value="{!emailStr}" />
The VF page controller has the following class members:
    public String CompanyName {get;set;}
    public String OppId {get;set;}
    public Integer year {get;set;}
    public String emailStr {get;set;}
and has the following method in the VF page controller is what I want to call that creates the document and sends the email:
public PageReference CreateAndSendPdf() {
I need to be able to set the above parameters and then call the method from the new batch class.  What I’ve tried so far is:
if (lstValidOpps.size() > 0){
   for (Opportunity o : lstValidOpps){
                        
       PageReference pr = new PageReference('/apex/vfPage');
       // set query string parameters and values for the page
       pr.getParameters().put('oppId',o.Id);
       pr.getParameters().put('year','2028');
       pr.getParameters().put('emailStr','aam1r@mail.com');
       //pr.CreateAndSendPdf(); // This line fails
       }
   }
}
However I get an error when I save the batch class.  Line pr.CreateAndSendPdf() throws:
Method does not exist or incorrect signature: void CreateAndSendPdf() from the type System.PageReference
Sorry, I’ve not included all the code as there’s quite a lot of it, but the essentials are included. 
Any ideas how I can achieve this?
aam1r
aam1raam1r
Ok, so "pr.CreateAndSendPdf()" can't work as methods is not (can't be?) exposed in the vf page in the way class members are.  I therefore tried calling the vf contraoller class instead.

Here is an extract showing the vf page controller class' constructor method:
public with sharing class vfPageController {

    public String CompanyName {get;set;}
    public String OppId {get;set;}
    public Integer year {get;set;}
    public String emailStr {get;set;}
   
    public Boolean DisplayError {get;set;}
    public String ErrorMessage{get;set;}

    public vfPageController(ApexPages.StandardController standardController) { 
        OppId = ApexPages.CurrentPage().getparameters().get('Id');
        CompanyName =  ApexPages.CurrentPage().getparameters().get('OppName');
        year = Date.today().year();
        emailStr = [SELECT Conga_Signer_Email__c FROM Opportunity WHERE Id = :OppId].Conga_Signer_Email__c;
    }
And i've changed the code in the batch class to be:
if (lstValidOpps.size() > 0){
      for (Opportunity o : lstValidOpps){
             ScheduledServiceHostController controller = new ScheduledServiceHostController(new ApexPages.StandardController(o));
             controller.OppId = o.Id;
             controller.year = 2029;
             controller.emailStr = 'aam1r@mail.com';
             controller.isBatch = true;
             controller.CreateAndSendPdf();
      }
 }
Now this saves but when running it fails when the 'vfPageController' constructor is called, on the following, which is returning null:
OppId = ApexPages.CurrentPage().getparameters().get('Id');
The vf page runs fine, but i can't get passed this stage.  How can i bypass the member assignments in the constructor when instantiating the class in this way?
aam1raam1r
It's ok, i figured it out. Simply used a condition in the Controller constructor method to check if ApexPages.CurrentPage() == null.  This allows me to instantiate the vf page controller class from my batch class and then i can manually set the parameters required, before calling the method i need.