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
Ryan NeelRyan Neel 

Illegal assignment from Account to List<Account>

Hi folks, I had the following code working (Pic1), but I wanted to change the SOSL query to SOQL (Pic2) so I could limit the fields the user was searching through.  After changing the query to SOQL however, I'm getting the error "Illegal assignment from Account to List<Account> (29:13)".

SOSL Query:
public with sharing class AccountSearchController {
    @AuraEnabled
    public static List<Account> searchAccounts( String searchTerm ) {
        List<Account> accounts = new List<Account>();
        if ( String.isNotBlank( searchTerm ) ) {
            List<List<sObject>> searchResults = [
                FIND :searchTerm
                RETURNING Account(
                    Id, Name, Phone, Website,
                    BillingStreet, BillingCity,
                    BillingState, BillingPostalCode
                   ORDER BY Name
                   LIMIT 10
              )
            ];
            accounts = searchResults[0];
        }
        return accounts;
    }
}
SOQL Query:
public with sharing class AccountSearchController {
    @AuraEnabled
    public static List<Account> searchAccounts( String searchTerm ) {
        List<Account> accounts = new List<Account>();
        if ( String.isNotBlank( searchTerm ) ) {
            List<List<sObject>> searchResults = [
                SELECT Id
                      ,Name
                      ,Phone
                      ,Website
                      ,BillingStreet
                      ,BillingCity
                      ,BillingState
                      ,BillingPostalCode
                FROM Account
                WHERE BillingState = :searchTerm OR
                      BillingPostalCode = :searchTerm
                LIMIT 50  
            ];
            accounts = searchResults[0];
        }
        return accounts;
    }
}

 
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/169760/illegal-assignment-from-list-to-list

Try the suggestions as mentioned in the above developer discussion.

It might help you.
Maharajan CMaharajan C
Hi Ryan,

The below code is fine. No need of searchResults property.
 
public with sharing class AccountSearchController {
    @AuraEnabled
    public static List<Account> searchAccounts( String searchTerm ) {
        List<Account> accounts = new List<Account>();
        if ( String.isNotBlank( searchTerm ) ) {
            accounts = [SELECT Id,Name,Phone,Website,BillingStreet,BillingCity,BillingState,BillingPostalCode
                        FROM Account WHERE BillingState = :searchTerm OR BillingPostalCode = :searchTerm
                        LIMIT 50 ];
        }
        return accounts;
    }
}

Thanks,
Maharajan.C
ravi soniravi soni
hi Ryan,
try following code.
public with sharing class AccountSearchController {
    @AuraEnabled
    public static List<Account> searchAccounts( String searchTerm ) {
        List<Account> accounts = new List<Account>();
        if ( String.isNotBlank( searchTerm ) ) {
            List<Account> searchResults = [SELECT Id,Name,Phone,Website,BillingStreet,BillingCity,BillingState,BillingPostalCode
                                           FROM Account WHERE BillingState = :searchTerm OR
                                           BillingPostalCode = :searchTerm LIMIT 50 ];
            accounts = searchResults;
        }
        return accounts;
    }
}

Note :  accounts = searchResults; your error is throwing from this line because you are assign single object into list<object> that is not good.
if you want to get first record of list then you should return single Account rather than list<Account>.

let me know if it helps you by marking it as best.
Thank You
baba morrisbaba morris
Light Of Illuminati For Wealth and Power +27672493579 in Gauteng, Limpopo, Free State, KwaZulu-Natal, Northern Cape, Eastern Cape, Mpumalanga, North West, United States, United Kingdom, Australia, Austria, Belgium, Australia, Malaysia, Kenya, England, German, Spain, Jamaica, Brazil, Germany, Denmark, Wales, France, Norway, Sweden, Sweden, Denmark, Poland, Canada, South Africa, South Sudan, Sudan, Switzerland, Swaziland, Armenia, Belgium, Botswana, Zambia, Mauritius, Mauritania, Bahamas, Malta, Netherlands, Nepal, New Zealand, Reunion Island, Croatia, Cyprus, Finland, Fiji, Greenland, Georgia, Guam, Guyana, Hungary, Iceland, Ireland, Italy, Luxembourg, Lithuania, Tuvalu, Uganda, Virgin Islands (U.S.), Virgin Islands (British), Vanuatu, Zimbabwe, Birmingham, Bristol, Manchester, Cardiff, Edinburgh, Glasgow, York, Belfast, Sheffield, City of London, Cambridge, Nottingham, Oxford, Newcastle, Leeds, Bath, Coventry, Portsmouth, Aberdeen, Leicester, Brighton, Plymouth, Southampton, Swansea, Plymouth, Kingston upon Hull, Wolverhampton, Derby, Stock City, Exeter, Norwich, Nottingham, Nottinghamshire, Westminster, Winchester, Worcester, Worcestershire, Inverness, Durham, Dundee, City of Westminster, Reading, Salisbury, Bradford, Newport, Brighton and Hove, Lincoln, Perth, Londonderry, Chichester, Chester, Greater Manchester, St Albans, Peterborough, Lisburn, Gloucester, Bristol.

Call/Whatsapp us on +27672493579 or Email: priestheldon@gmail.com For more information.
For those who are interested in making money, every good thing comes with money, comes with extra effort , All u need do is a “Spiritual work” and every wicked power delaying your progress wants clear and good things will come to you like, money, favor from people, open doors, business breakthrough, good job. Etc. For more info you can call +27672493579 or Email: priestheldon@gmail.com

Note: It’s not a child’s play, it’s for those who are desperate and ready to make a change in their life.User-added image