0

I'm trying to create a dynamic query. I'll just simplify the code for better understanding. So for example my code looks like this.

String fieldName = '(Select id from care_applications__r)';
String sObjectName = 'Account';
String recordId = '123456789';
String query;
List<Sobject> listSobject = new List<Sobject>();

query = ' SELECT ID,' + fieldName + ' FROM ' + sObjectName + ' WHERE Id = \'' + recordId + '\'';

recordList = Database.Query(query);

The thing is when I'm using query editor of developer console. I was able to get the child record id's

But when I try to debug the recordList. I can't seem the get the child Id's instead it's giving me the accountId not the child record Id and when I'm trying to access it like for example.

System.debug(recordList[0].get(fieldName));

It will throw me an Invalid Field (Select Id From Care_Applications__r);

I don't know if I'm missing on something because my code looks fine. Thank you in advance.

I'm using the code btw as a controller for lightning component.

1
  • I don't see where you're verifying the relationship field even exists in your method here. When using a generic SObject you will get this if the relationship does not exist - i would refer to @Amit response below to point you to the child relationship name. If the main object is unknown - try using schema to find the relationship field matching the object you're fetching the related list for.
    – Ronnie
    Aug 2, 2018 at 12:03

2 Answers 2

2

If you refer to Understanding Query Results, and refer to the Parent-to-Child example, you will find the query and explanation as below.

SELECT Id, Name,
  (
    SELECT Id, FirstName, LastName
    FROM Contacts
  )
FROM Account
WHERE Name like 'Acme%'

This query returns a set of accounts, and within each account, a query result set of Contact fields containing the contact information from the subquery.

So in order to fetch the results in Apex, you will need to parse the "query result" of Contacts and then retrieve their fields accordingly. So your apex logic will look something as below (just replace the reference of contacts with your relationship name):

List<Sobject> slist = Database.query('Select Id, Name, (Select Id, Name From Contacts) From Account limit 5');
for(SObject s : slist) {
    Account a = (Account) s;
    system.debug('Account Name:' + a.Name);

    List<Contact> contacts = a.Contacts; // replace this with your relationship name
    for(Contact c: contacts) {
        system.debug('Contact Name:' + c.Name);
    }
}
3

To get the child records from the SOQL you need to use ChildRelationship name that is "care_applications__r" in your case.

Whenever we are using the inner query to get the child records then we always use the relationship name.

For example, We are using below Query to get the contacts related to an Account.

Select id, Name, (Select Id, Name From Contacts) From Account

Now, we wanted to retrieve the contacts records then use relationship name which is Contacts accountList[0].Contacts

To get the relationship name. Go to child Object(Contact) Open the relationship Field (Account) and see the Relationship name there

enter image description here

Use below code for your case: -

System.debug(recordList[0].care_applications__r);

Edit: -

In case of Custom Object, you need to append __r in relationship name

4
  • Hi Amit, i actually tried that earlier. but it throws me an invalid field. The query that I'm using is working on a query editor which means that I'm using the correct Child Relationship Name. thank you for your reply.
    – imajmdf
    Aug 2, 2018 at 11:09
  • 1
    Yes. The query will definitely work in Query Editor but to get the Child Records from the List in Apex Script you need to Use Child Relationship Name which is care_applications__r instead of (Select id from care_applications__r)
    – Amit Singh
    Aug 2, 2018 at 11:13
  • recordList[0].care_Applications__r is not working since it's an sObject. I need to make it dynamic since it's one of the requirements. I believe if it's an account list it will work but since it's an sobject it won't. Any suggestions on what I can do to make it dynamic?
    – imajmdf
    Aug 2, 2018 at 11:16
  • When you say it should be dynamic then from where you are getting the child relationship name?
    – Amit Singh
    Aug 2, 2018 at 11:40

You must log in to answer this question.

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