1

For one of the requirements, I need to query and list down Apex class those are only @AuraEnabled in nature. Is there any Metadata API is available for this?

I believe this is not going to help me...

ApexClass classNameList = [SELECT Body FROM ApexClass]

1 Answer 1

7

@AuraEnabled annotation is not available for a class, but the only available for static methods. But if you want to obtain a list of Apex Classes with @AuraEnabled methods you can evolve your approach this way:

List<ApexClass> apexClasses = [
        SELECT Name, Body
        FROM ApexClass
        /* WHERE Status = 'Active' */
];
List<String> classesNamesWithAuraEnabledMethods = new List<String>();
for (ApexClass apexClass: apexClasses) {
    if (apexClass.Body.contains('@AuraEnabled')) {
        classesNamesWithAuraEnabledMethods.add(apexClass.Name);
    }
}
// desired list of classes with @AuraEnabled methods
System.debug(classesNamesWithAuraEnabledMethods); 

This is a basic example that doesn't cover all the scenarios (for instance if your @AuraEnabled method is commented) but you can write your own filter rules dealing only with apex class body string representation.

UPDATE: You can also use SOSL query to find all @AuraEnabled classes just like in @sfdxfox answer:

List<ApexClass> classesWithAuraEnabledMethods = [
        FIND '@IsTest' 
        IN ALL FIELDS 
        RETURNING ApexClass(Id, Name)
][0];

But using that approach you should also keep in mind that according to the Apex Governor Limits the max total number of records retrieved by a single SOSL query is 2000.

1
  • 2
    FYI, SOSL can find those classes without querying all classes to find the values; see this answer where i suggest that to find test classes.
    – sfdcfox
    Aug 4, 2020 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .