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
Jenn SanchezJenn Sanchez 

Return custom object record ID to visualforce page

I am very new to development in general and to Salesforce development. I have a custom object Announcements__c. I want to display the most recent announcement record on a visualforce page. I have gone through the visualforce basics module (https://trailhead.salesforce.com/content/learn/modules/visualforce_fundamentals) but still cannot figure out how to pass the record ID of the most recent announcement record (custom object). Here is my apex class - which I modeled after the trailblazer example which used the Contact standard object, so not really sure this translates well to my custom object:

User-added image
And here is the visualforce markup:

User-added image

I get no errors, but also don't get my custom object field to display. Any help would be much appreciated!

Sharon JackSharon Jack
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have Routerlogin.net (https://www.netdervice.com/2019/11/routerloginnet-not-working-router-is.html)
Puneet_MishraPuneet_Mishra
Hi Jenn,

The first thing you will notice with your code is that you are using standard controller instead of your custom controller that is why you are not seeing any error.
 
// using standard controller, instead of "AnnoucementListException"
<apex:page sidebar="false" showHeader="false" standardController="Annoucements__c">

If you are into writing something custom of your own, then change standardController to Controller and mention you custom class name.
One of the important point to note that inputField/outputField can only be used with sObject, i.e. if you are passing a list<record> and accessing those in apex:outputfield you will receive an error even if your list has only one record.

Considering that change your code like below options:
// class name is extension, are you trying to use extension with standard controller ?
public class AnnoucementListExtension { 

    public Annoucements__c getAnnoucements() {
        return Database.Query('SELECT Id, Annoucement_Details__c ' + ' FROM Annoucements__c ' + ' ORDER BY LastModifiedDate ' + ' DESC ' + ' LIMIT 1 ' );
    }

}

<apex:page Controller="AnnoucementListExtension" showHeader="false", showbar="false"> 
    <apex:pageBlock title="New Annoucements"> 
        <apex:pageBlockSection columns="1"> 
           <apex:outputField value="{!annoucements.Annoucement_details__c}"/>
        <apex:pageBlockSection
    </apex:pageBlock> 
</apex:page>

If you want to play with list only then use below code:
check pageBlockTable: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_pageBlockTable.htm 
public class AnnoucementListExtension { 

    public List<Annoucements__c> getAnnoucements() {
        return Database.Query('SELECT Id, Annoucement_Details__c ' + ' FROM Annoucements__c ' + ' ORDER BY LastModifiedDate ' + ' DESC ' + ' LIMIT 1 ' );
    }
}


<apex:page Controller="AnnoucementListExtension" showHeader="false", showbar="false">
    <apex:pageBlock title="New Annoucements">
        <apex:pageBlockSection columns="1">
              <apex:pageBlockTable value="{!annoucements}" var="annouce">
                  <apex:column value="{!annouce.Annoucement_details__c}"/>
              </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

You can remove pageBlockSection.

Hope it helped.

Regards,
Puneet.