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
samuisamui 

VisualForce / apex:detail / StandardController extension

Hi everyone,

 

I'm having trouble working with the apex:detail tag.

I must be missing something because even the simplest use case seems to elude me.

 

I have made a VisualForce page which uses a StandardController with a custom Extension.

 

When I insert an apex:detail tag in my visual force page (within a pageBlockSection in case that is of importance), I get it to display my custom object's fields and that's nice.

However I have some fields which were made read-only and they display with a nice padlock in the standard layout, but on my visual force page they simply appear as editable (inlineEdit="true").

 

How can I make them read-only in my visual force page too?

 

For reference here's how I include the apex:detail tag in my page:

<apex:pageBlockSection title="Offer Settings" columns="1">
 <apex:detail relatedList="false" title="false" inlineEdit="true"/>
</apex:pageBlockSection>

 

I have a 2nd issue.

I wanted that VisualForce page to be the default page for my custom object so I have overridden the standard buttons (ViewEdit andNew). And when I click on New, I get to the page mentionned above but the apex:detail part of the page is simply missing and I can't edit my object's field on creation.

Why is that?

 

And last but not least,

When I click on the Edit button I get redirected to my page but the fields from the apex:detail part of the page remains displayed asoutputFields instead of inputFields. I was expecting the fields to appear like when using mode="edit".

Am I missing something?

Is that related to my overridding of the Edit button?

 

 

codeshodecodeshode

Hey Samui,

 

Couple of things here, if your intention is to mirror the standard page then i would advice to get rid of any pageblock or pagesection. The apex:detail should be directly child of apex:page.

 

second, i dont see the subject attribute in your code. it must take the id of the standard controller.

 

Take a look at small piece of code i have wriitten.

 

<apex:page standardController="Account" extensions="ContactDetailController" >
  <apex:detail subject="{!accountId}" relatedList="true" title="true" inlineEdit="true" showChatter="true"/> 
</apex:page>

 Controller :

 

    public String accountId {get; set;} //account id
    public Account acc {get; set;} // account record
    
    /**
    @author Manish
    @purpose class constructor, fetch account details
    */
    public ContactDetailController(ApexPages.StandardController controller) {
        accountId = controller.getId();
        if(accountId != null){
            try{
                acc = [SELECT Id, isPersonAccount, Name FROM Account WHERE id=:accountId limit 1];
            }catch(Exception e){
                ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.error,'Unable to find the Account');
                ApexPages.addMessage(msg);
            }
        }
    }

 So whats the the next step, just do this one thing.

 

Go to button section on the object, override the 'Detail' to point to this page.

 

And now whenever you see this page, it will have inline edit, standard editing and all similar things.

 

TO direct to this page from apex:

 

Account acc = new Account();
... 
... 
...

try{
    Database.insert acc;
}catch(Exception e){
    //TODO: do something
}

//To go the page

PageReference accdetailPage = acc.view();

 /**
or you can use 
PageReference accdetailPage = new PageReference('/'+acc.id);
*/

 I hope this helps, in case i misunderstood your question, i would love to hear back from you, otherwise. If I was of somehelp, do mark this as solved.

 

Thanks

 

samuisamui

Hi codeshode, thanks for answering.

 

My idea is not to mimic exactly the standard page, I just would like to retrieve some of it's behavior.

 

I was thinking the subject attribute was optional and that it would automatically retrieve the id of the current record from the standard controller (since I'm using a standard controller here). Anyway, I did add the subject attribute but that did not change anything.

 

Regarding the code you have provided, I actually get the same problem as with my own code.
If I override the three buttons View, Edit and New to link to the page you have provided, the result will not be the one I was expecting, but then I think my expectations were wrong.
- The View button behavior is fine.
- The Edit button behavior is wrong because it will be exactly the same as the View behavior instead of displaying the fields as editable.
- The New button behavior is wrong because it will not display anything (I guess the subject="{!accountId}" is faulty here since the new account will not be attributed an Id until we click Save and I guess the apex:detail component expects a valid Id).

 

So I guess I will be obliged to create three views, one for each action?

Apparently I cannot use the apex:detail tag in an Edit page (https://success.salesforce.com/ideaview?id=08730000000BrQtAAK) or a New page (http://salesforce.stackexchange.com/questions/4798/how-does-apexdetail-work-can-we-show-layout-for-a-new-record).

 

Thanks

codeshodecodeshode
Alright so you wanted to override all the buttons with one single page. Eh, I guess i missed onto that.
<apex:detail> only gives us the facility to display the standard detail page.
The idea that you have shared is the best thing salesforce must do. So as you said, three pages is your only option.

Anyways thanks for the reply.