+ Start a Discussion

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


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">
          <apex:slds />

public class VFC009_CreateContract {

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


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.
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'));