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?
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);
}