function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ElectronElectron 

Get Record Type ID by Name

I want to use record Type to limit trigger action. 

 

Now I use querey to get it my code liks below

string Id = [SELECT id from RecordType where Name ='Someone'].Id;

 

I also found this page, we could use method to get the Record Type Id to avoid the query.

I still can't understand it.

 

Is there some simple and easy wayt to get the ID only by Record Type's Name?

Best Answer chosen by Admin (Salesforce Developers) 
vagishvagish

You can use:

 

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

 

Here, 'Development' is the record type's name. You shuld use 'someone'. Also, you will have to specify your SObject type, here I have mentioned Account.

 

Thanks,

Vagish

All Answers

vagishvagish

You can use:

 

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

 

Here, 'Development' is the record type's name. You shuld use 'someone'. Also, you will have to specify your SObject type, here I have mentioned Account.

 

Thanks,

Vagish

This was selected as the best answer
ElectronElectron

Thank you

 

For a record Type on Contact the code should be 

 

Id devRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Customer').getRecordTypeId();

 I thoutht the Schema.SObjectType should be take replaced by the SObject's Name.

ElectronElectron

Also I want to recommand everyone, don't use it in trigger directly.

 

because normal user doesn't haver enough permission to execute the method, if not you may get  same error I met before.

http://boards.developerforce.com/t5/Apex-Code-Development/Visual-force-Email-Template-to-get-Related-Object-Name-by-Look/m-p/618393#M113691

sfdcFanBoysfdcFanBoy
In the same way how do I get the recordType Developer name. Below code doesn't work
 
Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getDeveloperName();
DEBADYUTI SIL 15DEBADYUTI SIL 15
Hope below code Helps: 

String objectAPIName = 'Case' ; //any object api
  Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
   List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
    Map<String,Id> mapofCaseRecordTypeNameandId = new Map<String,Id>();
    for(Schema.RecordTypeInfo info : recordTypeInfo){
     mapofCaseRecordTypeNameandId.put(info.getName(),info.getRecordTypeId());
    }
   system.debug('***mapofCaseRecordTypeNameandId*'+mapofCaseRecordTypeNameandId);
DEBADYUTI SIL 15DEBADYUTI SIL 15
Id devRecordTypeId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('Billing').getRecordTypeId();

This way we can pass object api name and record type name as argument
Jiri LahodaJiri Lahoda
Hi,
Not good solution for multy-language orgs. There is actualy a workaround that I am using (using label and translations for each recordtype):
Schema.SObjectType.Event.getRecordTypeInfosByName().get(Label.RecordTypeName).getRecordTypeId();


Vote for idea:
https://success.salesforce.com/ideaview?id=08730000000K9zoAAC
Lakshman KanukolluLakshman Kanukollu
[select id from Recordtype where Name='recordtype name'] with this you will get record type id
Thomas GagnéThomas Gagné
To guard against record type name collisions in the future with installed pacakges, I recommend developers also select " where namespace = '' ".

We were just bitten (hard) by an installed package that included its own "Individual" and "Business" Account record types.  We are now having to go through all our code making sure that when we select our record types we make sure not to select installed packages' types.
Sagar Nagvekar 14Sagar Nagvekar 14
Get the list of Ids of record type of opportunity for fortune 500 companies or high net worth individuals :-
For fortune 500 companies :-
Record Type Label :- Fortune 500 Opportunity
Record Type Name :- Fortune_500_Opportunity

For high net worth individuals :-
Record Type Label :- High Net Worth
Record Type Name :- High_Net_Worth

