2

I have a visualforce page that overrides both the 'New' and 'Edit' Buttons of a custom object. In my controller extension how can I get the name of the action that called this page ? I don't see anything in the StandardController, there are methods to redirect to the standard edit and view pages but nothing that will return the button clicked.

1 Answer 1

3

I don't know if there's any standard way to do so, but you can utilize an approach here in your Extension's constructor to identify if the click was from New or Edit.

When you click on New button, that signifies that you are creating a new record and that it won't have an Id yet, whereas for Edit there will be always one. So checking that in the Extension will help you to identify the flow.

public class MyExtension {

    private MyObj__c abc;
    private String buttonClicked;

    public MyExtension(ApexPages.StandardController stdController) {
        this.abc = (MyObj__c) stdController.getRecord();

        buttonClicked = (abc.Id == null ? 'New' : 'Edit');
        // do something now
    }
}
5
  • +1 Strictly speaking, the parentheses around the ternary expression is not necessary, since there's no ambiguity in the syntax.
    – sfdcfox
    Commented Apr 11, 2019 at 17:19
  • @sfdcfox I agree. I just keep it for a better readability just to not confuse between = and == in the same statement.
    – Jayant Das
    Commented Apr 11, 2019 at 17:19
  • I'll test it out, thanks !
    – MadMax
    Commented Apr 11, 2019 at 18:29
  • I was already getting the record so perfect I'll just check if it's null. What's the abc for?
    – MadMax
    Commented Apr 11, 2019 at 18:32
  • @SallyRothroat Ah, I missed that part. It's just a variable of type of the object which is fetched in the constructor, I have updated my answer. If you are already getting your record, then you just need to verify the Id and done.
    – Jayant Das
    Commented Apr 11, 2019 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .