I am unable to retrieve Asset RecordTypes when I call following method in my apex code:


It throws NullPointerException. It works for other standard and custom objects and able to retrieve RecordTypes.

Apex Method:

public Static Map<String, RecordType> getRecordTypeBySObjectType(String sobjectType) {
        Map<String, RecordType> mapRecordType = new Map<String, RecordType>();
        List<RecordType> recordTypeList = [SELECT Id, DeveloperName FROM RecordType WHERE SobjectType =: sobjectType];
        for ( RecordType itrRecordType : recordTypeList )
            mapRecordType.put(itrRecordType.DeveloperName, itrRecordType);
        return mapRecordType;

I know nothing wrong with code but not sure if its related to data issue.

  • Are you sure there is actually a RecordType whose SObjectType='Asset' and whose DeveloperName='Auto'? – Adrian Larson Oct 3 '17 at 15:47
  • Yes I am sure and its working from workbench and returning the Id as well but not working in my code. – JavaAster Oct 3 '17 at 15:48
  • Please include a screenshot of your workbench query as evidence. – Adrian Larson Oct 3 '17 at 15:49
  • Won't be able to include it as I had to mock it up to put in this forum. But believe me its working. Any idea if something to do with data sharing or any config that I am missing here. – JavaAster Oct 3 '17 at 15:55
  • I ran this exact code and it worked as expected. I received no error so it has to be something data related. I created a record type of 'Auto' on Asset and ran the code and output the Id into a debug ran from Anonymous Apex and I received no error. – JRiffe Oct 3 '17 at 18:34

Your version of the class needs to be API version 38 or above. Salesforce added Record Types to Assets in the Spring '15 release.

The release notes pertaining to Record Types and Assets.

| improve this answer | |
  • That would be compile time error. My Apex Class API version is 40. – JavaAster Oct 3 '17 at 17:14

I suspect the problem is that the map coming back from Utils.getRecordTypeBySObjectType('Asset') doesn't contain the key 'Auto'.

You should first check if the map contains the 'Auto' key before trying to get the corresponding Id.

Also, as @JRiffe answered, check that the actual transaction originated from a v38.0+ API version. In theory it is enough that the Utils class is on v40.0, but it would pay to back track through the preceding class versions as well.

| improve this answer | |

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.