(Schema.SObjectType) || Method does not exist or incorrect signature: void

apexrecord-typeschemasobjectsobjecttype

I'm trying to list the Record Types that a user has access to. Unfortunately, this cannot be done through SOQL Query (IIRC) and the only choice I have would be to either use Schema.SObjectType (Apex) or Metadata API.

I have found a Source Code which adds this method:

// Returns a List of the Names of all RecordTypes
// available to the running user for a given SOBject type
public static List<String> GetAvailableRecordTypeNamesForSObject(
    Schema.SObjectType objType
) {
    List<String> names = new List<String>();
    List<RecordTypeInfo> infos = objType.getDescribe().getRecordTypeInfos();
    // If there are 2 or more RecordTypes...
    if (infos.size() > 1) {
        for (RecordTypeInfo i : infos) {
            if (i.isAvailable() 
                // Ignore the Master Record Type, whose Id always ends with 'AAA'.
                // We check the Id because Name can change depending on the user's language.
                && !String.valueOf(i.getRecordTypeId()).endsWith('AAA'))
                names.add(i.getName());
        }
    } 
    // Otherwise there's just the Master record type,
    // so add it in, since it MUST always be available
    else names.add(infos[0].getName());
    return names;
}

And can be run through Anonymous Apex using this block:

List <String> availableRecordTypes = GetAvailableRecordTypeNamesForSObject(Case.SObjectType);
System.debug(availableRecordTypes);

However, I'm getting this error.

Method does not exist or incorrect signature: void GetAvailableRecordTypeNamesForSObject(Schema.SObjectType) from the type anon

Can you help me correct the Source Code above? Thank you.

Source: here (Finding if which users have a record type available to them using SOQL)

Best Answer

You have used a static method. So it must added inside a Class like this.

public without sharing class SystemUtils {


    public static List<String> GetAvailableRecordTypeNamesForSObject(
            Schema.SObjectType objType
    ) {
        List<String> names = new List<String>();
        List<RecordTypeInfo> infos = objType.getDescribe().getRecordTypeInfos();
        // If there are 2 or more RecordTypes...
        if (infos.size() > 1) {
            for (RecordTypeInfo i : infos) {
                if (i.isAvailable()
                        // Ignore the Master Record Type, whose Id always ends with 'AAA'.
                        // We check the Id because Name can change depending on the user's language.
                        && !String.valueOf(i.getRecordTypeId()).endsWith('AAA'))
                    names.add(i.getName());
            }
        }
        // Otherwise there's just the Master record type,
        // so add it in, since it MUST always be available
        else names.add(infos[0].getName());
        return names;
    }

}

You need class.staticmethod name to call a static method, if it is being called outside that class.

SystemUtils.GetAvailableRecordTypeNamesForSObject(Case.SObjectType);

If both method and caller has been written in Anonymous apex, only then calling it without a class like this GetAvailableRecordTypeNamesForSObject(Case.SObjectType); would work.

Related Topic