[SalesForce] Conditional custom button on page direct to URL if condition = true, display message if false

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"),
     URLFOR("/apex/myPDF?myId="&myObj__c.Id),
     ....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.

Best Answer

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

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

Related Topic