1
votes

Newbie to salesforce so please excuse any errors or wrong syntax. Essentially we have a dirty workaround manual menu that allow users to download a PDF letter from command buttons.

At the moment the Apex controller has multiple PageReference classes that manually (hardcoded) references a VisualForce Page for every letter that needs converted to PDF. for example

public PageReference saveLetter1(){
//visualforce page to create PDF from
pageReference pdf = Page.ltr_one_generalexample;

I want to create a reusable class by simply passing the VF Page name as a string value in the command button and use it in the apex class, but cannot seem to grab the value (always null).

<apex:commandButton action="{!saveLetter1}"  styleClass="btn-info btn-lg" 
value="Download Letter">
<apex:param name="VPName" value="!ltr_ws_addbin_new_refusal" assignTo=" 
{!VPName}"/>
 </apex:commandButton>

And in Apex class

public String VPName{get;set;}
//and use as 
pageReference pdf = Page.VPName;

Can this be done and can a page reference use a string variable as indicated. If there is an easier way??

3

3 Answers

0
votes

Your Visualforce looks accurate along with the property but for the page Reference it should be

PageReference pdf = new PageReference('/'+VPName);//or '/apex/'+VPName if apex page
0
votes

By default commandButtons don't pass the parameters. It's stupid but it's one of quirks.

See my answer from 2012 for more info and 2 fixes: https://salesforce.stackexchange.com/questions/4937/why-does-apexparam-assignto-work-with-apexcommandlink-but-not-apexcommandbutt/4950#4950

0
votes

Cheers guys, managed to get this working, I found out that:

  1. I could pass the values from a command button if I added the variable values as hidden fields instead of params.

    Or using the command link with params as mentioned by Eyescream

But it did require the '/apex/' + varname from Garath to pick it up so between all your answers I was able to complete my first little Apex intro.