Thursday, June 13, 2019

Accessing Parent Field Values from sObjects in Dynamic Queries Using Dynamic Apex

In case of dynamic queries, we often store queries output in sObject and then refer field values. In order to refer field values, we first type cast generic sObject into specific objects like Account, Contact or custom objects as mentioned below:

string qString = 'Select id, name from Account Limit 1';
sObject sb = database.query(qString);
Account acc = (Account)sb;
string accountName = acc.Name;

Suppose we know the field API names then we can utilize the concept of sObject to get field values directly without type casting as mention below:

string qString = 'Select id, name from Account Limit 1';
sObject sb = database.query(qString);
string accountName = sb.get('Name');

So now with the help of sObjects concept, we can access parent field values from child records.

Now consider a scenario in which I have list which stores all field API names which can be used to query opportunity line items. I will generate the dynamic query and will use concept of sObject to access Opportunity fields from sObject which is output of dynamic query.

List<string> fieldAPINamesList = new List<string>{'Id','Opportunity.Name','Opportunity.Partner__r.Name'};
string qString ='Select '+string.join(fieldAPINamesList,',')+ ' from OpportunityLineItem where id=\'00k90000002z4ml\'';
system.debug('****qString:'+qString);
sobject sb = database.query(qString);
//syntax to get OpportunityLineItem Id  value from sObject
string OpportunityLineItemId = string.valueof(sb.get('Id'));
system.debug('******OpportunityLineItemId:'+OpportunityLineItemId);
//syntax to get Opportunity.Name field value from sObject
string OpportunityName = string.valueof(sb.getSobject('Opportunity').get('Name'));
system.debug('******OpportunityName:'+OpportunityName);
//syntax to get Opportunity.Partner__r.Name field value from sObject
//If partner field in opportunity is blank then you will get null pointer exception
string partnerName = string.valueof(sb.getSobject('Opportunity').getSObject('Partner__r').get('Name'));
system.debug('******partnerName:'+partnerName);

Note: While accessing the parent record details in child to parent query, you will get null pointer exception if parent is blank in child record.

I have written an utility method which takes sObject and fieldAPIName as a parameter and returns field value. Please refer below code for reference:

If you run above script in developer console by specifying correct opportunityLineItem Id, then you will see below logs:


Hope this will help!!!

8 comments:

  1. Thanks for sharing such a wonderful Post with us. I learnt a lot from your post. Keep Sharing. !!!

    Create Multiple Fields in Multiple Objects

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. www.dynamicmarketing.sg/seo-consultant-singapore

    ReplyDelete
  4. Excellent post. Solved my immediate problem of querying parent fields using dynamic soql.

    ReplyDelete
  5. Thanks for this! I was about to write a class to do exactly this though prob. not quite as elegantly, but since I found your I was able to adapt it to my use case and it saved me quite a bit of time!

    ReplyDelete
  6. The article is wonderful and quite inspiring. By using and engaging in this online spacebar clicker blog, you can determine and increase your spacebar test speed in a predetermined length of time. Reading the blog will help you use the spacebar more rapidly.

    ReplyDelete