The usual way to get a recordTypeId in Apex is to make a query like this:
1 |
[Select id from RecordType where sObjectType = 'Account' and developerName ='Person Account' ].id |
Instead of that try this line:
1 |
Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId() |
Just replace Account and ‘Person Account’ by your Object and Record Type Name
UPDATE:
I’ve been notice that “Person Account” is the label description and not the developer name of the Record Type. Having said that if you work in a multi-lang org, You’ll need to make a Query.
The most efficient way to make that kind of query is to cache the result in a static variable. Like this:
1 2 3 4 5 6 7 8 9 10 11 |
class AccountHelper{ public static String personRecordTypeId; public static String getPersonRecordTypeId(){ if(AccountHelper.personRecordTypeId == null){ AccountHelper.personRecordTypeId =[Select id from RecordType where sObjectType = 'Account' and developerName ='Person Account' ].id } return AccountHelper.personRecordTypeId ; } |
Happy coding
About Martin Borthiry
I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment
- Web |
- More Posts (15)