function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ethanoneethanone 

Halt Controller Operation Gracefully

I'm writing a page button controller that basically does a customized clone operation.  I want the controller to check to see if the Opportunity stage is closed.  If it is not, i need the controller to display an error on the screen and send the user back to the view page. What is the best way to go about this?

Thanks in advance.
Craig WarheitCraig Warheit
Check to see if the Opportunity is closed, and if it's not, create an error message and return null.

////controller

if(!Opportunity.isClosed){
     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Opportunity must be closed!'));
     return null;
}


///page

<apex:pageMessages ></apex:pageMessages>


ethanoneethanone
Thanks, I've found that this snippet cannot go in the contructor (can't return anything from the construtor). Should this have its own method? Where is the best place to put this?