문제

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

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top