0

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)

0

1 Answer 1

0

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.

3
  • Hi @Nagendra Thank you very much! Invoking the Class name before the method worked for me! Appreciate the help! I have upvoted your answer, though since I'm new here, my vote seemed to have not displayed.
    – VanceSOQL
    Nov 15, 2021 at 8:23
  • @Yvan-NewbieDeveloper You can accept the answer instead. Nov 15, 2021 at 8:27
  • Hi @Nagendra Yup - did that now. Thanks again!
    – VanceSOQL
    Nov 15, 2021 at 9:05

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.