You need to sign in to do that
Don't have an account?
Apex select doesn't retrieve all the fields
Hello all,
I'm doing a webservice method that has a soql in it. I have a Resource Assignment object which has 2 Lookups, one to Project and one to Resource. I'm querying fields from both of them with smth like this:
webservice static String getProjectsJSON() { List<Resource_Assignment__c> raList = [SELECT Id, Name, Amount__c, Start_Date__c, End_Date__c, Project__r.Name, Resource__c, Resource__r.Name, Resource__r.Start_Date__c, Project__r.Stage__c FROM Resource_Assignment__c WHERE Start_Date__c=NEXT_N_DAYS:150 AND End_Date__c=NEXT_N_DAYS:150]; System.debug('raList has this value: ' + raList); ...
The problem is I cannot access the fields from the lookups.
for (Resource_Assignment__c ra: raList) { String name = ra.Resource__r.Name; .... }
Trying to access for example Resource__r.Name will give me an error that there's no such field on the Resource Assignment object.
In the debug log I get the following:
|VARIABLE_ASSIGNMENT|[40]|raList|[{"Name":"RA - 0018","Project__r":{"Name":"Test Proj 1","Id":"a00E00000037WihIAE"},"End_Date__c":"2012-08-01T00:00:00.000","Project__c":"a00E00000037WihIAE","Resource__r":{"Name":"Marius B","Id":"a02E00000028m9pIAA","Start_Date__c":"2012-06-05T00:00:00.000"},"Resource__c":"a02E00000028m9pIAA","Id":"a01E000000At2WUIAZ","Start_Date__c":"2012-07-19T00:00:00.000","Amount__c":11},.....
However when I show the list using System.debug I only get this:
|USER_DEBUG|[43]|DEBUG|raList has this value: (Resource_Assignment__c:{Name=RA - 0018, End_Date__c=2012-08-01 00:00:00, Project__c=a00E00000037WihIAE, Id=a01E000000At2WUIAZ, Resource__c=a02E00000028m9pIAA, Start_Date__c=2012-07-19 00:00:00, Amount__c=11}, ....
So... anyone has any idea what I'm doing wrong?
I checked the field visibility, sharing rules etc. I run this as sys admin so there shouldn't be any reason why those fields are not visible/retrieved/whatever... :(
Thank you,
Adrian
You've to add the related object's fields in the Query which you want to use it
Please refere the code below
List<Resource_Assignment__c> raList = [SELECT Id, Name, Amount__c, Start_Date__c, End_Date__c, ( SELECT Name,Start_Date__c FROM Resource__c), (SELECT Name,Stage__c FROM Project__c) FROM Resource_Assignment__c WHERE Start_Date__c=NEXT_N_DAYS:150 AND End_Date__c=NEXT_N_DAYS:150];
Thanks for the response @Gunners_23!
Aren't I adding those fields using the Resource__r.Name, Project__r.Name fields already? What's the difference to your code?
Anyway I've tested you soql and it gives the following error:
And it doesn't work with __r either... :(
sorry i might have messed up with the relationship because i am not aware of the relationship between those objects.
The difference is you should query the fields of the related objects rather than using directly. check out in the query there is
another select statment which gets the related object fields data
for ex :
if candidate object has related job application object then query to get the candidate record with related job application
records must be :
Candidate__c candidate = [SELECT Name,First_Name__c,Last_Name__c,
(SELECT NAME,Average_Rating__c FROM Job_Applications__r) FROM Candidate__c LIMIT 1];
//Notice the way i'm fetching along with candidate fields i'm getting fields from job application through another select
statement
List<Job_Application__c> jobAppList = candidate.Job_Applications__r;
//storing the related list of job application results in to the list
Alright... I managed to solve it... and the solution is preeeetty weird(jump to the end to see it).
First, my Resource_Assignment__c was a child of Resource__c and Project__c( the object had 2 Lookups to those 2). SO I was traversing corectly the relationships to get the fields from those related objects, i.e. using Resource__r.Name. If the relationship would of been the other way, for example Resource was a child of Resource Assignment then the select in select method would of worked.
I'll put here the documentation to the problem at hand maybe it will help smbdy else:
1. when to use the . operator and when to use select in select: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
2. how to corectly use select in select: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm#i1422263
The solution it seems was to restart the force.com ide.... :) I know that compiling occurs at cloud level so I really don't know what to make of this. Maybe it's a weird eclipse bug or smth, but after restarting the ide the save worked just fine *shrugs. That's all.
Anyway, Thanks again @Gunners_23! It's nice to know that somebody else cares. When I'll have the time I'll try to give back to the community.
Have a nice day,
Adrian