SOQL加入查询返回sobject,而不是字段。如何使用获得的ID?
-
08-10-2019 - |
题
我在下面有SOQL,并且得到包含Sobject ID的结果。我的假设是查询也将返回Sobject的字段。例如,我的查询尝试获取”startDay__c
“(日期)就像shigotoshousai sobject的字段一样。但是查询的结果只是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 = a05500000000dlhnoaav},shigotoAssign_c:{id = a06500000067anoaai,shigotoshousai_c = A055000000DLHNTAAV})
我有Shigotoshousai __c Sobject而不是其财产的身份证”startDay__c
“。我认为输出会像:
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnOAAV, startDay__c=2010-10-10},
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnTAAV, startDay__c=2010-10-13})
但是查询结果刚刚返回了我的shigotoshousai__c sobject :(
现在我知道有S的ID值higotoShousai__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?
解决方案
问题在于,您将SOQL查询结果分配给了通用Sobject [],除了ID之外,它没有其自己的任何具体字段。只要您不尝试用动态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);
}
其他提示
也只想分享我的信息,因为它与问题有关。希望有人能找到这一点。
以下查询涉及父母,孩子,孙子和我使用Apex达到每个值。现在,我可以在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);