Usually, we fetch record type with the help of Salesforce SOQL which counts in Salesforce SOQL governors limit.
Example :
[Select id from RecordType where sObjectType = 'Account' and developerName ='CustomerAccount'].id
Instead of SOQL which counts in governors limit, you can use dynamic apex code to access RecordTypeId.
Here is a simple way to do this without any SOQL query:
Id customerAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('CustomerAccount').getRecordTypeId();
Here, 'CustomerAccount' is the account`s record type name. Also, you will have to specify your SObject type name like Account, Contact, Case etc, here in this example I have mentioned Account.
Basically to avoid SOQL governor Limit we can use dynamic apex code to get RecordTypeId. Schema
Full apex code in detail:
A normal way to access record type id using SOQL query which counts in Salesforce SOQL governors limit.
Step 1: Create apex class
public class GetRecordTypeId{
Public ID objId{ get; set; }
public GetRecordTypeId(){
List<RecordType> lstRecordType = [Select id from RecordType where sObjectType = 'Account' and developerName ='CustomerAccount'];
objId = lstRecordType[0].id;
}
}
Best way to get record type id of any object using Schema which does not count in Salesforce SOQL governors limit.
In below code snippet, instead of hard coding the object name and record type label, I am explaining dynamic approach which gives flexibility to admin or developer.
Step 1: Create apex class
public class GetRecordTypeId
{
public GetRecordTypeId()
{ }
// You need to pass two parameters SObject Type and RecordType Label not the Record Type Developer Name
public static ID GetRecordTypeIdDynamically(String ObjectName, String RecordTypeLabel)
{
SObject ObjSObject;
// Declaring Schema
Schema.SObjectType objSchemaSObjectType = Schema.getGlobalDescribe().get(ObjectName);
if (objSchemaSObjectType != null)
{
ObjSObject = objSchemaSObjectType.newSObject();
// Declaring Object which Contains methods for describing sObjects.
Schema.DescribeSObjectResult ObjDesSObjectRes = ObjSObject.getSObjectType().getDescribe();
if (ObjDesSObjectRes != null)
{
Map mapRecordType = ObjDesSObjectRes.getRecordTypeInfosByName();
if (mapRecordType != null)
{
Schema.RecordTypeInfo objRecordTypeRes = mapRecordType.get(RecordTypeLabel);
if (objRecordTypeRes != null)
{
return objRecordTypeRes.getRecordTypeId();
}
}
}
}
return null;
}
}
Example :
[Select id from RecordType where sObjectType = 'Account' and developerName ='CustomerAccount'].id
Instead of SOQL which counts in governors limit, you can use dynamic apex code to access RecordTypeId.
Here is a simple way to do this without any SOQL query:
Id customerAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('CustomerAccount').getRecordTypeId();
Here, 'CustomerAccount' is the account`s record type name. Also, you will have to specify your SObject type name like Account, Contact, Case etc, here in this example I have mentioned Account.
Basically to avoid SOQL governor Limit we can use dynamic apex code to get RecordTypeId. Schema
Full apex code in detail:
A normal way to access record type id using SOQL query which counts in Salesforce SOQL governors limit.
public class GetRecordTypeId{
Public ID objId{ get; set; }
public GetRecordTypeId(){
List<RecordType> lstRecordType = [Select id from RecordType where sObjectType = 'Account' and developerName ='CustomerAccount'];
objId = lstRecordType[0].id;
}
}
Best way to get record type id of any object using Schema which does not count in Salesforce SOQL governors limit.
In below code snippet, instead of hard coding the object name and record type label, I am explaining dynamic approach which gives flexibility to admin or developer.
Step 1: Create apex class
public class GetRecordTypeId
{
public GetRecordTypeId()
{ }
// You need to pass two parameters SObject Type and RecordType Label not the Record Type Developer Name
public static ID GetRecordTypeIdDynamically(String ObjectName, String RecordTypeLabel)
{
SObject ObjSObject;
// Declaring Schema
Schema.SObjectType objSchemaSObjectType = Schema.getGlobalDescribe().get(ObjectName);
if (objSchemaSObjectType != null)
{
ObjSObject = objSchemaSObjectType.newSObject();
// Declaring Object which Contains methods for describing sObjects.
Schema.DescribeSObjectResult ObjDesSObjectRes = ObjSObject.getSObjectType().getDescribe();
if (ObjDesSObjectRes != null)
{
Map mapRecordType = ObjDesSObjectRes.getRecordTypeInfosByName();
if (mapRecordType != null)
{
Schema.RecordTypeInfo objRecordTypeRes = mapRecordType.get(RecordTypeLabel);
if (objRecordTypeRes != null)
{
return objRecordTypeRes.getRecordTypeId();
}
}
}
}
return null;
}
}
Step 2 :
Now pass parameters to above method.
Parameters are: Object name and Record Type label. Do not pass developer name.
ID RecordTypeId = GetRecordTypeId.GetRecordTypeIdDynamically('Account' , 'CustomerAccount');
Copy and paste above code snippets and reuse it where you require.
More Salesforce Blogs:
Territory Management In Salesforce Can Increase Your Profit !
Import CSV file In Salesforce using Apex in 3 Easiest Steps
Check out Salesforce Daily Limit Only in 5 Simplest Steps
Learning Pagination In Salesforce Is Not Difficult At All ! You Just Need 3 Easy Steps
How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind
Schedulable Batch Apex In 3 Easy steps In Salesforce
Run Schedule a Class In Every 5 Mins in Salesforce
An Introduction to Visualforce View State in Salesforce
Actionpoller Tag in Visualforce Page
Main Difference Between ISBLANK And ISNULL in Salesforce
Display Helptext In Visualforce Page In Less Time, Check this out
Have I missed anything? Please comment below and I will add that to this list.
Now pass parameters to above method.
Parameters are: Object name and Record Type label. Do not pass developer name.
ID RecordTypeId = GetRecordTypeId.GetRecordTypeIdDynamically('Account' , 'CustomerAccount');
Copy and paste above code snippets and reuse it where you require.
More Salesforce Blogs:
Territory Management In Salesforce Can Increase Your Profit !
Import CSV file In Salesforce using Apex in 3 Easiest Steps
Check out Salesforce Daily Limit Only in 5 Simplest Steps
Learning Pagination In Salesforce Is Not Difficult At All ! You Just Need 3 Easy Steps
How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind
Schedulable Batch Apex In 3 Easy steps In Salesforce
Run Schedule a Class In Every 5 Mins in Salesforce
An Introduction to Visualforce View State in Salesforce
Actionpoller Tag in Visualforce Page
Main Difference Between ISBLANK And ISNULL in Salesforce
Display Helptext In Visualforce Page In Less Time, Check this out
Have I missed anything? Please comment below and I will add that to this list.
Enjoy! If you have any questions, comments, 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.
No comments:
Post a Comment