function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nida Khan 5Nida Khan 5 

How to make this method of Apex, Aura Enabled to use it in lightning component? Urgent!

Hi All,

I want to use this method in lightining, and thus want to make it aura enabled. How can I do it?

public static List<SelectOption> getAccounts() {
        List<SelectOption> accountOptions = new List<SelectOption>(); 
        String rtName = ldObj.RecordType.Name;
        accountOptions.add( new SelectOption('NULL','Create New Account :'+ldObj.Company) );
        //String query = ' SELECT Id, Name From Account Where recordType.Name =:rtName and name LIKE '+ '\'%'+ldObj.Company+'%\''+ 'limit 1000';
        String query = ' SELECT Id, Name From Account Where name LIKE '+ '\'%'+ldObj.Company+'%\''+ 'limit 1000';
        for(Account acc : database.query(query) ) {
            accountOptions.add( new SelectOption(acc.Id, 'Attach To Existing:'+ acc.Name) );    
        }
        return accountOptions;      
    }

Please give your suggestions for the same.
Mike Floyd 21Mike Floyd 21
You can use the "@AuraEnabled" annotation, like so:
 
@AuraEnabled
public static List<SelectOption> getAccounts() {
        List<SelectOption> accountOptions = new List<SelectOption>(); 
        String rtName = ldObj.RecordType.Name;
        accountOptions.add( new SelectOption('NULL','Create New Account :'+ldObj.Company) );
        //String query = ' SELECT Id, Name From Account Where recordType.Name =:rtName and name LIKE '+ '\'%'+ldObj.Company+'%\''+ 'limit 1000';
        String query = ' SELECT Id, Name From Account Where name LIKE '+ '\'%'+ldObj.Company+'%\''+ 'limit 1000';
        for(Account acc : database.query(query) ) {
            accountOptions.add( new SelectOption(acc.Id, 'Attach To Existing:'+ acc.Name) );    
        }
        return accountOptions;      
    }


 
Nida Khan 5Nida Khan 5
Hi, @auraenabled annotation is not supported in methods such as selectoption and of pagereference type. So , how we can use them
Mike Floyd 21Mike Floyd 21
Sorry Nida, didn't see that it was a method returning SelectOption objects. Like you said, doesn't look like it is an available annotation for that object type. Searching around, this is the only relevant thing I found:

http://salesforce.stackexchange.com/questions/53596/auraenabled-support-for-apex-class-return-types
Yousuf Mohammad 6Yousuf Mohammad 6
The workaround for this is to searialize the return value and JSON.parse in Client Controller.