how to combine the two soql queries as asingle query?

 Contact con = [Select Id,Name,AccountId From Contact Where ContactId In (Select ContactId From User Where Id=:UserInfo.getUserId()];
 List<Opportunity> opp =[select Name,Amount from Opportunity where id=:con.AccountId];


This is my apex class :::

 Public class OpportunityDetails{

   Opportunity c = new Opportunity (); 
   public Opportunity getC(){ 
   return c;

    public ApexPages.StandardSetController setCon {
        get {

            if(setCon == null) {
                setCon = new ApexPages.StandardSetController

               Here I'm going to place those two queries as a single soql query

               // (Database.getQueryLocator([select name,amount from opportunity where AccountId In (select accountId from Contact where Id =: UserInfo.getUserId())]));

            return setCon;

   public List<Opportunity> getOpportunities() {

        return (List<Opportunity>) setCon.getRecords();
  • consider accepting the below answer, I believe it answers what you were attempting to do and it will help point others in the right direction should they have a similar question. Thanks – glls Nov 3 '17 at 14:48

These two query are on different object and Salesforce doesn't support query multiple object in single SOQL. So you can't do that.

If you want to search something you can use SOSL. It allows us to select multiple objects.

  • thanks for giving the reply and i'm trying to access salesforce opportunities in customer portal but customer portal licences are not having the opportunities access so is there anyway to access the opportunities. – sekhar Oct 12 '17 at 9:57
  • @sekhar use without sharing in class to query data and wrapper to display it on UI. – Tushar Sharma Oct 12 '17 at 9:58
  • I'm manually sharing the account object and giving the opportunities access for opportunities and added the portal users for accessing but in portal still it shows insufficient previligers.any idea on this – sekhar Oct 12 '17 at 9:59
  • @sekhar I don't think you can share opportunity with portal user. There license doesn't support it. – Tushar Sharma Oct 12 '17 at 10:01
  • Sharma is there any other way to access the opportunities? – sekhar Oct 12 '17 at 10:05

Unfortunately for this particular example, you have to do the subquery on User which can't easily be put inside one query, but you could query on the parent object and use child joins to get Contacts and Opportunities in general:

User usr = [select ContactId, AccountId from User where Id = :UserInfo.getUserId()];
List<Account> accns = [select (select Id from Contacts where Id = :usr.ContactId), 
                              (select ID from Opportunities)
                      from Account 
                      where Id = :usr.AccountId];
Contact myContact = accns[0].Contacts[0];
List<Opportunity> opps = accns[0].Opportunities;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.