[SalesForce] Fetch specific field value (e.g. Name) from dynamic sObject in Visualforce page

I've read many suggestions regarding retrieving values from sObject in controllers and classes. I want to know if I can retrieve a specific field value from an sObject (which could be an Account in one instance, contact in another, or even a custom object in another, etc). For this example, I want to retrieve the Name field. The sObject in this case is an Account.

The snippit of coding giving me issues is:

value="{!sObj.Name}"/>

in the following line of code:

<apex:param name="rowIndex" assignTo="{!SAN}" value="{!sObj.Name}"/>

The error I receive is as follows: Error: Unknown property 'SObject.Name'

In the debug logs, it clearly shows that the object has the Name attribute, but since it seems to be an sObject I am unable to access it. I have tried to paste as minimal code as possible. If there is any confusion, please let me know.

Component

<!-- Attributes to accept the parameters -->
<apex:attribute name="Records" Type="sObject[]" assignTo="{!sObjLst}" required="true" description="Accepts list of records of any object and assign to a variable in controller class"/>
<apex:attribute name="Fields" Type="String[]" required="true" description="Accepts list of field API names of a sobject in string format"/>
<apex:attribute name="Title" Type="String" required="true" description="Accepts the title of the section"/>
<apex:attribute name="IsActionColumnEnabled" Type="Boolean" required="true" description="Determines whether action column should be displayed in table"/>
<apex:attribute name="Buttons" Type="List" required="false" description="Buttons for column"/>
<apex:attribute name="URLs" Type="List" required="false" description="URLs for buttons"/>

<apex:pageBlockTable value="{!sObjRecords}" var="sObj" styleClass="dataTable" id="pageBlockTable">

    <!-- Dispalys the multiple columns based on the user input -->
    <apex:repeat value="{!Fields}" var="fld">
        <apex:column value="{!sObj[fld]}"/>
    </apex:repeat>

        <!-- Action Column with buttons - May not applicable for all pages -->
        <apex:column rendered="{!IsActionColumnEnabled}" >
            <apex:facet name="header">{!$Label.YAHS_PRT_Action}</apex:facet>
                <apex:repeat value="{!Buttons}" var="button">
                    <apex:commandButton value="{!button}" Action="{!urlRedirection}" rerender="hiddenBlock" styleClass="btn btn-primary nbtn" >
                        <!-- Allows for retrieval of SAN for a given row where a button is pressed -->
                        <apex:param name="rowIndex" assignTo="{!SAN}" value="{!sObj.Name}"/>
                        <!-- Hidden pageBlock allows for the param above to be passed within a commandButton -->
                        <apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>
                    </apex:commandButton>
                </apex:repeat>
        </apex:column>
    </apex:pageBlockTable>

Component Controller

//Stores the records which are supplied to the 'Records' attribute.
public Sobject[] sObjLst {get;set;}

/*
1. Implementing the pagination with ApexPages.StandardSetController.
2. We can utilize the built in methods available for the ApexPages.StandardSetController to build the pagination.
3. Following are the built in mehods we can utilize -
a. first()
b. previous()
c. next()
d. last()
e. getHasPrevious() - returns boolean value.
f. getHasNext() - returns boolean value.
g. setPageSize(IntegerValue)
*/

public ApexPages.StandardSetController con {
    get {
    //initializing con with the records.
    if(con == null)
        con = new ApexPages.StandardSetController(sObjLst);

    //Setting the pagination size
    con.setPageSize(paginationSelection);

    return con;
}
set;
}

//Method which returns subset of records from the sObjLst.
public List<sobject> getSobjRecords() {        
    //Type Casing the records and returning to display on the page.
    return (List<sobject>)con.getRecords();

}

VF Page

<c:GenericPaginationComponent Title="" records="{!locationAccLst}" fields="{!locationAccFieldLst}" IsActionColumnEnabled="true" Buttons="{!buttons}" URLs="{!endPointURL}"/>

VF Page Controller

//Querying the records from the database.
locationAccLst = [SELECT Name, ShippingAddress FROM Account limit 100];

//Preparing the list of fields to display in the table.
locationAccFieldLst = new List<String>{'Name', 'ShippingAddress'};

buttons = new List<String>{'Test Button1', 'Test Button2'};

Best Answer

Related Topic