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
ashish jadhav 9ashish jadhav 9 

How can I clear my understanding on SOQL? When to use sobject and when list<sobject>

I'm bit confused with this terms, when should I go for list<sobject> and when for sobject only? Can you please explain when to use list<list<set>>?
sslodhi87sslodhi87
Hi Ashish,

As per me you always should go for List<Sobject> that help us not to cause the List has no row for assigment exception. Below mentioned are scenario to handle both the cases
 
// this is the case when you need more than one record for process
List<Sobject> lstSobject = [SELECT Id, Name FROM Account LIMIT 1000];


//now this is the case when you need single record to perform some action or use in vf page

public Account objAccount {   get;set; }


List<Sobject> lstSobject = [SELECT Id, Name FROM Account WHERE Id =: accId LIMIT 1];

objAccount  = lstSobject.isEmpty() ? new Account() : lstSobject[0];
In this way you have handle the use case if the value accId is null or blank it is handled.

Please let me know if this help in your understanding.

Thanks
Prateek GargPrateek Garg
Ashish like if ur working with single record you can go with simply sobject.But when we are working with multiple records go for list<sobject>.but in salesforce we r working on multiple records so list is the best option.