soql結合クエリは、sobjectを返しますが、フィールドではありません。取得したIDを使用する方法は?
-
08-10-2019 - |
質問
以下にSOQLがあり、SobjectのIDが含まれている結果が得られます。私の仮定は、クエリがSobjectのフィールドも返すことでした。たとえば、私のクエリは取得しようとします」startDay__c
"(日付)of透bujectのフィールドのようなもの。しかし、クエリの結果は、sobjectインスタンスの単なるIDです。
(親: ShigotoShousai
子: ShigotoAssign
)
sObject[] result = [
SELECT
ShigotoShousai__r.id,
ShigotoShousai__r.startDay__c
FROM ShigotoAssign__c
];
System.Debug(結果)出力
shigotoAssign_c:{id = a06500000067anjaai、otoshousai_c = a055000000dlhnoaav}、shigotoAssign_c:{id = a06500000067anoaai、othotoshousai_c = a0550000dlhntaav})
私はその財産の代わりに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})
しかし、クエリの結果は、objectのshigotoshousai__cの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(すなわちobotoshousai__c)を参照することはできないと考えました。
しかし、私はそのIDを持っています..どうすればアクセスできますか、たとえば startDay__c
?このIDを使用する方法はありますか?
解決
問題は、SOQLクエリの結果をジェネリックSobject []に割り当てていることです。ダイナミックソクルで派手なことをしようとしていない限り、次のようなことを試してみてください。
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);
}
他のヒント
また、質問に関連しているので、私の情報を共有したいだけです。うまくいけば、誰かがこれを役立つと思います。
以下のクエリには、親、子供、孫が含まれ、私は頂点を使用して各値に到達します。これで、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);