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
Kevin RichardsKevin Richards 

Illegal assignment from List<SObject> to List<System.Cases> at line 4 column 18

Hello Salesforce,

I'm working through the Trailhead modules and have encountered a problem with completing the "Introduction to Custom Controllers" module.
My solution:

public class NewCaseListController{

public List<Cases> getCases() {
     List<Cases> results = Database.query(        
    'SELECT Id, CaseNumber FROM Cases WHERE Status=New');
      return results;    
                  }
}
I get the error "Illegal assignment from List<SObject> to List<System.Cases> at line 4 column 18" and can't seem to resolve this issue.

Please assist.

Kevin
Nevlyn DSousa TicloNevlyn DSousa Ticlo
Hi Kevin,

The results of Database.query is List<sObject>. you could try typecasting the result as follows

List<Cases> results = (List<Cases>)Database.query(        
    'SELECT Id, CaseNumber FROM Cases WHERE Status=New');
 
Let me know if this works for you
Dilip_VDilip_V
Hi Kevin,

Please use below controller.
Public class NewCaseListController {
List<Case> results = new List<Case>();


private String sortOrder = 'CaseNumber';
public List<Case> getCase() {
    
    results = [SELECT Id, CaseNumber FROM Case WHERE Status = 'New'];

    return results;
    }
}

Make it as best answer if it helps.

Thanks,
Dilip.
Amit Chaudhary 8Amit Chaudhary 8
It should not be cases it should be case.

Try below code.
public class NewCaseListController{

	public List<Case> getCases() 
	{
		List<Case> results = [SELECT Id, CaseNumber FROM Case WHERE Status = 'New'];
       return results;    
	}
}
Let us know if this will help you
 
Kevin RichardsKevin Richards
Thank you. It works.
Asad WaliAsad Wali

Hey Developers, I want to search records in account and i face this error.
Illegal assignment from List<SObject> to List<Account>

I am facing same this error. Do anyone know how i can fix this issue.