3

The documentation for Schema.SObjectType.myObj.getRecordTypeInfosByName() says

The current user is not required to have access to a record type to see it in this map

So I was in the intention of that the method will run in system context.

However when the translation is enabled, I noticed this seems runs in user context as it's giving NPE for specific users.

Schema.SObjectType.myObj.getRecordTypeInfosByName().get('myRType') works perfect for other users while it's returning null for users who have the record type translated. And confirmed it by having a debug point and verifying the map is having the translated record type.

Is there any confirmation on a documentation that this is the expected behavior? I believe there should be some centralized field to retrieve the master value.

P.S. It seems summer '18 preview is having getRecordTypeInfosByDeveloperName() delivered. But still didn't have a chance to test with that.

  • @ca_peterson is there a confirmation this meant to be run in user context? it seems summer '18 preview is still not available for us and can't check getRecordTypeInfosByDeveloperName(). What I can confirm is that the map which returns by getRecordTypeInfosByName() having Developer Name also translated. – highfive May 11 '18 at 4:05
3

Record Type Names work the same way as a Field or Object's Label. Since the Record Type Name is subject to translation, it can differ depending on the user's locale settings.

Because of this, Schema.SObjectType.myObj.getRecordTypeInfosByName() will return different Record Type Names depending on the running user's locale.

Currently the only way to guarantee the correct Record Type is returned is by querying the Record Type using the DeveloperName field.

Example:

[SELECT Id, Name FROM RecordType WHERE SObjectType = 'My_SObject__c' AND DeveloperName = 'myRTType'];

But as you mentioned, Summer '18 release is introducing Schema.DescribeSObjectResult.getRecordTypeInfosByDeveloperName() and Schema.RecordTypeInfo.getDeveloperName() which you could then use to guarantee the correct Record Type is returned regardless of the running user. This would then become the optimal choice as it uses describe calls which are more efficient than SOQL and do not count against limits.

  • It seems summer '18 preview is still not available for us. Have to check later if it's helpful or not. What I can confirm is that the map which returns by getRecordTypeInfosByName() having Developer Name also translated. – highfive May 11 '18 at 4:00
  • 'DeveloperName' is the API Name of a Record Type and will always be unique (locale/translation will never change this.). And 'getRecordTypeInfosByName' returns the Names of Record Types (similar to Labels) and not the 'DeveloperName'. Summer 18 release will go live in a few weeks, so if you cannot wait then you can use the SOQL method I mentioned for now. – Bob Lopez May 11 '18 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.