简体   繁体   English

SOQL - 如何根据 ParentId 和基于名称的 ContactId 检索 CaseNumber?

[英]SOQL - How to retrieve CaseNumber based on the ParentId and Name based ContactId?

I'm extracting the " Id ", " CaseNumber ", " ContactId " & " ParentId " variables from " Case " object:我正在从“ Case ”object 中提取“ Id ”、“ CaseNumber ”、“ ContactId ”和“ ParentId ”变量:

SELECT Id, CaseNumber, ContactId, ParentId FROM Case
Id ID CaseNumber案件编号 ContactId联系方式 ParentId父母身份
5004V000000000000A 5004V000000000000A 00000001 00000001 003300000000000000 003300000000000000 #N/A #不适用
5004V000000000000B 5004V000000000000B 00000002 00000002 003300000000000000 003300000000000000 5004V000000000000A 5004V000000000000A

But instead of display the ID string of the " ParentId ", I need to display the CaseNumber whose the " Id " of corresponding " ParentId " belongs to.但是,我不需要显示“ ParentId ”的 ID 字符串,而是需要显示相应“ ParentId ” 的“ Id ” 所属的CaseNumber

Using the above example as a reference, by replacing the " ParentId " by the ParentCaseNumber the desired result would be:使用上面的示例作为参考,通过用ParentCaseNumber替换“ ParentId ”,所需的结果将是:

Id ID CaseNumber案件编号 ContactId联系方式 ParentCaseNumber
5004V000000000000A 5004V000000000000A 00000001 00000001 003300000000000000 003300000000000000 #N/A #不适用
5004V000000000000B 5004V000000000000B 00000002 00000002 003300000000000000 003300000000000000 00000001 00000001

Additionally, instead of displaying the " ContactId " I need to display the ContactName .此外,我需要显示ContactName而不是显示“ ContactId ”。 I know there is a " Name " variable inside the " User " object that matches the " ContactId " provided by " Case " object query.我知道在“ User ”object 中有一个“ Name ”变量与“ Case ”object 查询提供的“ ContactId ”相匹配。

" User " object query example: 用户”object查询示例:

SELECT Id, ContactId, Name FROM User`
Id ID ContactId联系方式 Name名称
005300000000000000 005300000000000000 003300000000000000 003300000000000000 John Smith约翰·史密斯

So I need to replace " ContactId " returned by the " Case " object query by the " Name " found on "User" object as above.因此,我需要将“ Case ”object 查询返回的“ ContactId ”替换为在“User”object 上找到的“ Name ”,如上所述。 The desired query output result would be:所需的查询 output 结果将是:

Id ID CaseNumber案件编号 ContactName ParentCaseNumber父案例编号
5004V000000000000A 5004V000000000000A 00000001 00000001 John Smith约翰·史密斯 #N/A #不适用
5004V000000000000B 5004V000000000000B 00000002 00000002 John Smith约翰·史密斯 00000001 00000001

Anyone could help me to build a SOQL query to accomplish such result?任何人都可以帮助我构建 SOQL 查询来实现这样的结果吗?

SELECT Id, CaseNumber, Contact.Name, Parent.CaseNumber
FROM Case

You need to read up a bit about relationship queries which is Salesforce's way of doing joins between tables.您需要阅读一些有关关系查询的内容,这是 Salesforce 在表之间进行连接的方式。

You can go "up" 5 times, have 5 dots ( SELECT Account.Owner.Profile.Name FROM Case ).您可以 go “up” 5 次,有 5 个点( SELECT Account.Owner.Profile.Name FROM Case )。 And you can go "down" the related lists too ( SELECT Id, (SELECT Email FROM Contacts) FROM Account你也可以 go “向下”相关列表 ( SELECT Id, (SELECT Email FROM Contacts) FROM Account

https://trailhead.salesforce.com/content/learn/modules/soql-for-admins/create-relationship-queries-with-standard-objects and https://trailhead.salesforce.com/content/learn/modules/apex_database/apex_database_soql may help. https://trailhead.salesforce.com/content/learn/modules/soql-for-admins/create-relationship-queries-with-standard-objectshttps://trailhead.salesforce.com/content/learn/modules/ apex_database/apex_database_soql可能有帮助。

Try the following query and let us know how it turns out for you.尝试以下查询,让我们知道您的结果如何。 We have tested this in an org and it works without complication.我们已经在一个组织中对此进行了测试,并且它可以正常工作。 Have a blessed one.拥有一个幸福的人。

SELECT Id, CaseNumber, Contact.Name, Parent.CaseNumber FROM Case

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM