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
Murali KrishnaMurali Krishna 

Regarding how to Query records from 4 Standars objects

how we Query records from Account,contacts,opportunity and campaign?

Best Answer chosen by Admin (Salesforce Developers) 
sandeep@Salesforcesandeep@Salesforce

There is not relation between all four object but only three objects are having relationship like Account is parent of Contact and Opportunity so SOQL can be 

 

[Select id , (Select id from Opportunities) , (Select id from contacts) from Account  ]  but Campaign is not related to these in any way but we can set up relation ship creating custom lookup field but that is dependent on requirement.

All Answers

sandeep@Salesforcesandeep@Salesforce

It is very simple using SOQL (Salesfoce Object query Language)

 

List<Account> ListofAccount = new List<Account>()
Listof Optty = [select id, Name from Account ] ;

 

List<contact> ListofContacts = new List<contact>()
Listof Optty = [select id, Name from contact ] ;

 

List<Opportunity> ListofOptty = new List<Opportunity>()
Listof Optty = [select id, Name from Opportunity ] ;

 

List<Campaign> ListofCampaigns = new List<Campaign>()
Listof Optty = [select id, Name from Campaign ] ;

Murali KrishnaMurali Krishna
hi sandeep,
forgot to mention, is it possible to do in single SOQL statement
sandeep@Salesforcesandeep@Salesforce

There is not relation between all four object but only three objects are having relationship like Account is parent of Contact and Opportunity so SOQL can be 

 

[Select id , (Select id from Opportunities) , (Select id from contacts) from Account  ]  but Campaign is not related to these in any way but we can set up relation ship creating custom lookup field but that is dependent on requirement.

This was selected as the best answer
Murali KrishnaMurali Krishna

thank you sandeep