6

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.

3
  • Could you specify the syntax error and perhaps show the code in a formatted fashion. It would help to be able to see the code and the error it produces. Oct 17, 2013 at 12:29
  • Save error: Invalid field homepgProfile for SObject Contact. MY CODE userProfile = userLst[0].Contact.homepgProfile[0].AccountProfile_Field__c;
    – AnuRaj
    Oct 17, 2013 at 12:33
  • 1
    I would be very careful with what you're building, that looks like exposing a lot of security holes. (soql injection) Oct 17, 2013 at 14:28

2 Answers 2

5

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

List <MyCustomObject__c> myList = [SELECT id, name, rel__r.Name FROM ....] ; 

you can access the fields of MyCustomObject__c retrieved using the following notation

for (MyCustomObject__c mco:myList)
{
    system.debug('name:' + mco.get('name'));
}

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

SObject a = mco.getSObject('rel__c'); 

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

0

are you trying to access child relationships?? if I assume your userLst var is of a type, and if you want to access a field that is on a relationship you can do it this way

  //From the Parent object
  List<Account> accts = [SELECT Id, Name, Custom_Field__c (SELECT Id, Name, Custom_Field__c FROM Contacts) FROM Account];

  //Accessing to those records
  for(Account a : accts){
      for(Contact c : a.Contacts){

      }
  }

  // From the child object
  SELECT Id From Contact WHERE AccountId in (SELECT Id FROM Account)

And you can convert those queries dynamically if you want to, and please share your code so we can better understand your problem

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .