SOQL 连接查询返回 sObject 但不返回字段。获得的ID如何使用?

发布于 2024-10-01 21:31:58 字数 1498 浏览 8 评论 0原文

我有下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-10-08 21:31:58

问题是您将 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);
}

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:

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);
}
七月上 2024-10-08 21:31:58

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

下面的查询涉及父、子、孙,我使用 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);

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 :)

    //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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文