使用对象关系从另一个 table 访问一个字段
Access a field from another table using the object relation
我是 SalesForce 和 SOQL 的新手,如果问题已经得到解答,请提前致歉 link 对我来说。
我的 SOQL 查询的目的是获取所有合同信息以生成 PDF。
有tables:合同、联系人和账户
合约table中有字段: Maitre_d_apprentissage__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Apprenti__c, ApprentiNom__c,ApprentiPrenom__c
有关系:
- Apprenti__r 其中 link Apprenti__c 联系 table
- Maitre_d_apprentissage__r 其中 link Maitre_d_apprentissage__c 至 联系方式table
当我查看table时,我看到MaitreApprentissageNom1__c等于Maitre_d_apprentissage__r.LastName 和 ApprentiNom__c 等于 Apprenti__r.LastName。所以我得出结论,我可以从 ContactTable遵循同样的原则。所以我在我的查询中添加了 Apprenti__r.Date_de_naissance__c 和 Maitre_d_apprentissage__r.Date_de_naissance__c 来获取我的 Date_de_naissance__c 字段联系 table.
我在结果中看到查询成功获取了信息,但某些值已更改列(第 6 和 7 行),您可以看到查询 1 和查询 2 之间的区别。在第一个查询中我只 return Apprenti__r.Date_de_naissance__c 并且在第二个查询中我 return Apprenti__r.Date_de_naissance__c 和 Maitre_d_apprentissage__r.Date_de_naissance__c
查询 1:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c
FROM Contract
结果 1:
查询 2:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract
结果二:
我想了解问题出在哪里以及如何纠正它。提前谢谢你。
可能只是您的查询编辑器显示内容不正确。您可以看到它与联系人 table 的 2 次查找混淆了,为什么还有一列 header“Contact.Date_de_naissance__c”(以及为什么它出现两次)。而且它们没有按照您要求的顺序显示...
您使用的是什么编辑器?您可以尝试 built-in“开发者控制台”或 http://workbench.developerforce.com/
你需要它做什么?在 Apex 中,字段的顺序无关紧要,在 REST API 查询中,通过查找获取的值将以 JSON sub-objects 的形式出现,因此总会有一种方法可以准确地找出哪个值是来自哪个关系。
在 Dev Console 中尝试 运行 这个并检查它是否解决了你的恐惧:
System.debug(JSON.serializePretty([SELECT
ApprentiNom__c, ApprentiPrenom__c,
Apprenti__r.Date_de_naissance__c,
MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c,
Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract]));
然后添加Maitre_d_apprentissage__r.LastName
查询,看看有什么变化,什么保持原样。
我是 SalesForce 和 SOQL 的新手,如果问题已经得到解答,请提前致歉 link 对我来说。
我的 SOQL 查询的目的是获取所有合同信息以生成 PDF。
有tables:合同、联系人和账户
合约table中有字段: Maitre_d_apprentissage__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Apprenti__c, ApprentiNom__c,ApprentiPrenom__c
有关系:
- Apprenti__r 其中 link Apprenti__c 联系 table
- Maitre_d_apprentissage__r 其中 link Maitre_d_apprentissage__c 至 联系方式table
当我查看table时,我看到MaitreApprentissageNom1__c等于Maitre_d_apprentissage__r.LastName 和 ApprentiNom__c 等于 Apprenti__r.LastName。所以我得出结论,我可以从 ContactTable遵循同样的原则。所以我在我的查询中添加了 Apprenti__r.Date_de_naissance__c 和 Maitre_d_apprentissage__r.Date_de_naissance__c 来获取我的 Date_de_naissance__c 字段联系 table.
我在结果中看到查询成功获取了信息,但某些值已更改列(第 6 和 7 行),您可以看到查询 1 和查询 2 之间的区别。在第一个查询中我只 return Apprenti__r.Date_de_naissance__c 并且在第二个查询中我 return Apprenti__r.Date_de_naissance__c 和 Maitre_d_apprentissage__r.Date_de_naissance__c
查询 1:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c
FROM Contract
结果 1:
查询 2:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract
结果二:
我想了解问题出在哪里以及如何纠正它。提前谢谢你。
可能只是您的查询编辑器显示内容不正确。您可以看到它与联系人 table 的 2 次查找混淆了,为什么还有一列 header“Contact.Date_de_naissance__c”(以及为什么它出现两次)。而且它们没有按照您要求的顺序显示...
您使用的是什么编辑器?您可以尝试 built-in“开发者控制台”或 http://workbench.developerforce.com/
你需要它做什么?在 Apex 中,字段的顺序无关紧要,在 REST API 查询中,通过查找获取的值将以 JSON sub-objects 的形式出现,因此总会有一种方法可以准确地找出哪个值是来自哪个关系。
在 Dev Console 中尝试 运行 这个并检查它是否解决了你的恐惧:
System.debug(JSON.serializePretty([SELECT
ApprentiNom__c, ApprentiPrenom__c,
Apprenti__r.Date_de_naissance__c,
MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c,
Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract]));
然后添加Maitre_d_apprentissage__r.LastName
查询,看看有什么变化,什么保持原样。