6

I have a custom Visualforce page for a custom object. I have overriden the new, edit, and view buttons to point to this page. I would like to dynamically change the look of the page based on what mode the user is in.

For example, show an <apex:inputField /> when they are editing, but an <apex:outputField /> when viewing:

<apex:outputField value="{!MyCustomObject__c.Field1}" rendered="{!pageMode == 'view'}" />
<apex:inputField value="{!MyCustomObject__c.Field1}" rendered="{!pageMode == 'edit'}" />

I would also use this property to set the mode attribute of my <apex:pageBlock />

Is there a good way to do this using a button override? I do not want to create custom buttons and use URL parameters if I can avoid it.

2
  • This is a very good question. My initial response was to set the page attribute "readOnly" to true, but you would need to do that in your controller extension and I don't know how to evaluate what context "view, edit, etc" the page is in. I'd guess that if you do an override you can see if the url ends with /e for edit, etc and do that. But just a guess. Commented Jul 29, 2014 at 18:56
  • You could also create two separate pages, one for view and one for edit. It would be easier to maintain with two separate pages as well
    – Eric
    Commented Jul 29, 2014 at 19:12

1 Answer 1

3

This will lead to a lot of conditionally rendered tags. You might consider having separate page for "view" and separate for "new/edit" but use same controller extension if you really want.

But if you're determined... You can look at the URL that was used to navigate to your page and combine it with presence of Id in the standard controller.

I've called my page override and used Opportunity as the test object.

New: /apex/override?retURL=%2F006%2Fo&save_new=1&sfdc.override=1
View: /apex/override?id=0067000000AH3ME&sfdc.override=1
Edit: /apex/override?id=0067000000AH3ME&retURL=%2F006%3Ffcf%3D00B70000005pFQv%26rolodexIndex%3D-1%26page%3D1&sfdc.override=1

Something like this should work, at least until SF decides to rename/remove the retUrl param (unlikely I'd say... Especially than on viewing there's rarely something to cancel or "on success return to... where?").

public MyExtension(ApexPages.StandardController ctrl){
    Id i = ctrl.getId();
    Map<String,String> parameters = ApexPages.currentPage().parameters();
    if(i == null){
        System.debug('new');
    } else if(parameters.contains('retUrl')){
        System.debug('edit');
    } else {
        System.debug('view');
    }
}
1
  • Thank you. This does work. However, if I'm going to have to check url parameters, I may as well make custom buttons so I can control the values and not have to worry about Salesforce changing a behavior.
    – fehays
    Commented Jul 29, 2014 at 21:15

You must log in to answer this question.

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