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
tukmoltukmol 

how determine the current mode: view or edit?

Hi all,

 

I'm creating a page to handle (override the default) adding/editing/viewing of custom object records.

 

I know I can create 2 or 3 pages to handle all the modes mentioned, but it would be handy to maintain just one page, since they would have the same layout and fields set. So it would be like have a page "Page1" to override "New", "Edit" and "View" of a custom object.

 

Now, how do would I determine if a record opened is in New/Edit or View mode?

 

Is there something like ApexPage.currentPage().isEditMode() that I could use?

 

Spent hours (a day actually) searching for this but didn't find an answer.

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

There is not, sadly. It would be nice to have just one code section that does it all, but it's not that simple. The best you can probably get away with is one page for all the edit modes (edit, new, and clone), and one for the non-edit modes (such as view and tab view).

All Answers

sfdcfoxsfdcfox

There is not, sadly. It would be nice to have just one code section that does it all, but it's not that simple. The best you can probably get away with is one page for all the edit modes (edit, new, and clone), and one for the non-edit modes (such as view and tab view).

This was selected as the best answer
tukmoltukmol

thanks sfdcfox for the answer.

 

at least I am sure now that there's no OOB property/function to use.

 

I am on my quest now for the possible hack ( i mean "work around").

 

Since your answer hit my query clearly, I accepted your answer as solution.

tukmoltukmol

By the way, while waiting for an answer... I've tried several approaches to get to the goal of "one page to rule 'em all" and the nearest (not one but two pages) I've tried was to using an apex component (<apex:component>) for the common layout.

 

the common layout component is then called/inserted into "viewPage" and "editPage"

 

within the controller extension, the "isEditMode" property is detemined via "pageName" (hardcoded)

i.e. If pagename is "viewPage", editMode returns false which is being used in rendered attribute of input/output fields within the common layout component. sObject is passed to the component via its <apex:attribute>.

 

this works fine, and I think this is OK for now.

 

but it would be really nice to have this function or property

tukmoltukmol

update...

 

Another workaround I've tried was to have my own edit ("editRecord()") method within the controller extension. This edit method would then call the built-in edit() and append a parameter/value "edit=1" to the URL, like this (not the complete code):

 

public class MyControllerExt {
    private ApexPages.StandardController   p_controller = null;    
    
    public MyControllerExt (ApexPages.StandardController stdController) {
        this.p_controller = stdController;      
    }

    public boolean editMode {
        get {
            boolean ret = false;
            if (this.isNew) { //isNew is a property to determine if a record is new or not
                ret = true;
            } else {
                PageReference page = ApexPages.currentPage();
                Map<String,String> params = page.getParameters();
                if (params.get('edit') == '1') { //if edit parameter is '1'
                    ret = true;
                }
            }
            return ret;
        }
    }

    public PageReference editRecord() { 
    	//p_controller is the StandardController passed to the constructor       
        PageReference ref = this.p_controller.edit();
        String url = ref.getUrl();        
        ref = new PageReference(url + '&edit=1'); //add the editmode marker
        return ref;
    }
}

 

 

This worked great...

 

Until the "Mini Page" popped up in "Recent items" showing  the "Edit" button, which cannot be overridden!

 

Whew! a deal breaker! bummer.

 

Oo I'm back to "common component" approach as mentioned above.

 

By the way, even though I'm an old programmer, I'm just almost a week old with salesforce, so I'm basically unaware/clueless to most things regarding this platform :D

 

So if you could, please excuse me if you see me around posting everything :D

 

Cheers!

Christopher PryorChristopher Pryor

I am not sure about Saleforce1 or the "Mini Page" mentioned above.  But if you are just overriding the default buttons on a custom or standard object, you can use the URL to determine what mode you are in.

private Map<String, String> PageParameters = new Map<String, String>();
public String DetectedMode {get; set;}

PageParameters = ApexPages.currentPage().getParameters();
if (PageParameters.get('id') == null)
{ DetectedMode = 'New';  } else if (PageParameters.get('id') != null & PageParameters.get('retURL') != null)
{ DetectedMode = 'Edit'; } else if (PageParameters.get('id') != null & PageParameters.get('retURL') == null)
{ DetectedMode = 'View'; }