+ Start a Discussion
Supriyo Ghosh 5Supriyo Ghosh 5 

Simple apex code and vf page

Hi,

I want to create a report through vf page.My object name is EmpMaster.I want to display name,address and Emp code.Can anyone help me with the apex code and vf page.

Name      Address           EmpCode
Suman    Kolkata              20012
Saha       West Bengal      50026

I want to display like this.Please help.
Best Answer chosen by Supriyo Ghosh 5
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below

Standrad controller

public class EmpMasterReport
{
   public List<Employee_Master__c> EmpList {get;set;}
   
    public EmpMasterReport(ApexPages.StandardController stdController) {
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c FROM Employee_Master__c ];
    }
    
    public EmpMasterReport()
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c FROM Employee_Master__c ];
    }
   
}

VF page

<apex:page id="p1" standardController="Employee_Master__c" extensions="EmpMasterReport" sidebar="false">
    <apex:form>
        <apex:pageBlock title="Employee Master Details" id="Emp_Master">
            <apex:pageBlockTable value="{! EmpList }" var="em">
                <apex:column value="{! em.Name}"/>
                <apex:column value="{!em.Address__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page> 

Let us know if this will help you.

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
This you can learn in fun way by trail head. Please that will help you
1) https://developer.salesforce.com/trailhead/en/visualforce_fundamentals/visualforce_standard_controllers
2) https://developer.salesforce.com/trailhead/en/module/visualforce_fundamentals

Sample code for you for contact object. You can add your custom object name
 
<apex:page standardController="Contact" recordSetVar="contacts">
    <apex:form>

        <apex:pageBlock title="Contacts List" id="contacts_list">
            <apex:pageBlockTable value="{! contacts }" var="ct">
                <apex:column value="{! ct.FirstName }"/>
                <apex:column value="{! ct.LastName }"/>
                <apex:column value="{! ct.Email }"/>
                <apex:column value="{! ct.Account.Name }"/>
            </apex:pageBlockTable>
          
        </apex:pageBlock>

    </apex:form>
</apex:page>
Let us know if this will help you

 
Supriyo Ghosh 5Supriyo Ghosh 5
Hi,

I have written the standrad controller and vf page.but unable to fetch the data.Please help.

Standrad controller
public class EmpMasterReport
 {
   public List<Employee_Master__c> EmpList{get;set;}
   
   public EmpMasterReport(){
       EmpList = [ SELECT Name,Address__c FROM Employee_Master__c ];
   }
   
  }

vf page
<apex:page id="p1" standardController="Employee_Master__c" extensions="EmpMasterReport" sidebar="false">
  <apex:form>
  <apex:pageBlock title="Employee Master Details" id="Emp_Master">
    
    <apex:pageBlockTable value="{! Employee_Master__c }" var="em">
        <apex:column value="{! em.Name}"/>
        <apex:column value="{!em.Address__c}"/>
       
    
    </apex:pageBlockTable>
  </apex:pageBlock>
  </apex:form>
</apex:page>
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below

Standrad controller

public class EmpMasterReport
{
   public List<Employee_Master__c> EmpList {get;set;}
   
    public EmpMasterReport(ApexPages.StandardController stdController) {
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c FROM Employee_Master__c ];
    }
    
    public EmpMasterReport()
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c FROM Employee_Master__c ];
    }
   
}

VF page

<apex:page id="p1" standardController="Employee_Master__c" extensions="EmpMasterReport" sidebar="false">
    <apex:form>
        <apex:pageBlock title="Employee Master Details" id="Emp_Master">
            <apex:pageBlockTable value="{! EmpList }" var="em">
                <apex:column value="{! em.Name}"/>
                <apex:column value="{!em.Address__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page> 

Let us know if this will help you.

Thanks
Amit Chaudhary
This was selected as the best answer
Dutta SouravDutta Sourav
Hi Supriyo,

You just need to change this line:

<apex:pageBlockTable value="{! Employee_Master__c }" var="em">

Replace it with this:
<apex:pageBlockTable value="{!EmpList}" var="em">
Hope this helps!

Kind Regards,
Sourav.
 
Supriyo Ghosh 5Supriyo Ghosh 5
Thanks @amit.Its working fine.
Supriyo Ghosh 5Supriyo Ghosh 5
Hi,
I want to download this report as PDF.Any idea how to do that.Please help.
Amit Chaudhary 8Amit Chaudhary 8
Yes you can do that with .
<apex:page renderAs="pdf">
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_renderas_pdf.htm

Please start a new thread  (new question) so that other can help you