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
jgradw2jgradw2 

Apex SOQL Invalid Type problem

Hello and thank you for responding!

 

I am misunderstanding an invalid type error I'm  receiving. The most simple example is this:

Set<Account>Agencies = new Set<Account>();
Agencies.addAll([SELECT Account.ID, Account.Name FROM Contact WHERE ID = '']);

 The error for this assignment would be to the likes of: cannot assign type <List>Contact to type <Set>Account.

 

Why is the FROM clause object dictating the type of the query? And I need to understand why this does not work because I have other queries that work similarly as this one, and would otherwise require nested subqueries, which are not supported in SOQL. 

 

Thank you very much :)

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

your query returns contacts, so you can assign that to a set of accounts, you'll need to loop over the results and add the nested account object from the contact into your set.

All Answers

SuperfellSuperfell

your query returns contacts, so you can assign that to a set of accounts, you'll need to loop over the results and add the nested account object from the contact into your set.

This was selected as the best answer
jgradw2jgradw2

Thank you very much, that does get me the results I need :)