2

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.

3 Answers 3

4

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.

2

The reason you are getting this error is here:

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

The list_accountShare is declared as List<AccountShare> and hence the ids used in this list of sObjects are lists of AccountShare ids. Yet, the SOQL is expecting a list of Account ids when you use a bind variable of list<Sobject>.

You'll need to take the output of the first SOQL and build up a list of AccountIds to use as the bind expression in the second SOQL

1

I tried this code in my execute anonymous block its working fine for me. Set<id> groupId = new Set<id>{'00540000001mA7bAAE','00540000001mA7bAAE'}; List<AccountShare> list_AccountShare = [Select AccountId from AccountShare where ((UserOrGroupId IN :groupID) OR (UserOrGroupId ='00540000001mV8iAAE')) AND (RowCause IN ('Rule','Owner'))]; System.debug(' AccountShare is '+ list_AccountShare); What I did is added the bracket for OR condition and AND condition. But I am not sure this is right or not but i got the output in anonymous block.

Thanks, IRaj.

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.