SOQL 连接查询返回 sObject 但不返回字段。获得的ID如何使用?
我有下面的 SOQL,我得到的结果包含 sObject 的 ID。 我的假设是查询也会返回 SObject 的字段。 例如,我的查询尝试获取“startDay__c
”(日期),它类似于 ShigotoShousai 对象的字段。但查询的结果只是sObject实例的ID。
(父级:ShigotoShousai
子级:ShigotoAssign
)
sObject[] result = [
SELECT
ShigotoShousai__r.id,
ShigotoShousai__r.startDay__c
FROM ShigotoAssign__c
];
system.debug(结果)输出
shigotoAssign_c:{Id=a06500000067aNjAAI, ShigotoShousai_c=a055000000DlHnOAAV}, shigotoAssign_c:{Id=a06500000067aNoAAI, ShigotoShousai_c=a055000000DlHnTAAV})
我得到了 ShigotoShousai__c sObject 的 ID 而不是它的属性“startDay__c
”。 我认为输出会是这样的:
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnOAAV, startDay__c=2010-10-10},
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnTAAV, startDay__c=2010-10-13})
But query result just returned me ID of ShigotoShousai__c sobject :(
现在我知道 ID 值为 ShigotoShousai__c
并且想要访问它的字段,所以我做了以下操作。
ShigotoShousai__c foo = (ShigotoShousai__c)result[0].get('ShigotoShousai__c');
//now I assume I can access to some fields like below
system.debug(foo.workDate);
这给出了我的错误:
System.TypeException: Invalid conversion from runtime
type Id to SOBJECT:shigotoShousai__c
然后我发现ID不能用来引用SObject(即ShigotoShousai__c)。
但是我有它的id..我如何访问,例如startDay__c
有没有办法使用这个ID? ?
I have SOQL below and I get the result that contains sObject's ID.
My assumption was the query will return the fields of SObject as well.
For instance my query try to get "startDay__c
" (The date) which is like field of ShigotoShousai sobject. But result of query is just ID of the sObject instance.
(parent: ShigotoShousai
child: ShigotoAssign
)
sObject[] result = [
SELECT
ShigotoShousai__r.id,
ShigotoShousai__r.startDay__c
FROM ShigotoAssign__c
];
system.debug(result) output
shigotoAssign_c:{Id=a06500000067aNjAAI, ShigotoShousai_c=a055000000DlHnOAAV}, shigotoAssign_c:{Id=a06500000067aNoAAI, ShigotoShousai_c=a055000000DlHnTAAV})
I got ID of ShigotoShousai__c sObject instead of its property "startDay__c
".
I thought output would be something like:
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnOAAV, startDay__c=2010-10-10},
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnTAAV, startDay__c=2010-10-13})
But query result just returned me ID of ShigotoShousai__c sobject :(
Now I know have ID value of ShigotoShousai__c
and want to access its field so I did following.
ShigotoShousai__c foo = (ShigotoShousai__c)result[0].get('ShigotoShousai__c');
//now I assume I can access to some fields like below
system.debug(foo.workDate);
And this gives me error:
System.TypeException: Invalid conversion from runtime
type Id to SOBJECT:shigotoShousai__c
Then I figured that ID cannot be used to refer to SObject (i.e. ShigotoShousai__c).
But I have its id.. How can I access, say startDay__c
? Is there a way to use this ID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您将 SOQL 查询结果分配给通用 Sobject[],除了 Id 之外,它没有自己的任何具体字段。只要您不想用动态 SOQL 做任何花哨的事情,就可以尝试这样的事情:
The problem is that you are assigning the SOQL query result to the generic Sobject[], which does not have any concrete fields of its own, except for Id. As long as you're not trying to do anything fancy with dynamic SOQL, try something like this:
也只是想分享我的信息,因为它与问题相关。希望有人觉得这有帮助。
下面的查询涉及父、子、孙,我使用 apex 来达到每个值。
现在我可以在 Visualforce 页面上显示这些值:)
Also just want to share my info since it is related to the question. Hopefully someone finds this helpful.
Below query involves parent, child, grandchild and I used apex to reach each values.
Now I can display those value on visualforce page :)