I am trying to get field value from a dynamic SOQL result. Here i have a object where end user need to enter the field name which he need. I am SOQL it like this:
UserprofileSOQL = 'SELECT Contact.'+homepgProfile[0].UserProfile_Field__c+',
Contact.Account.'+homepgProfile[0].AccountProfile_Field__c+'
FROM User WHERE id = \''+ userID +'\'';
where homepgProfile[0].AccountProfile_Field__c
and homepgProfile[0].UserProfile_Field__c
from an object where the end user enter the filed name.
Now i am trying to get the field value from the query result. I try to get the field value
as userProfile = userLst[0].Contact.homepgProfile[0].UserProfile_Field__c
but i am getting the following error:
Save error: Invalid field homepgProfile for SObject Contact. MY CODE
userProfile =
userLst[0].Contact.homepgProfile[0].AccountProfile_Field__c;
Please guide me the proper way to get the solution.
Best Answer
You can use the get method, but this is not so simple when you need to access relationship fields.
In more details if you have the following SoQL
you can access the fields of MyCustomObject__c retrieved using the following notation
However in your case you need to traverse relationships so its a little bit more complex. You need one more step to retrieve the related object using
then you need to do one more step using get method on a.
For more details check this http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_dml.htm