Tag Archives: sObject

Get All Required Fields of sObject Using Apex in Salesforce

Sample Code:

Here in below code I’m getting all required fields of “Account” object.

Map<String, Schema.SObjectType> mapObj  = Schema.getGlobalDescribe();
Schema.SObjectType sObjType = mapObj.get('Account');
Schema.DescribeSObjectResult objDescribe = sObjType.getDescribe();
Map<String,Schema.SObjectField> mapFields = objDescribe.fields.getMap();
List<String> requiredFieldList = new List<String>();

for(String obj : mapFields.keyset()) {
    Schema.DescribeFieldResult describeField = mapFields.get(obj).getDescribe();
    if (describeField.isCreateable()  && !describeField.isNillable() && !describeField.isDefaultedOnCreate()) {
        requiredFieldList.add(obj);
        System.debug(obj);
    }
}

SOQL Query to Select All Fields of sObject

Sample Code:

public class DynamicSOQL {
  public static sObject getAllFieldsData(String objTypeName) {
        sObject sObj;
        Schema.DescribeSObjectResult[] descSObj = Schema.describeSObjects(new String[]{objTypeName});
        Map<String, Schema.SObjectField> fieldMap = descSObj[0].fields.getMap();
        List<String> fieldNames = new List<String>(fieldMap.keySet());
        system.debug('Fields-' + String.join(fieldNames, ','));
        String queryString = 'SELECT '+ String.join(fieldNames, ',') +' FROM '+ objTypeName +' LIMIT 1';
        sObj = Database.query(queryString);
        return sObj;
    }
}

Check Salesforce sObject is Custom or Standard Type

Check for a specific object:

Schema.DescribeSObjectResult objDescribe = Schema.sObjectType.Account;
System.debug(objDescribe.isCustom());

Check for all objects:

List<String> customObjectList = new List<string>();
List<String> standardObjectList = new List<string>();

for(Schema.SobjectType obj : Schema.getGlobalDescribe().values()) {
    Schema.DescribeSObjectResult objDescribe = obj.getDescribe();
    if(objDescribe.isCustom()) {
        String objName = objDescribe.getName();
        customObjectList.add(objName);
    }
    else {
        String objName = objDescribe.getName();
        standardObjectList.add(objName);
    }
}

System.debug('Custom Object List - ' + customObjectList);
System.debug('Standard Object List - ' + standardObjectList);

Get All Objects and Its Fields in Salesforce Org

We can use Schema.getGlobalDescribe() to get all properties of sObject and its fields.

Apex Class:

public class SampleController {
    
    Public string selectedObj {get;set;}
    public String selectedField {get; set;}
    
    Public List<Selectoption> getObjList(){
        List<Schema.SObjectType> objList = Schema.getGlobalDescribe().Values();     
        List<SelectOption> objNames = new List<SelectOption>();
        objNames.add(new SelectOption('','-- Select --'));
        for(Schema.SObjectType obj : objList)
        {
            objNames.add(new SelectOption(obj.getDescribe().getName(),obj.getDescribe().getLabel()));
        }
        objNames.sort();
        return objNames;
    }
    
    public List<SelectOption> getObjectFields() {
        List<SelectOption> fieldNames = new List<SelectOption>();
        fieldNames.add(new SelectOption('','-- Select --'));
        if(!String.isBlank(selectedObj)){
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjSchema = schemaMap.get(selectedObj);
            Map<String, Schema.SObjectField> fieldMap = ObjSchema.getDescribe().fields.getMap();
            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName, fieldMap.get(fieldName).getDescribe().getLabel()));
            }
        }
        return fieldNames;
    }      
}

Visualforce Page:

<apex:page controller="SampleController">
    <apex:form> 
        <apex:pageBlock>
            <apex:pageBlockSection columns="2">
                
                <apex:pageBlockSectionItem >
                    <apex:outputlabel value="Object Names :"/> 
                    <apex:actionRegion >      
                        <apex:selectList value="{!selectedObj}" size="1">
                            <apex:selectOptions value="{!ObjList}"/>
                            <apex:actionSupport event="onchange" rerender="objFields"/>
                        </apex:selectList>
                    </apex:actionRegion>                         
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                    <apex:outputlabel value="Field Names :"/>   
                    <apex:outputPanel id="objFields">   
                        <apex:actionRegion >  
                            <apex:selectList value="{!selectedField}" size="1">
                                <apex:selectOptions value="{!ObjectFields}"/>
                            </apex:selectList>
                        </apex:actionRegion>      
                    </apex:outputPanel>
                </apex:pageBlockSectionItem>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output: