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
hamshuhamshu 

Illegal assignment from LIST<SObject> to LIST<String>

Hi ,


    Question:is it possiable to store LIST<SObject> to LIST<String>.

    bec i am getting following error...

    Illegal assignment from LIST<SObject> to LIST<String>.

 

    Why i am tring to pass LIST<SObject> to LIST<String> bec i have to pass to  equalsIgnoreCase(Its takeing only string )

 

   following is my code

 

any help?

 

public static  List<string> searchMovie(string searchTerm)
        {
            List<string> distinctLastnames  = Database.query('Select   Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3');
            for(string lastname: searchterm){
            Boolean found = false;
            for(Integer i=0; i< distinctLastnames.size(); i++){
            
            if(lastname.equalsIgnoreCase(distinctLastnames[i]))
            { //Check if current lastname has been added yet
            found=true;
            break;
            }
             }
             if(!found)
            distinctLastnames.add(lastname);
             }
                return distinctLastnames;
        }

 

Thanks,

jaba

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

I fear you need to work on the basics a lil bit.

The code has lots of problem in it

 

public static List < string > searchMovie(string searchTerm) {
/*    
List < String > distinctLastnames = Database.query('Select Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3');
*/
//SOQL always returns list of sObject, So if you are doing querying on Apartments__c is will return list of Apartments__c and not strings
 List < Apartments__c > apartments = Database.query('Select   Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3');

/* for (string lastname: searchterm) { Boolean found = false; for (Integer i = 0; i < distinctLastnames.size(); i++) { if (lastname.equalsIgnoreCase(distinctLastnames[i])) { //Check if current lastname has been added yet found = true; break; } } if (!found) distinctLastnames.add(lastname); }
*/
//You can only iterate over iterables like map,list,set or custom iterators, the above code wont work because you are iterating over string "searchTerm"
return distinctLastnames; }

 Well I guess you need the distinct project names the code should be something like

 

public static Set < string > searchMovie(string searchTerm) {
    List < Apartments__c > Apartments = Database.query('Select   Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3');
    Set < String > distinctLastnames = new Set < String > ();
    for (Apartments__c apt: Apartments) {
        distinctLastnames.add(apt.Project_Name__c);
    }
    return distinctLastnames;
}

 

All Answers

souvik9086souvik9086

Hi,

 

Do like this

 

String soqlQry = 'Select Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3';

List<Apartments__c> apartmentObjList = Database.query(soqlQry);

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

hamshuhamshu

hi Dutta,

 

Thanks for your reply..

  but i am getting following error when i pass the apartmentObjList list to  equalsIgnoreCase

 

Method does not exist or incorrect signature: [String].equalsIgnoreCase(SOBJECT:Apartments__c)..

 

Avidev9Avidev9

I fear you need to work on the basics a lil bit.

The code has lots of problem in it

 

public static List < string > searchMovie(string searchTerm) {
/*    
List < String > distinctLastnames = Database.query('Select Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3');
*/
//SOQL always returns list of sObject, So if you are doing querying on Apartments__c is will return list of Apartments__c and not strings
 List < Apartments__c > apartments = Database.query('Select   Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3');

/* for (string lastname: searchterm) { Boolean found = false; for (Integer i = 0; i < distinctLastnames.size(); i++) { if (lastname.equalsIgnoreCase(distinctLastnames[i])) { //Check if current lastname has been added yet found = true; break; } } if (!found) distinctLastnames.add(lastname); }
*/
//You can only iterate over iterables like map,list,set or custom iterators, the above code wont work because you are iterating over string "searchTerm"
return distinctLastnames; }

 Well I guess you need the distinct project names the code should be something like

 

public static Set < string > searchMovie(string searchTerm) {
    List < Apartments__c > Apartments = Database.query('Select   Project_Name__c from Apartments__c where Project_Name__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\' ORDER BY Project_Name__c LIMIT 3');
    Set < String > distinctLastnames = new Set < String > ();
    for (Apartments__c apt: Apartments) {
        distinctLastnames.add(apt.Project_Name__c);
    }
    return distinctLastnames;
}

 

This was selected as the best answer
hamshuhamshu

Thank you Avi...Will Work On BAsic..