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
Marc C.Marc C. 

Mixed-Object SOQL

It is possible to select fields from different objects in one SOQL query (like a JOIN). Example:

 

SELECT Id, Name, CloseDate, Account.Name, SAPID__c, StageName  FROM Opportunity LIMIT 10

 

But how does one get these results in apex code? I have not found out how to declare the result set:

 

List<whichType> = [SELECT Id, Name, CloseDate, Account.Name, SAPID__c, StageName  FROM Opportunity LIMIT 10];

sfdcfoxsfdcfox

The list's base type will be the object used in from clause:

 

Opportunity[ ] opps = [SELECT Id, Account.Name, StageName FROM Opportunity];

Accessing the related SObject is achieved through the same reference as in the query:

 

Account a = opps[0].Account;

You can also just directly pass through the reference:

 

if( opps[0].account.name == 'Test' )

 

N.V.V.L.Vinay KumarN.V.V.L.Vinay Kumar

SOQL - Salesforce Object Query Language

means it can be used only for Querying from one sObject only

 

IF there are relations we ca use Nested quert for getting the records of Child Objects

 

 

For querying from different objects we should use SOSL 

and the result set will be

 

List<sObject>