1

Below is my VF code, I need to render my PDF only if status__C = approved. How to achieve it. I'm new to development

<apex:page controller="MyController" renderAs="pdf" applyBodyTag="false">
<head>
<style>
body { font-family: 'Arial Unicode MS'; }

@page{
    size: letter;
    margin:10%;
    @top-left{
        content: "Dear,";
        font-family: Helvetica, Arial, sans-serif;
        font-size: 12px;
    }
    @bottom-right{
        content: "Yours Sincerely,";
        font-family: Helvetica, Arial, sans-serif;
        font-size: 10px;
    }
}

body {
        font-family: Helvetica, Arial, sans-serif;
        font-size: 11px;
}
    </style>
</head>
    <div align="right"><strong>Date</strong>: {!DAY(Today())} {!CASE(MONTH(Today()), 1, 'January', 2, 'February', 3, 'March', 4, 'April', 5, 'May', 6, 'June', 7, 'July', 8, 'August', 9, 'September', 10, 'October', 11, 'November', 12, 'December', 'Unknown')} {!YEAR(Today())}</div>
<center>
    <h1> Letter</h1>
    </center>
    <p>{!custom_object__C.Name__C}</p>    
</apex:page>

Below is Custom Controller

public class MyController {

    public   TTD_Letter__C ttdletter{get;set;}

        public MyController(){
            ttdletter = [SELECT Id,Name__c,Status__C FROM Letter__c 
                         WHERE Status__c = 'Approved' AND Id =  :ApexPages.currentPage().getParameters().get('letterId') 
                         LIMIT 1];
        }

    }

2 Answers 2

1

If you wanted to do the check on the Page load then You can do this using the Property. Create a Property in Apex Class.Like

public String isPDF { get; set; }

Then inside the constructor assign the value based on the Status

IF(ttdletter != null && ttdletter.Status__c == 'Approved'){ 
     isPDF  = 'pdf';
 }else{
    isPDF  = 'html';
 }

The complete code for the apex class will look like below

public class MyController {
    public   TTD_Letter__C ttdletter{get;set;}
    public String isPDF { get; set; }
    public MyController(){
        ttdletter = [SELECT Id,Name__c,Status__C FROM Letter__c 
                     WHERE Id =  :ApexPages.currentPage().getParameters().get('letterId') 
                     LIMIT 1];
        IF(ttdletter != null && ttdletter.Status__c == 'Approved'){ 
            isPDF  = 'pdf';
        }else{
            isPDF  = 'html';
        }
    }
}

In the class it will check if the status is Approved then it will return pdf in isPDF variable otherwise will return html.

And now use this property in VF page RenderAs attribute

<apex:page controller="MyController" renderAs="{!isPDF}" applyBodyTag="false">

Below will be the code for VF page

<apex:page controller="MyController" renderAs="{!isPDF}" applyBodyTag="false">
<head>
<style>
body { font-family: 'Arial Unicode MS'; }

@page{
    size: letter;
    margin:10%;
    @top-left{
        content: "Dear,";
        font-family: Helvetica, Arial, sans-serif;
        font-size: 12px;
    }
    @bottom-right{
        content: "Yours Sincerely,";
        font-family: Helvetica, Arial, sans-serif;
        font-size: 10px;
    }
}

body {
        font-family: Helvetica, Arial, sans-serif;
        font-size: 11px;
}
    </style>
</head>
    <div align="right"><strong>Date</strong>: {!DAY(Today())} {!CASE(MONTH(Today()), 1, 'January', 2, 'February', 3, 'March', 4, 'April', 5, 'May', 6, 'June', 7, 'July', 8, 'August', 9, 'September', 10, 'October', 11, 'November', 12, 'December', 'Unknown')} {!YEAR(Today())}</div>
<center>
    <h1> Letter</h1>
    </center>
    <p>{!custom_object__C.Name__C}</p>    
</apex:page>
1
  • Thank you. It helped me, but in the preview of VF page i'm unable to see the preview due to this " List has no rows for assignment to SObject ". In my APex code in SOQL query if I remove Id = :ApexPages.currentPage().getParameters().get('Id'), the preview is available. But to get the pdf according to the record, i should use that parameter
    – sf123
    Jul 15, 2018 at 5:02
4

If you are calling this visualforce from button click event through controller method then, you need to put the condition at that place.

public PageReference openPDFPage()
{
    TTD_Letter__C  ttdletter = [SELECT Id,Name__c,Status__C FROM Letter__c 
                         WHERE Status__c = 'Approved' AND Id =  :ApexPages.currentPage().getParameters().get('letterId') 
                         LIMIT 1];
    if(ttdletter.Status__C == 'approved')
    {
        PageReference pg = ApexPages.MyVisualforcePDFPage;
        pg.setRedirect(true);
        return pg;  
    }
    return null;

}
2
  • Thank you. It helped me, but in the preview of VF page i'm unable to see the preview due to this " List has no rows for assignment to SObject ". In my APex code in SOQL query if I remove Id = :ApexPages.currentPage().getParameters().get('Id'), the preview is available. But to get the pdf according to the record, i should use that parameter
    – sf123
    Jul 15, 2018 at 5:14
  • I have given an approach to handle your requirement. You might change the code according to your need Jul 15, 2018 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .