Thursday, 22 June 2017

How to get the RecordTypeId with Apex code in Salesforce platform

Hello everyone,

As a salesforce developer i have faced some of the situations to reuse the same code to build the salesforce project. In this blog i want include some reusable piece of salesforce code in day to day life activities. 


while writing test classes some times we need to get the record type id of an object in that case just use the below code,

Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();

where
    Account is the name of the object,
    The String 'Development' is the name of the record type.

Just replace the object name with your sObject and put the name of your record type in place of the string 'Development'.

for example to get the recordtypeId of the Opportunity Object having the record type name 'VIP', the code will look like,

Id opprecordtypeId=Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('VIP').getRecordTypeId();

No comments:

Post a Comment