Tag Archives: Apex

How to get a RecordType Id by Name without SOQL?

Sometimes in apex we required RecordType Id of an object by its Name. So, here is an example how we can find RecordType Id by Name.

In below example BankAccount__c is the custom object and “Savings Account” is one of the RecordType Name of BankAccount__c object. Now without SOQL I’ve to find “Savings Account” RecordType Id.

String recordTypeName = 'Savings Account';
Schema.SObjectType.BankAccount__c.getRecordTypeInfosByName().get(recordTypeName).getRecordTypeId();

Display all the fields of a sObject using Apex & Visualforce Page

Apex Code:

public class  DisplayAllFieldsClass {
 
     public map<string,schema.sobjectfield> data {get;set;}
   
     public  DisplayAllFieldsClass (){
         data = Schema.SObjectType.BISWAJEET__Student__c.fields.getMap();
     }
}

Visualforce Page Code:

<apex:page doctype="html-5.0" showheader="false" controller="DisplayAllFieldsClass">
    <apex:datatable value="{!data}" var="d">
             <apex:column headervalue="Field Name">
                 {!d}
             </apex:column>         
         </apex:datatable>
</apex:page>

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);
    }
}