List<Id> allOppIds = new List<Id>();
Schema.DescribeSObjectResult doOpportunityRTs = Schema.SObjectType.Opportunity;  
Map<String,Schema.RecordTypeInfo> rtMapByOpportunity = doOpportunityRTs.getRecordTypeInfosByName(); 
for(String rtTypeInfo: rtMapByOpportunity.keySet())
{
    if(rtTypeInfo == 'Fortune 500 Opportunity') // Note the record type label is used here, not the record type name
    {
        System.debug(LoggingLevel.INFO, '//// This is opportunity record type for identifying fortune 500 companies');
        Schema.RecordTypeInfo recordType1 = rtMapByOpportunity.get('Fortune 500 Opportunity');
        System.debug(LoggingLevel.INFO, '//// The fortune 500 record type details are ' + recordType1);
        Id for500RecordTypeId = recordType1.getRecordTypeId();
        System.debug(LoggingLevel.INFO, '//// for500RecordTypeId is ' + for500RecordTypeId);
        allOppIds.add(for500RecordTypeId);
    }

    if(rtTypeInfo == 'High Net Worth') // Note the record type label is used here, not the record type name
    {
        System.debug(LoggingLevel.INFO, '//// This is opportunity record type for identifying high net worth individuals');
        Schema.RecordTypeInfo recordType2 = rtMapByOpportunity.get('High Net Worth');
        System.debug(LoggingLevel.INFO, '//// The high net worth record type details are ' + recordType2);
        Id highNetWorthRecordTypeId = recordType2.getRecordTypeId();
        System.debug(LoggingLevel.INFO, '//// highNetWorthRecordTypeId is ' + highNetWorthRecordTypeId);
        allOppIds.add(highNetWorthRecordTypeId);
    }
}
System.debug(LoggingLevel.INFO, '//// allOppIds size is ' + allOppIds.size()); // This should print size = 2
Yury BondarauYury Bondarau
You can as well get RecordTypeId by Developer Name (since Summer 18) which is better. 
Id clientRT = SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Client').getRecordTypeId();

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject_describe.htm
Thomas GagnéThomas Gagné
Finding a recordtype using its name is a bad plan.  Record type names change.  Developer names do not.  Unfortunately, the Describe API doesn't support retrieval using developer name (and namespace prefix).

This is why it's important to use a class to do it correctly. 

Ours retrieves all the record types into static maps, so that the query is performed only once, and uses the developername and namespaceprefix values to make sure to always retrieve the correct record type.  When a namespace isn't provided, it uses the default.
Target Org 30Target Org 30
How to get QueueId by Name using Schema methods: like below:-                                                                                                                                  QueueId = Schema.SObjectType.Lead.getQueueIdInfosByName().get('BankingQueue').getQueueId();
Moh_SarfarajMoh_Sarfaraj
Hi, for a dynamic query you have to only pass object name & record type name for getting the id of particular record type

String objectName = 'Contact';
String recordTypeName = 'Lock Record type';

Id recordypeId = Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosByName().get(recordTypeName).getRecordTypeId();

Thanks,
Sarfraz
GANESH ALUGU 347GANESH ALUGU 347
To get a recordTypeId dynamically based on the developername of recordtype i.e API Name of RecordType
Id recordTypeId= SObjectType.ObjectAPIName.getRecordTypeInfosByDeveloperName().get('RecordTypeAPIName').getRecordTypeId();
System.debug('RecordTypeId=== '+recordTypeId);
Troy CenterTroy Center

Thanks vagish, for the answer back in 2013. I still use yours today. As do most dev's I know of. TC - Seattle, USA. 

David Roberts 4David Roberts 4
Following Thomas Gagné's suggestion, I used:
SELECT Id FROM RecordType where DeveloperName = 'Training' and NamespacePrefix = '' and SobjectType='Event'
to get a particular Event record type.
so a method will be something like:
 
@AuraEnabled
    public static Event createNewEvent() {
        
        //create an Event record of a particular record type
        Id eventRecordId;
        Event evt;
        List<RecordType> lstRecordTypes = [SELECT Id FROM RecordType where DeveloperName = 'Training' and NamespacePrefix = '' and SobjectType='Event'];
        if ( !lstRecordTypes.isEmpty() ) {
            eventRecordId = lstRecordTypes[0].Id;
            evt = new Event(RecordTypeId = eventRecordId);}
        else{
            //always create an event but change according to your circumstances
            evt = new Event();
		}//endif not empty
		return evt;
    }//createNewEvent