-1

I'm trying to retrieve the contact's account opportunities i gathered all opportunities but when i'm trying to retrieve all opportunities it printing empty.

this is method

@AuraEnabled
public static List<Opportunity> getAccountOpportunities(){

   List<String> oppss = new List<String>();
   Set<Id> accountIds = new Set<Id>();
   List<Contact> conts = [SELECT AccountId FROM Contact WHERE Email = 'test@gmail.com'];
   for(Contact c : conts){

      accountIds.add(c.AccountId);
   }

   List<Account>  accounts = [select name,(select Name from opportunities) from Account where id = :accountIds];   
    for(Account a : accounts){
        for(Opportunity c : a.opportunities){
            String oppname = c.name;
            oppss.add(oppname);
            System.debug(oppname);//printing the opportunities success          
        }

    }

    System.debug(oppss);//successfully getting all opps
    List<Opportunity>  opps = [select name from Opportunity where id = :oppss ];
    System.debug('All Opportunities'+opps);
    //returns empty shows All Opportunities()
    return opps;
} 
1

Problem is in below code

List<String> oppss = new List<String>();
for(Account a : accounts){
        for(Opportunity c : a.opportunities){
            String oppname = c.name;
            oppss.add(oppname);
            System.debug(oppname);//printing the opportunities success          
}

You are comparing oppss list of name with id in SOQL

List opps = [select name from Opportunity where id = :oppss ];

Either you have to compare with list of name or list of opportunity id.

solution 1:

Change to list id instead list of names .

 set<id> oppss = new set<id>();
    for(Account a : accounts)
            for(Opportunity c : a.opportunities)
                oppss.add(c.id);     

or

solution 2 :

Change in SOQL where id to Name

 List<Opportunity>  opps = [select name from Opportunity where name= :oppss ];

I Prefer go for solution 1 instead of solution 2, Solution 2 you may be result to get other opportunity because the name conflict also.

updated:

@AuraEnabled
public static List<Opportunity> getAccountOpportunities(){
       set<id> oppss = new set<id>();
       Set<Id> accountIds = new Set<Id>();
       List<Contact> conts = [SELECT AccountId FROM Contact WHERE Email = 'nithesh_k@persistent.com'];
       for(Contact c : conts)
          accountIds.add(c.AccountId);

        List<Account>  accounts = [select name,(select Name from opportunities) from Account where id = :accountIds];   

        for(Account a : accounts)
            for(Opportunity c : a.opportunities)
                oppss.add(c.id);    

           System.debug('oppss' + oppss);//successfully getting all opps

           List<Opportunity>  opps = [select name from Opportunity where id = :oppss ];
           System.debug('All Opportunities'+opps);
return opps;
}

Question 2 :

set<id> setOpp =new set<id>();
    for(opportunity o: [select accountID from opportunity ] )
        setOpp.add(o.accountid);

    List<Account>  accounts = [select name,(select Name from opportunities) from Account where id = :accountIds and id= :setOpp];   

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.