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
lapaullapaul 

The PageReference method does not do anything when called from another method

 

Hi,

I have 2 Apex methods print_identity and print_action. print_identity calls print_action and print_action is supposed to return the Page.IdentityTheftCert but it does not seem to do anything. Any ideas? see the 2 methods below. Please advice. Thanks

 

 public void print_identity(){
      TestTitle = 'Identity Theft Prevention Program Test';
      print_action();
   }

 

 

public PageReference print_action(){
    User u = [Select Department, Division, Name, Username From User Where id = :UserInfo.getUserId()];
    String dept = u.Department;
    String div = u.Division;
    String uname = u.Name;
    String uloginname = u.Username;
   
CrescentOnlineTests__c Where UserName__c  =: uloginname And Test_Name__c =: 'Identity Theft Prevention Program Test'];
    List<CrescentOnlineTests__c> Results = [Select UserName__c, Test_Name__c, DatePassed__c From CrescentOnlineTests__c Where UserName__c  =: uloginname And Test_Name__c =: TestTitle];
    
    
    if (Results.size() > 0){
        //OnlineTest = Results[0];
       //if (OnlineTest.DatePassed__c != null || OnlineTest.DatePassed__c != ''){
       if (Results[0].DatePassed__c != null || Results[0].DatePassed__c != ''){ 
           printyn = false;
           certDate = Results[0].DatePassed__c;
           
           PageReference pg = Page.IdentityTheftCert;
           pg.setRedirect(true); //it will stay in the current state without this line.
           return pg;
       }
       else {
           printyn = true;
           return null;
           }
       }  
     else {
           printyn = true;
           return null;
 
       }
       return null;
   }  

 

 

gv007gv007

PageReference pg = new PageReference('IdentityTheftCertL');

 

try this

Imran MohammedImran Mohammed

I think your print_action method should something like this

public void print_identity(){
      TestTitle = 'Identity Theft Prevention Program Test';
      PageReference p = print_action();

      return p;
}

 

And your print_action method should have a return type of PagerReference. Making that change your print_action should be

public PageReference print_identity(){
      TestTitle = 'Identity Theft Prevention Program Test';
      PageReference p = print_action();

      return p;
}

 

Forgot to mention, In you print_action method

 PageReference pg = new PageReference('IdentityTheftCert');
 

The PageReference will work only if it returns back to VF page.

It will not have if the PageReference returns back to the invoking method as you have, because the control is still in Apex class.

 

 

lapaullapaul

Thanks. It works now.

 

FYI - return print_action();  will also works too.