I'm having trouble fetching courses__c from the Certification__c object (who is the mother).

The code works, for the certifications : I fetch only but every Certification__c that have related Courses.

My issue is that the second loop fetch nothing. In fact, in the second for loop certif.courses__r.size() always equals 0.

Here is my code

// DEV 450 - P 192 - Subrequests
public static void DEV450_P192_SOQL_Subqueries () {
    // this query selects only certifs that have courses related

    String req = 'SELECT name FROM Certification__c WHERE ID IN (SELECT Certification__c from Course__c)';
    Integer count = 0;
    List<Certification__c> CertList = Database.query(req);
    // boucle sur les certification__c
    for(Certification__c certif : CertList) {
        System.debug('Certification ' + count + ' : ' + certif);
        // pas bonne pratique mais pour debug ony : renvoie 0
        // System.debug('Nombre de courses dans certif : ' + certif.courses__r.size());

        // deuxieme boucle pour preserver la heap size
        for (Course__c course : certif.Courses__r) {
            System.debug('Nom du cours' + course.name);

This is what I tried to test (don't look at the 1. question) :

enter image description here

Do I have to improve the query, for it to bring me back more informations about the Certification__c objects ? I tried it too but I was unsuccessful ...

Thanks for your help, Feels good to have a community for beginners ! :)

Nb : how can I post with colors inside the code ? It's difficult to read without it .. Sorry, I'll edit. For the moment, I'll post my code in screenshot also :

enter image description here

  • 1
    If you use the {} button in the editor, you'll usually get some syntax highlighting. It's not as nice as an IDE highlight, but it is usually functional. Having code we can read without an image is preferable. – sfdcfox Jun 27 '18 at 13:07
  • 1
    Actually, I should mention that the syntax highlighter is tied to the tags that you use. When posting questions about Apex Code, for example, use apex to make sure it highlights correctly. Same thing for Visualforce, Lightning, etc. – sfdcfox Jun 27 '18 at 13:31
  • Thanks for mentioning that, I think I've already tried to use the {} editor's button, but never used the 'language parameter', Thanks for my next posts ;) – Alexis MASSON Jun 28 '18 at 7:43

The filter simply selects which records are going to be returned. If you actually wanted the courses, you'd put that in to the query's field list:

String req = 'SELECT name, (SELECT Name FROM Courses__r) FROM Certification__c WHERE ID IN (SELECT Certification__c from Course__c)';

Note that you'll need to go in to the Course__c object, take a look at the Certification__c field, and see what the "Relationship Name" is (here, I used Courses__r, but it could be configured to be something else).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.