+ Start a Discussion
Dimple SinghDimple Singh 

What is return type of soql ?

sfdcMonkey.comsfdcMonkey.com
Hi Dimple, 
A SOQL query will always return a list of sobjects. If you are assigning a query to a single sobject Apex will execute your query then attempt to assign to this sobject (unfortunately, if there are no elements in the returned query or more than one you will get an exception).

Thanks let us know if it helps you and close your query with best answer so it will help others in future 
 
Ajay K DubediAjay K Dubedi
Hi Dimple,

Maps to: Describe the data return types for SOQL  queries and their impact on variable assignment.
SOQL return types
SOQL queries evaluate to the following return types:

an sObject singleton - 'Account acc = [SELECT Id FROM Account LIMIT 1]'
a list of sObjects - [SELECT Id FROM Account]'
an integer - 'Integer i = [SELECT COUNT() FROM Account]'


Further information can be found :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL.htm
https://developer.salesforce.com/docs/atlas.en-us.198.0.soql_sosl.meta/soql_sosl/

Hope this helps.

Thank You
Ajay Dubedi
Akshay_DhimanAkshay_Dhiman
Hi Dimple Singh,


A SOQL query will always return a list of sobjects. If you are assigning a query to a single sobject Apex will execute your query then attempt to assign to this sobject (unfortunately, if there are no elements in the returned query or more than the one you will get an exception).

List<Account> accountQuery = [SELECT Id, Name FROM Account WHERE Name = :search Limit 1];

if (accountQuery.size() == 1) { // execute code }

Please mark as best answer if it helps you.

Thank you.
Akshay
Shubham NandwanaShubham Nandwana
Hi Dimple,
SOQL statements evaluate to a list of sObjects, a single sObject, or an Integer for count method queries. Below are examples of it.
1.List of sObjects
List<Account> aa = [SELECT Id, Name FROM Account WHERE Name = 'Acme'];

2. You can also create new objects from SOQL queries on existing ones. The following example creates a new contact for the first account with the number of employees greater than 10:
Contact c = new Contact(Account = [SELECT Name FROM Account 
    WHERE NumberOfEmployees > 10 LIMIT 1]);
c.FirstName = 'James';
c.LastName = 'Yoyce';

 3. The count method can be used to return the number of rows returned by a query. The following example returns the total number of contacts with the last name of Weissman:
Integer i = [SELECT COUNT() FROM Contact WHERE LastName = 'Weissman'];

Hope it helps. Mark this as best solution if it does.

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
Vishal ArbuneVishal Arbune
SOQL query returns below data types:
1. Single sObject.
2. List of sobjects.
3. Integer.
Paul WorltonPaul Worlton
One return type that was not mentioned...

If you assign the results of a SQOL to a single object and SOQL finds no results, the return type is NULL.

If you aren't aware of this and you aren't doing a NULL check on the results, it can cause a "derefrencing a null value" exception.  It is best just to avoid the trouble and always return a list (with a size() or isEmpty() check).