Showing posts with label Apex Describe. Show all posts
Showing posts with label Apex Describe. Show all posts

Saturday, 12 March 2016

Dynamic Query | Select All Field in SOQL | Select * Form Account


Dynamic Select * Form Account Query in salesforce


public Void GetAllField()
{
String query ='';
String SobjectApiName = 'Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

String strFields = '';

for(String fieldName : fieldMap.keyset() )
{
if(strFields == null || strFields == '')
{
strFields = fieldName;
}else{
strFields = strFields + ' , ' + fieldName;
}
}

query = 'select ' +
strFields + ' from ' + SobjectApiName + ' Limit 10 ';

List <Account> accList = Database.query(query);

}


Thanks ,
Amit Chaudhary

Wednesday, 16 December 2015

How to get RecordTypeId without SOQL | Get RecordTypeId by Describe Call



Some time in code we need to get recordTypeId . For that generally we used SOQL like below :-


Id contRecordTypeId = [Select id from RecordType where sObjectType = 'Contact' and developerName ='NameOfRecordType' ].id ; 


You can try below Describe to get record Type Id without SOQL


Id contRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('NameOfRecordType').getRecordTypeId();


Thanks
Amit Chaudhary

Tuesday, 15 December 2015

Record ID Prefix | Object Type from Record ID Prefix | Prefix of an Object in Salesforce.


We know there are two types of record-id are present in salesforce (18 digit -- Case Insensitive,15 digit -- Case Sensitive). Only 3 digit of ids represent object type .The following is a list of the Salesforce Standard Object ID prefixes


Object
Prefix
Account
001
NOTE
002
Contact
003
User
005
Opportunity
006
Activity
007


 Please check below post for more detail

If you want to get the Prefix of any object . Please try below code.


String keyPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
System.debug('PREFIX--' + keyPrefix );


If you want to get the Object Name from RecordId then please try below code.


public class SchemaGlobalDescribeToGetObjectName
{
    public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix)
 {
  String objectName = '';
        try
  {
            String IdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3); 
            Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
            for(Schema.SObjectType stype : gd.values())
   {
                Schema.DescribeSObjectResult r = stype.getDescribe();
                String prefix = r.getKeyPrefix();
                if(prefix!=null && prefix.equals(IdPrefix))
    {
                    objectName = r.getName();
                    System.debug('Object Name! ' + objectName);
                    break;
                }
            }
        }catch(Exception e){
            System.debug(e);
        }
        return objectName;
    }
}


If you want to Get the Object prefix by Workbench then please try below step :-

The easiest way to find out which key prefix maps to which object is by using a tool like the workbench . After you log into workbench, go to and pick an object from the pick list.

Step 1 :- Login in to Workbench

Step 2:- Info > Standard & Custom Objects 


Step 3:- Then select your object





Thanks
Amit Chaudhary

Thursday, 19 November 2015

Apex Describe | Dynamic retrieval of object label & field label


     Dynamic retrieval of object label & field label


You can describe sObjects either by using tokens or the describeSObjects Schema method.


Apex Class


public with sharing class DescibeDemoController 
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public List<FieldWrapper> listField{get;set;}

    public DescibeDemoController() 
    {
        listField = new List<FieldWrapper>();
    }

    // find all sObjects available in the organization
    public  List<SelectOption> getListObejectName() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
            objNames.add(new SelectOption(name,name));
        return objNames;
    }

    
    // Find the fields for the selected object
    public void showFields() 
    {
        listField.clear();
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
        {
            schema.describefieldresult dfield = sfield.getDescribe();
            FieldWrapper wObj = new FieldWrapper();
            wObj.fieldName = dfield.getLabel ();
            wObj.fieldAPIName = dfield.getname();
            listField.add(wObj);
        }
    }

    public class FieldWrapper
    {
        public String fieldName {get; set;}
        public String fieldAPIName {get; set;}
    }

}
VF Page


<apex:page controller="DescibeDemoController">
    <apex:form id="Describe">
        <apex:pageBlock id="block2" >
            <apex:pageblockbuttons location="top" >
                    <apex:commandButton value="Show Fields" action="{!showFields}" />
            </apex:pageblockbuttons>
            
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Object Name</apex:outputLabel>
                    <apex:selectList value="{!selectedObject}" size="1">
                        <apex:selectOptions value="{!ListObejectName}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
        </apex:pageBlock>
        
        <apex:pageBlock id="result" title="Field Detail for {!selectedObject}" rendered="{!if(listField.size > 0 ,true,false)}"   >
            <apex:pageBlockTable value="{!listField}" var="field" rendered="{!if(listField.size > 0 ,true,false)}"> 
                <apex:column value="{!field.fieldName }" headerValue="Name" />
                <apex:column value="{!field.fieldAPIName }"  headerValue="API Name"/>
            </apex:pageblockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>




Related link