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
narensfdcnarensfdc 

Dynamic Apex for getting the fields of an sobject

Hi,

 

    Please help me in coding for

    How to get all the fields of sObject using dynamic Apex?(complete code)  ---  like for account object

    

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
<apex:page controller="ControllerClassName" action="{!autoRun}">

   <apex:form>

      <apex:pageblock>

           <apex:pageblockTable value="{!strList}" var="obj">

              <apex:column value="{!obj}"/>

           </apex:pageblockTable>

      </apex:pageblock>

   </apex:form>

</apex:page>

 

Class:

public class ControllerClassName
{
    public List<String> strList { get;set; }
    
    public void autoRun() 
    {
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();

        strList = new List<String>(objectFields.keySet());

    }
}

 Try this.

All Answers

JBabuJBabu

Hi,

 

Your requirement is not clear. Hopefully the below link will helps you.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic.htm

 

Thanks,

Babu.

narensfdcnarensfdc

Hi

 

     My requirement is using dynamic apex to disply all  sobjects on VF page we have the code like

     

     

<apex:page controller="dynamic1cls" >
   <apex:form >
      <apex:pageblock >
           <apex:pageblockTable value="{!lst}" var="obj">
              <apex:column value="{!obj}"/>
           </apex:pageblockTable>
      </apex:pageblock>
   </apex:form>
</apex:page>

 

 

public with sharing class dynamic1cls {

   

   public List<String> lst{get; set;}

     public dynamic1cls(){

         List<Schema.SobjectType> lstobj = Schema.getGlobalDescribe().values();
             lst = new List<String>();
            for(Schema.SobjectType s: lstObj)

            lst.add(string.valueOf(s));

    }
}

 

 

In the same way i need to diplay the fields of an Account object on Visualforce page 

 

Please help me out

 

Thanks.

Naidu PothiniNaidu Pothini
<apex:page controller="ControllerClassName" action="{!autoRun}">

   <apex:form>

      <apex:pageblock>

           <apex:pageblockTable value="{!strList}" var="obj">

              <apex:column value="{!obj}"/>

           </apex:pageblockTable>

      </apex:pageblock>

   </apex:form>

</apex:page>

 

Class:

public class ControllerClassName
{
    public List<String> strList { get;set; }
    
    public void autoRun() 
    {
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();

        strList = new List<String>(objectFields.keySet());

    }
}

 Try this.

This was selected as the best answer
narensfdcnarensfdc

Hi 

 

     Thanks for your reply and the code is fine.

 

Naren

narensfdcnarensfdc

Hi Naidu,

            

             The code is fine,if we want to display only custom fields,then how to code.

              Please help me out.

          

Thanks,

Naren.

rajesh k 10rajesh k 10
Hi,
                      one page  I displayed all fields using schema methods(here object name through URL will come like string strtemp = 'rajesh__'+strFieldName + '__c'; )and wrapper class. using wrapper class i gave each field as Edit and Delete command links .When i click delete command link how to delete that delete command link related field from database

                       Another visualforce page i displayed all Custom objects using schema methods and wrapper class. Using wrapper class i gave each Custom object as Edit and Delete command link .When i click delete command link how to delete that delete command link related object from databse.

please help me...........
rajesh k 10rajesh k 10
Hi narensfdc,

                          Using above code here
                 lst.add(string.valueOf(s));

How to add object related created dates   ?

please help me..............