I have a APEX Class that runs from a button on a standard record page. It creates a related record in another table. The code works fine as it is now.

But I want to add a feature that does a check (Selection Field == Value) and if the check fails the user is given a custom message and the code stops running, and no record gets created. I can't figure this out.

I have run across such functions as 'adderror' and 'addmessages', but not sure how to get these working. This starts from a button press on the starting page, the record is created and then the user is taken to the records page. If there is no record to be created then there is no page to go to. Can I put the error message back onto the starting page? But would it not need to be refreshed to show the error, and then won't the error status be lost? Or do I need to create a new VF page that is just a spot holder for the error? If error, then VF page and show error, if not then normal process? I was hoping I could do a simple 'alert', but that doesn't work.

Any pointers would help here, not sure what I need to do to get this working.


1 Answer 1


I think you answer your own question. When we make buttons record detail pages we never have the button do any of the work via javascript. The button simply redirects to a blank VF page. This is pattern is especially important because Lightning does not support javascript buttons for security reasons. This approach is supported in Lightning.


The Page contains an "init" method that is added to the pages "action" attribute:

<apex:page standardController="Opportunity" extensions="oppExtension" action="{!init}">
    <!-- optional go back link -->

If there is a failure, you can catch it and use the AddMessages method to display something to your user, otherwise, redirect the user to the new record page.

public class oppExtension {
    public PageReference init() {
        PageReference result;

        // get url params
        Id oppId = ApexPages.getParameters().get('oppId');
        String retURL = ApexPages.getParamters().get('retURL'); // Option param for go-back link functionality

        // do tha thing
        if (thing.success) {
            result = new PageReference('/' + thing.Id);
        } else {
            ApexPages.addMessage(new ApexPages.Message(
                'Error: ' + thin.ErrorMessage,

        return result; // Returns null if there is an error
  • can we use .addError outside of trigger context? May 30, 2018 at 21:24
  • Ya know, I haven't actually put it to a test. I don't see any good reason SFDC should limit it to triggers since it's primary purpose is to prevent DML, Ill do a quite test though.
    – gNerb
    May 30, 2018 at 21:26
  • I removed it, I tested in anon and while I didn't get any errors, it didn't actually stop my DML either.
    – gNerb
    May 30, 2018 at 21:28
  • 1
    as far as I know, .addError works for trigger records only, but first sample should work just fine May 30, 2018 at 21:29
  • Thanks gNerb. this helped me get it working. I already had that VF page process setup, but was just stuck on sending messages. This got me pointed in the correct direction to get it working, thanks.
    – PHonnold
    Jun 4, 2018 at 21:01

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.