Skip to main content
Alternative option to use `Type.forName`, as suggested by sfdcfox.
Source Link
Daniel Ballinger
  • 103k
  • 40
  • 275
  • 601

I suspect part of the problem is the repeated calls to Schema.getGlobalDescribe().

That method returns metadata for all the accessible sObjects and is generally expensive if you are only interested in one particular sObject.

So, instead of doing this:

Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
Schema.DescribeSObjectResult describe = targetType.getDescribe();

Try something simpler like:

Schema.DescribeSObjectResult describe = Schema.describeSObjects(new String[]{typeName})[0];

If you want to take it a step further, have a read of Why is Schema.describeSObjects(types) Slower Than Schema.getGlobalDescribe()?. As sfdcfox suggested, you will get even better results using Type.forName. E.g.

Schema.DescribeSObjectResult describe = ((SObject)(Type.forName('Schema.'+typeName).newInstance())).getSObjectType().getDescribe();

Also, break your test methods into four distinct tests. One for each method. E.g.

@isTest
public class FieldSetControllerTest {
    public static String typeName = 'Contact';
    public static String fsName = 'lightningFormContactFields';

    @isTest
    public static void contactsHaveAFieldSetDefined(){
        boolean contactsHaveAFieldSet = FieldSetController.hasFieldSets(typeName);
        System.assert(contactsHaveAFieldSet);
    }

    // Plus more tests. Each focusing on one method at a time and making assertions.
}

I suspect part of the problem is the repeated calls to Schema.getGlobalDescribe().

That method returns metadata for all the accessible sObjects and is generally expensive if you are only interested in one particular sObject.

So, instead of doing this:

Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
Schema.DescribeSObjectResult describe = targetType.getDescribe();

Try something simpler like:

Schema.DescribeSObjectResult describe = Schema.describeSObjects(new String[]{typeName})[0];

Also, break your test methods into four distinct tests. One for each method. E.g.

@isTest
public class FieldSetControllerTest {
    public static String typeName = 'Contact';
    public static String fsName = 'lightningFormContactFields';

    @isTest
    public static void contactsHaveAFieldSetDefined(){
        boolean contactsHaveAFieldSet = FieldSetController.hasFieldSets(typeName);
        System.assert(contactsHaveAFieldSet);
    }

    // Plus more tests. Each focusing on one method at a time and making assertions.
}

I suspect part of the problem is the repeated calls to Schema.getGlobalDescribe().

That method returns metadata for all the accessible sObjects and is generally expensive if you are only interested in one particular sObject.

So, instead of doing this:

Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
Schema.DescribeSObjectResult describe = targetType.getDescribe();

Try something simpler like:

Schema.DescribeSObjectResult describe = Schema.describeSObjects(new String[]{typeName})[0];

If you want to take it a step further, have a read of Why is Schema.describeSObjects(types) Slower Than Schema.getGlobalDescribe()?. As sfdcfox suggested, you will get even better results using Type.forName. E.g.

Schema.DescribeSObjectResult describe = ((SObject)(Type.forName('Schema.'+typeName).newInstance())).getSObjectType().getDescribe();

Also, break your test methods into four distinct tests. One for each method. E.g.

@isTest
public class FieldSetControllerTest {
    public static String typeName = 'Contact';
    public static String fsName = 'lightningFormContactFields';

    @isTest
    public static void contactsHaveAFieldSetDefined(){
        boolean contactsHaveAFieldSet = FieldSetController.hasFieldSets(typeName);
        System.assert(contactsHaveAFieldSet);
    }

    // Plus more tests. Each focusing on one method at a time and making assertions.
}
Source Link
Daniel Ballinger
  • 103k
  • 40
  • 275
  • 601

I suspect part of the problem is the repeated calls to Schema.getGlobalDescribe().

That method returns metadata for all the accessible sObjects and is generally expensive if you are only interested in one particular sObject.

So, instead of doing this:

Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
Schema.DescribeSObjectResult describe = targetType.getDescribe();

Try something simpler like:

Schema.DescribeSObjectResult describe = Schema.describeSObjects(new String[]{typeName})[0];

Also, break your test methods into four distinct tests. One for each method. E.g.

@isTest
public class FieldSetControllerTest {
    public static String typeName = 'Contact';
    public static String fsName = 'lightningFormContactFields';

    @isTest
    public static void contactsHaveAFieldSetDefined(){
        boolean contactsHaveAFieldSet = FieldSetController.hasFieldSets(typeName);
        System.assert(contactsHaveAFieldSet);
    }

    // Plus more tests. Each focusing on one method at a time and making assertions.
}