Apex获取帐户记录ID,列表中的联系名称(使用和条件)

发布于 2025-02-10 00:21:50 字数 494 浏览 1 评论 0原文

在Salesforce中,我想检索其相关联系人名称必须在字符串列表中具有值的帐户。字符串列表为{'test','用户'}。我需要找到所有帐户,其中联系名称同时具有测试和用户为相关联系人,

我正在尝试下面尝试,但是下面的查询将显示一个帐户,其中甚至有1个值匹配为contact名称。

list< string>名称=新列表< string> {'test','用户'}; list< earcess>  =  nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;   nbsp; nbsp; nbsp; nbsp; nbsp; NBSP;&nbsp ;  (select select ofceptid  from nbsp; contacts  where firstName  like  like :names names;

请帮助

In Salesforce I would like to retrieve AccountIds where its related Contact FirstName must have values in list of String. List of string is {'Test','User'}. I would need to find all Accounts where Contact first Name has both Test and User as related Contacts,

I am trying as below but the below query will show accounts where even 1 value matches as Contact First Name.

List<String> names = new List<String>{'Test','User'}; List<Account> accountList =         [ Select Id from Account Where Id IN         (Select AccountId FROM Contacts where FirstName LIKE :names)];

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2025-02-17 00:21:50
SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName = 'Test')
AND Id IN (SELECT AccountId FROM Contact WHERE LastName = 'User')

但这只能工作2次,您不能像这样写第三个“”(请参阅​​在这里)。

如果您需要一个更通用的解决方案,该解决方案可以获取任何大小的列表,您必须在循环中运行“我的”查询,一个2个名称,请将结果保存到某些set&lt; id&gt;或MAP ,使用mySet.RetainAll(IDSFROMCURRENTLOOPQUERY) ...当然在循环中查询的功能也有点邪恶。

SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName = 'Test')
AND Id IN (SELECT AccountId FROM Contact WHERE LastName = 'User')

But it'll work only 2 times, you can't write 3rd "IN" like that (see here).

If you need a more generic solution that can take lists of any size you'd have to run "my" queries in loop, 2 names at a time, save results to some Set<Id> or Map, play with functions like myset.retainAll(idsFromCurrentLoopQuery)... Of course query in a loop is bit evil too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文