1

我在下面有 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})

但是查询结果只是返回了我 ShigotoShousai__c sobject 的 ID :(

现在我知道 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?

4

2 回答 2

4

问题是您将 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);
}
于 2010-11-11T06:11:39.717 回答
0

也只想分享我的信息,因为它与问题有关。希望有人觉得这很有帮助。

下面的查询涉及父母、孩子、孙子,我使用顶点来达到每个值。现在我可以在 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);
于 2010-11-11T07:09:09.757 回答