[SalesForce] Error: Compile Error: Invalid bind expression type of SOBJECT:AccountShare does not match domain of foreign key

I'm attempting to create a list with accounts from user's territory. SYSTEMS__c is custom object, Account__c is a lookup to accounts. Here what I have gone so far…

List<AccountShare> list_AccountShare = [Select  AccountId from AccountShare 
                                         where ( UserOrGroupId IN : map_group OR  UserOrGroupId =:userId )
                                                AND RowCause IN ('Territory', 'TerritoryManual', 'TerritoryRule')];

List<SYSTEMS__c> list_Partners = [SELECT Account__c,Number__c,LEGACY_CUST_NUM__c 
                                    FROM SYSTEMS__c where Account__c IN: list_AccountShare];

But I get this error..

Error: Compile Error: Invalid bind expression type of SOBJECT:AccountShare does not match domain of foreign key

I guess the IN clause syntax is incorrect. Any suggestions?
Thanks.

Best Answer

Inline lists can only bind to Id, not any other field. What you need is a subquery:

SELECT Account__c,Number__c,LEGACY_CUST_NUM__c 
FROM SYSTEMS__c where Account__c IN
(Select  AccountId from AccountShare 
where ( UserOrGroupId IN : map_group OR  UserOrGroupId =:userId )
AND RowCause IN ('Territory', 'TerritoryManual', 'TerritoryRule'))

If the sub-query is too complex, it may not let you, in which case you'll have to iterate through the list of shares you queried, and build the list manually.

Related Topic