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
Heather_HansonHeather_Hanson 

SavePDF, pageReference multiple VF pages?

I would like my SavePDF action to save the French and English PDF visualforce pages I have.  I've tried using an if/else statment but couldn't get it to work so I thought I would just try to have BOTH attached no matter what, but I'm having trouble getting that to work too...

Is this possible?  Here is what I've tried based on my research:
 
public pageReference savePDF(){

PageReference pagePdf = 
          new PageReference('/apex/Residential_Order_Form_PDF');
		  new PageReference('/apex/Residential_Order_Form_PDF_French');
          pagePdf.getParameters().put('id', idVal);
          Blob pdfPageBlob;
          pdfPageBlob = pagePdf.getContentAsPDF();
Should I be stating it differently?
 
Best Answer chosen by Heather_Hanson
Ryan GreeneRyan Greene
We could use UserInfo to find their current language setting to pull the correct language PDF
if(UserInfo.getLanguage()=='English'){
	PageReference pagePdf = new PageReference('/apex/Residential_Order_Form_PDF');
         pagePdf.getParameters().put('id', idVal);
         Blob pdfPageBlob;
         pdfPageBlob = pagePdf.getContentAsPDF();
        
    Attachment a = new Attachment();
         a.Body = pdfPageBlob; 
         a.ParentID = idVal;
         a.Name = 'Residential_Order_Form_' + '_' + sToday + '.pdf';
         a.Description = 'Residential Order Form_' + sToday;
         insert a;
}else if(UserInfo.getLanguage()=='French'){
    PageReference pagePdf2 = new PageReference('/apex/Residential_Order_Form_PDF_French');
         pagePdf2.getParameters().put('id', idVal);
         Blob pdfPageBlob2;
         pdfPageBlob2 = pagePdf.getContentAsPDF();

    Attachment a2 = new Attachment();
         a2.Body = pdfPageBlob2; 
         a2.ParentID = idVal;
         a2.Name = 'Residential_Order_Form_' + '_' + sToday + '.pdf';
         a2.Description = 'Residential Order Form_' + sToday;
         insert a2;
}

 

All Answers

Ryan GreeneRyan Greene
Try separating them:
public pageReference savePDF(){

PageReference pagePdf = 
          new PageReference('/apex/Residential_Order_Form_PDF');
          pagePdf.getParameters().put('id', idVal);
          Blob pdfPageBlob;
          pdfPageBlob = pagePdf.getContentAsPDF();

PageReference pagePdf2 = 
          new PageReference('/apex/Residential_Order_Form_PDF_French');
          pagePdf2.getParameters().put('id', idVal);
          Blob pdfPageBlob2;
          pdfPageBlob2 = pagePdf.getContentAsPDF();
Heather_HansonHeather_Hanson
OK so I did that and it didn't work, but I figured, I probably had to account for the pagePDF2 in the attachment part too so for simplicity sake, I just did the following:
 
PageReference pagePdf = 
          new PageReference('/apex/Residential_Order_Form_PDF');
          pagePdf.getParameters().put('id', idVal);
          Blob pdfPageBlob;
          pdfPageBlob = pagePdf.getContentAsPDF();
        
      PageReference pagePdf2 = 
		  new PageReference('/apex/Residential_Order_Form_PDF_French');
          pagePdf2.getParameters().put('id', idVal);
          Blob pdfPageBlob2;
          pdfPageBlob2 = pagePdf.getContentAsPDF();
                 
          Attachment a = new Attachment();
          a.Body = pdfPageBlob; 
          a.ParentID = idVal;
          a.Name = 'Residential_Order_Form_' + '_' + sToday + '.pdf';
          a.Description = 'Residential Order Form_' + sToday;
          insert a;
        
          Attachment a2 = new Attachment();
          a2.Body = pdfPageBlob2; 
          a2.ParentID = idVal;
          a2.Name = 'Residential_Order_Form_' + '_' + sToday + '.pdf';
          a2.Description = 'Residential Order Form_' + sToday;
          insert a2;

Now that resulted in 2 attachments as I hoped, but they were both in English (so they were both "/apex/Residential_Order_Form_PDF").

This is where I get a bit confused because my custom button is programmed to call the French forms if the opportunity language is French, which it is when I'm testing it.  So I press the button, see the French HTML form, press the PRINT button and see the French PDF form, but I press SAVE PDF and it saves the English.  I don't mention language in the Apex Class, I had hoped I could just reference the forms I want, but do I have no choice here? I don't have the language field referenced on my page and didn't want to figure out how to squeeze that in.  Any further suggetions?

 
Ryan GreeneRyan Greene
We could use UserInfo to find their current language setting to pull the correct language PDF
if(UserInfo.getLanguage()=='English'){
	PageReference pagePdf = new PageReference('/apex/Residential_Order_Form_PDF');
         pagePdf.getParameters().put('id', idVal);
         Blob pdfPageBlob;
         pdfPageBlob = pagePdf.getContentAsPDF();
        
    Attachment a = new Attachment();
         a.Body = pdfPageBlob; 
         a.ParentID = idVal;
         a.Name = 'Residential_Order_Form_' + '_' + sToday + '.pdf';
         a.Description = 'Residential Order Form_' + sToday;
         insert a;
}else if(UserInfo.getLanguage()=='French'){
    PageReference pagePdf2 = new PageReference('/apex/Residential_Order_Form_PDF_French');
         pagePdf2.getParameters().put('id', idVal);
         Blob pdfPageBlob2;
         pdfPageBlob2 = pagePdf.getContentAsPDF();

    Attachment a2 = new Attachment();
         a2.Body = pdfPageBlob2; 
         a2.ParentID = idVal;
         a2.Name = 'Residential_Order_Form_' + '_' + sToday + '.pdf';
         a2.Description = 'Residential Order Form_' + sToday;
         insert a2;
}

 
This was selected as the best answer
Heather_HansonHeather_Hanson
Yes, that works, but not for me because our users are all in English...it is the customer who determines the language.  I realized that I only need to reference the language on the HTML version if I were to use the Opp.language field instead of the User language which is less complicated so that is what I did and everything is working now.

Thank you!!