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
Newbie10Newbie10 

List exception

Must be something very silly.But i get an error like this

14:17:59:040 FATAL_ERROR System.QueryException: List has more than 1 row for assignment to SObject

 

for code below

 

List<Contact> mylist = new list<Contact> {[select id from Contact ]};

 

Anyone can spot anything?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You're trying to squeeze a list into a single array item. Simply use:

Contact[] myList = [SELECT Id FROM Contact];

Use the syntax you are trying to use when a single object is used:

Contact[] myList = new Contact[] { new Contact() };

All Answers

sfdcfoxsfdcfox
You're trying to squeeze a list into a single array item. Simply use:

Contact[] myList = [SELECT Id FROM Contact];

Use the syntax you are trying to use when a single object is used:

Contact[] myList = new Contact[] { new Contact() };
This was selected as the best answer
Thiyagarajan.SelvarajThiyagarajan.Selvaraj

Try this : 

 

List<Contact> mylist = new list<Contact> ([select id from Contact ]);

 

Thanks

Newbie10Newbie10

Thanks Both.It works