[英]SOQL join query returns sObject but not fields. How to use the obtained ID?
I have SOQL below and I get the result that contains sObject's ID. 我在下面有SOQL,我得到的结果包含sObject的ID。 My assumption was the query will return the fields of SObject as well.
我的假设是查询将返回SObject的字段。 For instance my query try to get "
startDay__c
" (The date) which is like field of ShigotoShousai sobject. 例如,我的查询尝试获取“
startDay__c
”(日期),这就像ShigotoShousai sobject的字段。 But result of query is just ID of the sObject instance. 但查询结果只是sObject实例的ID。
( parent : ShigotoShousai
child : ShigotoAssign
) ( 父母 :
ShigotoShousai
孩子 : ShigotoAssign
)
sObject[] result = [
SELECT
ShigotoShousai__r.id,
ShigotoShousai__r.startDay__c
FROM ShigotoAssign__c
];
system.debug(result) output system.debug(result)输出
shigotoAssign_ c:{Id=a06500000067aNjAAI, ShigotoShousai _c=a055000000DlHnOAAV}, shigotoAssign_ c:{Id=a06500000067aNoAAI, ShigotoShousai _c=a055000000DlHnTAAV}) 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
". 我得到了ShigotoShousai__c sObject的ID而不是它的属性“
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 :( 但查询结果刚刚给我回复了ShigotoShousai__c sobject的ID :(
Now I know have ID value of S higotoShousai__c
and want to access its field so I did following. 现在我知道有了S
higotoShousai__c
ID值,想要访问它的字段,所以我做了以下。
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 (ie ShigotoShousai__c). 然后我认为ID不能用于引用SObject(即ShigotoShousai__c)。
But I have its id.. How can I access, say startDay__c
? 但我有它的身份..我怎么能访问,比如说
startDay__c
? Is there a way to use this ID? 有没有办法使用这个ID?
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. 问题是您正在将SOQL查询结果分配给通用Sobject [],它除了Id之外没有自己的任何具体字段。 As long as you're not trying to do anything fancy with dynamic SOQL, try something like this:
只要你不想尝试使用动态SOQL做任何事情,尝试这样的事情:
ShigotoAssign__c[] result = [
SELECT
ShigotoShousai__r.id,
ShigotoShousai__r.startDay__c
FROM ShigotoAssign__c
];
for(ShigotoAssign__c s : result) {
System.debug(s.ShigotoShousai__r.startDay__c);
}
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. 下面的查询涉及父,子,孙,我使用apex来达到每个值。 Now I can display those value on visualforce page :)
现在我可以在visualforce页面上显示这些值:)
//WORK
public String workName { get; set; }
public String workContent { get; set; }
public String workCategory { get; set; }
public String workSponsor { get; set; }
//WORK DETAIL
public String workDetailStartDay { get; set; }
public String workDetailStartHour { get; set; }
public String workDetailStartMin { get; set; }
public String workDetailEndDay { get; set; }
public String workDetailEndHour { get; set; }
public String workDetailEndMin { get; set; }
public String workDetailCategory { get; set; }
public String workDetailDivision { get; set; }
//WORK ASSIGN
ShigotoAssign__c[] result = [
SELECT
ShigotoShousai__r.Shigoto__r.name,
ShigotoShousai__r.Shigoto__r.workContent__c,
ShigotoShousai__r.Shigoto__r.category__c,
ShigotoShousai__r.Shigoto__r.sponsor__c,
ShigotoShousai__r.id, ShigotoShousai__r.startDay__c, ShigotoShousai__r.startHour__c,
ShigotoShousai__r.startMin__c,
ShigotoShousai__r.endDay__c, ShigotoShousai__r.endHour__c, ShigotoShousai__r.endMin__c,
ShigotoShousai__r.workCategory__c, ShigotoShousai__r.workDivision__c,
ShigotoShousai__r.address__c,
id, contactStat__c
FROM ShigotoAssign__c
WHERE id = :workAssigned.id
];
//get WORK info to show on email template page
workName = result[0].ShigotoShousai__r.Shigoto__r.name;
workContent = result[0].ShigotoShousai__r.Shigoto__r.workContent__c;
workSponsor = result[0].ShigotoShousai__r.Shigoto__r.sponsor__c;
//get WORK DETAIL info to show on email template page
workDetailStartDay = String.valueOf(result[0].ShigotoShousai__r.startDay__c);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.