4

How to get object name from standard controller vf without using controller or extension. like for id, we can get by using {!$CurrentPage.parameter.Id}.

2 Answers 2

5

A Visualforce page using a standard controller (i.e. not having a custom controller or a controller extension defined) exposes an instance of the object type defined in the standardController attribute using a property of the same name. So to display the name:

<apex:page standardController="Contact">
{!Contact.Name}
</apex:page>

The standard controller works out which fields to query based on its parsing of the {! } expressions in the page.

PS

Not too clear what you are aiming for. This compiles:

<apex:page standardController="Contact">
    <c:Generic sob="{!Contact}"/>
</apex:page>

<apex:component>
    <apex:attribute type="SObject" name="sob" description=""/>
    Hello {!sob['Name']}
</apex:component>

but fails at runtime because the standard controller can't detect that the name field should be queried.

4
  • Here {!Contact.Name} is in VF component and called for many standardController for different different object. I want to make it generic i.e. {!<Generic>.Name}
    – Praveen
    May 21, 2016 at 20:25
  • @Praveen Suggest you add more to your question to explain the use case. Remember that to present database data it has to be queried too.
    – Keith C
    May 21, 2016 at 20:35
  • I have situation, where i have to just create different pages for many objects. The page includes : <apex:page standardController="<Account/Contact/Opportunity>"> <c: <ComponentName>/> </apex:page> Here i have make component generic, which will be suitable for all objects. In component i have call generic field like Name,CreatedDate,LastmodifiedDate etc. Hope this make lil more clear. many Thanks for you inputs.
    – Praveen
    May 21, 2016 at 20:49
  • 1
    @Praveen The map-like syntax {!sob['Name']} would work in the component but you would have to write your own controller that queried every field; that's not too hard to do given the describe methods.
    – Keith C
    May 21, 2016 at 21:09
1

I think what you're looking for is $ObjectType. You can use it to create a Dyanamic sObject Handler that you can use with components without needing to call up a standardcontroller from a custom controller (Visualforce components also can’t use controller extensions).

You're essentially going to be doing dynamic apex in the controller and dynamic visualforce bindings in the page. You'll find a very good example in the documentation of a DynamicObjectHandler and visualforce markup to go along with it that will show you how to implement it in a component.

Here' a bit of the handler code:

public class DynamicObjectHandler {

    // This class acts as a controller for the DynamicObjectViewer component

    private String objType;
    private List<String> accessibleFields;

    public sObject obj { 
        get; 
        set {
              setObjectType(value);
              discoverAccessibleFields(value);
              obj = reloadObjectWithAllFieldData();
        } 
    }

    // The sObject type as a string
    public String getObjectType() {
          return(this.objType);
    }
    public String setObjectType(sObject newObj) {
        this.objType = newObj.getSObjectType().getDescribe().getName();
        return(this.objType);
    }

Here's bit of the component code:

<apex:component controller="DynamicObjectHandler">
    <apex:attribute name="rec" type="sObject" required="true"
        description="The object to be displayed." assignTo="{!obj}"/>

    <apex:form >
    <apex:pageBlock title="{!objectType}">
        <apex:pageBlockSection title="Fields" columns="1">
            <apex:dataTable value="{!accessibleFields}" var="f">
                <apex:column >
                    <apex:facet name="header">Label</apex:facet>
                    <apex:outputText value="{!$ObjectType[objectType].fields[f].Label}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">API Name</apex:facet>
                    <apex:outputText value="{!$ObjectType[objectType].fields[f].Name}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Type</apex:facet>
                    <apex:outputText value="{!$ObjectType[objectType].fields[f].Type}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Value</apex:facet>
                    <apex:outputText value="{!obj[f]}"/>
                </apex:column>
            </apex:dataTable>
        </apex:pageBlockSection>

I think the above snippets of code illustrate fairly well how what you're asking can be done dynamically without the need for a standard controller.

0

You must log in to answer this question.

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