You need to sign in to do that
Don't have an account?
Electron
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?
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
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
Thank you
For a record Type on Contact the code should be
I thoutht the Schema.SObjectType should be take replaced by the SObject's Name.
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
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);
This way we can pass object api name and record type name as argument
Not good solution for multy-language orgs. There is actualy a workaround that I am using (using label and translations for each recordtype):
Vote for idea:
https://success.salesforce.com/ideaview?id=08730000000K9zoAAC
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.
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
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject_describe.htm
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.
String objectName = 'Contact';
String recordTypeName = 'Lock Record type';
Id recordypeId = Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosByName().get(recordTypeName).getRecordTypeId();
Thanks,
Sarfraz
Id recordTypeId= SObjectType.ObjectAPIName.getRecordTypeInfosByDeveloperName().get('RecordTypeAPIName').getRecordTypeId();
System.debug('RecordTypeId=== '+recordTypeId);
Thanks vagish, for the answer back in 2013. I still use yours today. As do most dev's I know of. TC - Seattle, USA.
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: