Concepts or coding lessons of Salesforce that you can implement easily

Secret Trick To Retrieve Fields For Sobject Using Apex

Recently, I had a use case like dynamically determine what fields had been queried or set on a sObject. Unfortunately, we can not query or SOQL to object to retrieve fields metadata. 

Apex has a class called Schema. It includes all the schema of your organization. Objects, their fields, field type etc. can all retrieved using schema class.

Alternative of hard-coding this list, which is not sustainable or repairing it regularly, a good approach is to use Salesforce field describe results methods.

In the below example I am going explain to you how to Get all fields for sObject using Apex by utilizing the metadata available from the DescribeSObjectResult class. 

Follow the below steps:

Step 1: Create Apex Controller

public class RetrieveFieldsController
{
public List<String> objFields { get;set; }
public void retrieveFields(){
  // Object Name whose fields we have to retrieve
  String objectName = 'Account'; 
  Map<String , Schema.SObjectType> schemaGlobalDescription = Schema.getGlobalDescribe();
   Schema.sObjectType objType = schemaGlobalDescription.get(objectName); 
   Schema.DescribeSObjectResult objDescribeSObjectResult = objType.getDescribe(); 
   Map<String , Schema.SObjectField> mapFieldList = objDescribeSObjectResult.fields.getMap();  
  for(Schema.SObjectField field : mapFieldList.values())  
  {  
    Schema.DescribeFieldResult fieldResult = field.getDescribe(); 
// isAccessible () Returns true if the current user can see this object, false otherwise
      if(fieldResult.isAccessible())
        objFields.add(fieldResult.getName());
   }
  
  System.debug('objFields--'+objFields);
 }
}

Step 2: Create Visualforce page

<apex:page controller="RetrieveFieldsController" action="{!retrieveFields}">
   <apex:form>
      <apex:pageblock>
           <apex:pageblockTable value="{!objFields}" var="obj">
              <apex:column value="{!obj}"/>
           </apex:pageblockTable>
      </apex:pageblock>
   </apex:form>

</apex:page>



The following steps show you how you can work with describe information in Salesforce Apex class:
  1. Generate a list or map of tokens for the sObjects in your Salesforce organization (see Accessing All sObjects.)
  2. Determine the sObject which you need to access.
  3. Generate the describe result for the sObject you selected.
  4. If required, then generate a map of field tokens for the sObject (see Accessing All Field Describe Results for an sObject.)
  5. Then Generate the describe result for that field the code needs to access.
If you want to retrieve Names of All Objects Using Apex then check my post:



In Details Schema.getGlobalDescribe() :


It returns a map of all sObject names (keys) to sObject tokens (values) for the standard and custom objects defined in your organization.

The map has the following characteristics:
  • It is dynamic, that is, based on permissions, it generates at runtime on the sObjects currently available for the organization.
  • The sObject names are case insensitive.
  • The keys are prefixed by the namespace, if any.*
  • The keys indicate whether the sObject is a custom object.
Note: If the getGlobalDescribe method is called from an installed managed package, it returns all the sObject names and tokens for Chatter sObjects, such as NewsFeed and UserProfileFeed, even if Chatter is not enabled in the installing salesforce organization. But if the getGlobalDescribe method is called from normal apex class which is not part of installed manage package then this is not true.


Enjoy! If you have any questions, comments etc. please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

loading...

No comments:

Post a Comment