+ Start a Discussion
SandrineSandrine 

When user clicks on button, display a message and block the execution of apex code

Hello,

I have a button like below on Opportunity object
User-added image
 
<apex:page standardController="Opportunity" extensions="VFC009_CreateContract" lightningStyleSheets="true" docType="html-5.0" >
    <html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <head>
          <apex:slds />
        </head>
        <body>

        </body> 
    </html>
</apex:page>
 
public class VFC009_CreateContract {

    public VFC009_CreateContract(ApexPages.StandardController stdController){
        currentOpp = (Opportunity)stdController.getRecord();
        initPage();
    }

}

If the Created date of opportunity in less than jan 2015, i want to display a message to user to say that "opp is too old cant convert" else the apex code is executed normally.

How can i implement the code ?

Thank you for suggestion !
Best Answer chosen by Sandrine
Tad Aalgaard 3Tad Aalgaard 3
Add this to your visualforce page to display the error messages.
<apex:pagemessages>
</apex:pagemessages>
Add this to your Apex code to create a conditional error message.
Date myDate = date.newInstance(2015,01,01);
if(currentOpp.createdDate < myDate){
    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'opp is too old cant convert'));
}