2

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();
}
5
  • Are you trying to run getRecordTypeIdForObject('PersonAccount', 'PersonType')? That won't work, because PersonAccount is not an Sobject type. I would expect getRecordTypeIdForObject('Account', 'PersonType') to work - if you're using that and it's not working, please specify what error or other result you're getting. Dec 17, 2019 at 23:51
  • 1
    I use 'Account' and 'PersonType', but it returns null
    – Irene
    Dec 18, 2019 at 0:15
  • If 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? Dec 18, 2019 at 0:43
  • One of the record types does have a space in it yes. I didn't think that would matter?
    – Irene
    Dec 18, 2019 at 0:54
  • I mean, the string you pass has to match the Map key. 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). Dec 18, 2019 at 1:06

1 Answer 1

1

Not sure what is going on for you, but in my developer Person Accounts org I tried:

System.debug(
  ((SObject)Type.forName('Account').newInstance())
  .getSObjectType()
  .getDescribe().getRecordTypeInfosByName()
);

Here's what I got:

19:23:51.2 (40515202)|USER_DEBUG|[1]|DEBUG|{
Business Account=Schema.RecordTypeInfo[getDeveloperName=Business_Account;getName=Business Account;getRecordTypeId=01246000000QTvKAAW;isActive=true;isAvailable=true;isDefaultRecordTypeMapping=false;isMaster=false;], 
Master=Schema.RecordTypeInfo[getDeveloperName=Master;getName=Master;getRecordTypeId=012000000000000AAA;isActive=true;isAvailable=true;isDefaultRecordTypeMapping=false;isMaster=true;],
Person Account=Schema.RecordTypeInfo[getDeveloperName=PersonAccount;getName=Person Account;getRecordTypeId=01246000000QUHTAA4;isActive=true;isAvailable=true;isDefaultRecordTypeMapping=true;isMaster=false;]}

So in principle this should work fine. What kind of errors are you getting?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .