+ Start a Discussion
AndyPandyAndyPandy 

How to "select" all fields from a Custom Object in an APEX Class at once

Hi Guys,

 

I hope I am not committing a forum faux pas by doing this - but this issue has arisen as a result of another post I created recently, which has gone cold: Render as PDF via VF Page

 

I have an APEX Class, which I am using in connection with a VF Page, on which I just want to display fields from a custom object in a nice "printable form".

 

In the above linked post, I got a reply which explained that I could "select" fields in my Class which I wanted to display on the VF Page, as per below example, where the Batch_number__C is selected:

 

public class Adverse_Incident {
public Adverse_Incident__c aIn{get;set;}
public Adverse_Incident ()
   {
       aIn=new Adverse_Incident__c();
       aIn=[select Batch_number__c from Adverse_Incident limit 1];
   }
}

However, there are a LOT of fields in my custom object, and I'm sure there must be a way to "select" all the fields without having to do each one individually - so was hoping someone could help.

 

Then, once they are ALL selected in my APEX class, I could then select them all individually for my VF Page.

 

I hope this makes sense and that someone can help!

 

Many thanks,

 

Andy

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

If you're accessing the record alone and are not pulling information from different sObjects, you can just simily let your apex class go and use your VF page alone. On your VF page, make sure you standardController is your sObject API name. Once this is done you can just access your fields as needed on your VF page.

 

<apex:page renderAs="pdf" StandardController="Custom_sObject__c" >
  <apex:PageBlock>
    <apex:PageBlockSection columns="2">

      <apex:outputText value="{!Custom_sObject__c.Name}"/>
      <apex:outputText value="{!Custom_sObject__c.custom_Field__c}"/>

    </apex:PageBlockSection>
  </apex:PageBlock>
</apex:page>

 

All Answers

Alex.AcostaAlex.Acosta

If you're accessing the record alone and are not pulling information from different sObjects, you can just simily let your apex class go and use your VF page alone. On your VF page, make sure you standardController is your sObject API name. Once this is done you can just access your fields as needed on your VF page.

 

<apex:page renderAs="pdf" StandardController="Custom_sObject__c" >
  <apex:PageBlock>
    <apex:PageBlockSection columns="2">

      <apex:outputText value="{!Custom_sObject__c.Name}"/>
      <apex:outputText value="{!Custom_sObject__c.custom_Field__c}"/>

    </apex:PageBlockSection>
  </apex:PageBlock>
</apex:page>

 

This was selected as the best answer
AndyPandyAndyPandy

Alex,

 

You're a genius!!  One other thing though:

 

One thing that isn't quite coming out right, is my Account Names - the Custom Object has a Master-Detail lookup between both my Accounts and Person Accounts - when I reference both of those in my VF Page e.g.: (does this mean that I actually AM pulling information from different sObjects???):

 

<apex:outputText value="{!Adverse_Incident__c.Account__c}"/>

 The output is just the long ID number of the Account, not the user-friendly Account Name.

 

Would you know how to alter that?

 

Thank you again,

 

Andy

Alex.AcostaAlex.Acosta

2 ways, formula field or you'll then have to use customController/extension. If you're only going to be pulling the name off the account and nothing more, i would suggest just using a formula field to keep it as simple as possible for you. if you're going to be pulling more information, then yes, you'll need to add back your apex class.

AndyPandyAndyPandy

Fantastic,

 

Formula it is (just the names required).

 

For my Accounts, that has worked pefectly - but for my Person Accounts, the two variations of the forumla have either brought through nothing, or the long ID number again.

 

To confirm, Account is a Master-Detail relationship, Person Account is a Lookup relationship - would that matter?

 

Cheers,

 

Andy

Alex.AcostaAlex.Acosta

No this would not matter. Master detail relationship in it's roots is just like a lookup field as well. It just provides a few more functionalities. Can you post what your formula for person account looks like?

AndyPandyAndyPandy

Sorry Alex - ignore me, I was being an idiot - all sorted now :)

 

All that's left for me to do is figure out how to make a plain text PDF look professional and worthy of people clicking on my "generate pdf" button.

 

Thank you so much for your help - I couldn't have got this far without you.

 

Cheers,

 

Andy

Alex.AcostaAlex.Acosta

Anytime. Also for more references on any apex VF tags please take a look at the documentation here.

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_compref_pageBlock.htm|StartTopic=Content%2Fpages_compref_pageBlock.htm|SkinName=webhelp

 

Just remember you can always use standard HTML tags in place of APEX tags