0

We are trying to avoid using globalDescribe as that can eat a ton of time in larger orgs. So we are trying Type.forName to get SObjectType and their describes without hitting the costly global describe.

This seems to be leading to failing tests. In this loop:

for(ChildRelationship child : Schema.SObjectType.My_Object__c.childRelationships){
    Schema.DescribeSObjectResult childDescribe = getSObjectDescribe(child.getChildSObject());
    if(childObjects.contains(childDescribe.getName())){
        customObjectList.put(child.relationshipName, childDescribe.getLabel());
    }
}

Then getSObjectDescribe:

public static Schema.SObjectType getSObjectType(String apiName){
    Schema.SObjectType sObjType;

    if(String.isNotBlank(apiName)){
        Type tempType = Type.forName(apiName);

        if(tempType != null){
            try{
                sObjType = ((SObject)tempType.newInstance()).getSObjectType();
            } catch(System.TypeException e){
                //Type of object is not an SObject, carry on
            }
        }
    }

    return sObjType;
}

The issue is getSObjectDescribe returns null when package is packaging and running tests (so childDescribe.getName() in the loop above dies because childDescribe is null), but otherwise functions correctly. In my scratch org AND in the packaging org if these failing tests are just ran, they pass without problem. It is just during packaging that a NullPointerException pops up as the Type.forName seems to fail.

Has anyone experienced this or might know what is causing this and what to do about it?

Your Answer

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

Browse other questions tagged or ask your own question.