You need to sign in to do that
Don't have an account?
How can I select the AccountId from CampaignMember (Account List Controller)
I am looking to get a list of accounts that do not have any contacts who are members of a specific campaign.
Here is the code I have, but I cannot figure the way to get the list.
Here is the code I have, but I cannot figure the way to get the list.
public class AccountListController6 { private String sortOrder = 'Name'; public List<Account> getAccounts() { String strEt6 = '701220000001Q1GAAU'; List<Account> results = Database.query( 'SELECT Id,Name ' + 'FROM Account ' + 'WHERE ID ' + 'NOT IN ' + '(SELECT Contact.AccountId ' + 'FROM CampaignMember ' + 'WHERE CampaignId = :strEt6) ' + 'ORDER BY ' + sortOrder + ' ASC ' + 'LIMIT 10' ); return results; } }
Try this:
"The inner select field 'Contact.AccountId' cannot have more than one level of relationships"
Also, I'm not understanding why you moved the CampaignId outside of the subquery as I am only looking for accounts not represented in that specific campaign.
If I now understand you correctly you are looking for accounts that arent in that campaign you specified. If So try this out.
I broke your query into two seperate queries to make it more clear and avoid the previous error.
Hope this helps!
AM
Variable does not exist: accountIdsBelongingToSpecifiedCampaign
I checked the code you wrote and found issues with the syntax as well as the logic. When you query the CampaignMember object in SFDC, you will never be able to get a reference to the Account associated with the Contact by using Contact.AccountId; and it will be confusing because you will not get a Syntax error either.
You can try executing this code snipper in the anonymous code block and you will understand what I am trying to point out: What will be needed here is the following:
- Query the CampaignMember for the Campign ID to get the associated Contacts.
- Use the Contact list to query their associated Accounts.
- Use the Account list as the filter in the final query.
I have added the code snippet which will fetch the desired result for you with the corrected syntax: You can make the needed changes in terms of the fields you would like to fetch or additional logic you would need to add to the filters.I hope this helps you; please do let me know how it turns out. If you feel that your question was answered then do flag the appropriate answer as the solution to your query.
Kind Regards,
Swarna.