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
JMThorntonJMThornton 

Casting object to object?

Hello everyone,

 

I'm trying to use the result of a query to query on another object:

 

 

 List<Opportunity> oppList = [Select AccountId From Opportunity where Id = :a.Opportunity__c];

 List<Account> accList = [Select Is_Trade_Account__c From Account Where Id in :oppList];
I'm getting an invalid bind expression error on the 2nd query as it is of type Account.

Is there some way around this? Casting? toString? This seems like it should be a pretty fundamental concept and I'm just missing something.
Thank you in advance!
-Jon

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Try this...

 

 

List<Opportunity> oppList = [Select AccountId From Opportunity where Id = :a.Opportunity__c];
Set<ID> acctIds = new Set<ID>(); 
for (Opportunity o : oppList)
{
    acctIds.add(o.AccountId);
}

List<Account> accList = [Select Is_Trade_Account__c From Account Where Id in :acctIds];

 

 

All Answers

forecast_is_cloudyforecast_is_cloudy

Try this...

 

 

List<Opportunity> oppList = [Select AccountId From Opportunity where Id = :a.Opportunity__c];
Set<ID> acctIds = new Set<ID>(); 
for (Opportunity o : oppList)
{
    acctIds.add(o.AccountId);
}

List<Account> accList = [Select Is_Trade_Account__c From Account Where Id in :acctIds];

 

 

This was selected as the best answer
JMThorntonJMThornton

This is the solution, thanks for the help!

-Jon