I can get them by running the following query
SELECT Name, SobjectType, IsPersonType
FROM RecordType
WHERE SobjectType='Account' AND IsPersonType=True
or by doing
public Id personRECORDTYPEID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('PersonType').getRecordTypeId();
but I can't seem to get it right using my code below. I can't use the above because my code is reading any kind of object so specifing 'Account' is not really an option. I want to keep it generic for any type of object or recordtype by passing them in.
I pass the object name and recordtype name to this code, but it doesn't work for a PersonAccount.
public static Id getRecordTypeIdForObject(String objName, String recTypeName) {
return ((SObject)Type.forName(objName).newInstance())
.getSObjectType()
.getDescribe()
.getRecordTypeInfosByName()
.get(recTypeName)
.getRecordTypeId();
}
getRecordTypeIdForObject('PersonAccount', 'PersonType')
? That won't work, becausePersonAccount
is not an Sobject type. I would expectgetRecordTypeIdForObject('Account', 'PersonType')
to work - if you're using that and it's not working, please specify what error or other result you're getting.PersonType
is really the correct Recordtype Name, then it should work. If it's not, then your second example would fail Recordtype Names often have spaces in them, though. Can you paste the results of your first query?getRecordTypeInfosByName()
gets a Map whose key is the recordtype labels, which may have spaces.'Person Type'
and'PersonType'
are two different strings/map keys. Maybe getRecordTypeInfosByDeveloperName() is better for your use case (though DeveloperName isn't necessarily just removing the spaces).