Salesforce SOQL连接查询返回sObject,但不返回字段。如何使用获得的ID?
下面是SOQL,我得到的结果包含sObject的ID。 我的假设是查询也将返回SObject的字段。 例如,我的查询尝试获取“Salesforce SOQL连接查询返回sObject,但不返回字段。如何使用获得的ID?,salesforce,apex-code,visualforce,soql,Salesforce,Apex Code,Visualforce,Soql,下面是SOQL,我得到的结果包含sObject的ID。 我的假设是查询也将返回SObject的字段。 例如,我的查询尝试获取“startDay\uuu c”(日期),它类似于ShigotoShousai sobject的字段。但查询的结果只是sObject实例的ID (家长:ShigotoShousai子女:ShigotoAssign) 系统调试(结果)输出 shigotoAssign_c:{Id=a06500000067aNjAAI,ShigotoShousai_c=a055000000DlH
startDay\uuu c
”(日期),它类似于ShigotoShousai sobject的字段。但查询的结果只是sObject实例的ID
(家长:ShigotoShousai
子女:ShigotoAssign
)
系统调试(结果)输出
shigotoAssign_c:{Id=a06500000067aNjAAI,ShigotoShousai_c=a055000000DlHnOAAV},shigotoAssign_c:{Id=a06500000067aNoAAI,ShigotoShousai_c=a055000000DlHnOAAV})
我得到了ShigotoShousai_uc sObject的ID,而不是它的属性“startDay_uc
”。
我认为输出应该是这样的:
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnOAAV, startDay__c=2010-10-10},
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnTAAV, startDay__c=2010-10-13})
但查询结果刚刚返回了ShigotoShousai___CSobject的ID:(
现在我知道ID值为ShigotoShousai_uuC
,我想访问它的字段,所以我做了如下操作
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_u_____________________________
但是我有它的id。我如何访问,比如说
startDay\uuu 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);
}
问题是,您正在将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页面上显示这些值:)
也只是想分享我的信息,因为它是相关的问题。希望有人觉得这很有帮助 下面的查询涉及父、子、孙和我使用apex来获取每个值。 现在我可以在visualforce页面上显示这些值:)
非常感谢您的解决方案!!我被困了好几个小时了!好孩子能见到父母。我非常怀念常规SQL。一旦您开始掌握SOQL及其关系的诀窍,它就会非常强大。我经常往返于SQL和SOQL之间,它们都有各自的优点。非常感谢您提供的解决方案!!我被困了好几个小时了!好孩子能见到父母。我非常怀念常规SQL。一旦您开始掌握SOQL及其关系的诀窍,它就会非常强大。我经常往返于SQL和SOQL之间,它们都有各自的优点。您不必将所有这些值反引用到变量中,以使它们显示在Visualforce页面上。您只需创建一个类型为List的var,然后使用类似apex:dataTable的东西对其进行迭代,并在页面上使用点符号。这里有一个例子:另外,如果要将“id=”添加到WHERE子句中,则不需要将result设置为数组——只需说ShigotoAssign\uu c result=…您不必将所有这些值反引用到变量中,就可以让它们显示在Visualforce页面上。您只需创建一个类型为List的var,然后使用类似apex:dataTable的东西对其进行迭代,并在页面上使用点符号。下面是一个示例:另外,如果要将“id=”添加到WHERE子句,则不需要将结果设置为数组——只需说ShigotoAssign\uuu c result=。。。
//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);