I have a custom button on a custom object page from which I generate a VisualForce PDF document (Content Source = URL). There are conditions, however, where the PDF should not be generated based on the value of a field in the object.

I would like to build an IF condition on the button to open the PDF page if the value of the field is correct, but if not, to stay on the current page and display a message.

Salesforce discourages use of Javascript for buttons so I'm trying to find a different way, so if anyone can help I'd appreciate it. I've found articles but none showing how to do the 'else' condition I need.

{!if(ISPICKVAL (myObj__c.FieldA__c,"TypeA"),
     ....stay on page and display message.....}

I'd like to dispay a message on the current page to the effect of 'Sorry, you cannot generate a PDF for this item'.

The 'true' part of the IF works fine.

1 Answer 1

<apex:page standardController="MyObj__c" action="{!IF(ISPICKVAL (MyObj__c.FieldA__c,'TypeA'),
    <apex:pageMessage severity="warning" summary="Sorry, you can not ..."/>

You can use the page action attribute and return NULL to stay on the page. The custom button references the VF page so you don't have to use any JS

  • The page my button is on is not a VF page. Are you meaning I should add this action to the PDF page that I'm opening from the button?
    – Irene
    Sep 10, 2019 at 18:49
  • @Irene - yes; the button on the object page should be associated to a VF page that in effect, is a redirecting page - it redirects to either a PDF page or to a simple page with an error message
    – cropredy
    Sep 10, 2019 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.