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(result)出力

shigotoAssign_ c:{Id = a06500000067aNjAAI、ShigotoShousai _c = a055000000DlHnOAAV}、shigotoAssign_ c:{Id = a06500000067aNoAAI、ShigotoShousai _c = a055000000DlHnTAAV})

プロパティ""の代わりにShigotoShousai__csObjectのIDを取得しましたstartDay__c。出力は次のようになると思いました:

shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnOAAV, startDay__c=2010-10-10}, 
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnTAAV, startDay__c=2010-10-13})

しかし、クエリ結果はちょうど私にShigotoShousai__csobjectのIDを返しました:(

これで、ID値がSであることがわかり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 []に割り当てていることです。この汎用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

また、質問に関連しているので、私の情報を共有したいだけです。うまくいけば、誰かがこれが役立つと思うでしょう。

以下のクエリには、親、子、孫が含まれ、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);
于 2010-11-11T07:09:09.757 に答える