đź“Ł Attention Salesforce Certified Trailblazers! Link your Trailhead and Webassessor accounts and maintain your credentials by December 14th. Learn more.
close
trailhead

Add Author Data to Your Book Ratings Component

In this step you’ll write a multilayered query to get data from nearby objects and update the recommendations component for Books4Everyone. Books4Everyone wants to enhance the recommendations component by adding the author’s name to it.

Writing the SOQL

This time they didn’t give you any SQL to work with. They want to get the author’s name and display it on the Books4EveryoneRecommendations component. Here's the current query you’re using.

SELECT Name, Review__c, Rating__c, Book__r.Name
  FROM Recommendation__c
  WHERE Book__c != null
  

Since you also want to get the name of the author, you look at the data model to see how it’s related to the objects you’re already querying. Recommendations are related to books, which are related to authors. In SOQL you can do up to seven lookups, so you know you’re within your bounds. 

  1. In the Developer Console, open the Apex class Books4EveryoneHomeController.
  2. Update the SOQL in the getBookRecommendations method with a new return line.
    return [SELECT Name, Review__c, Rating__c, Book__r.Name, Book__r.Author__r.Name
      FROM Recommendation__c
      WHERE Book__c != null];
        
  3. Open the Books4EveryoneRecommendations Lightning component.
  4. Right now our table won’t display our newest data. Lets add that in by updating the code with the following.
    <table class="slds-table slds-table_bordered slds-table_cell-buffer">
      <thead>
        <tr class="slds-text-title_caps">
          <th scope="col">Book Title</th>
          <th scope="col">Author</th>
          <th scope="col">Rating</th>
          <th scope="col">Review</th>
        </tr>
      </thead>
      <tbody>
        <aura:iteration items="{!v.Recommendations}" var="recommendations">
          <tr scope="row"> <td> {!recommendations.Book__r.Name}</td>
            <td> {!recommendations.Book__r.Author__r.Name}</td> <td> {!recommendations.Rating__c}
            </td> <td> {!recommendations.Review__c}
            </td>
          </tr>
        </aura:iteration>
      </tbody>
    </table>
        

Great job! You’ve now written a multilayered query and can see the results on your home page. 

This is an image showing the home tab and the three lightning components with SOQL-queried data

Books4Everyone is thrilled with your work, and thanks you personally by baking a cake (shaped like a book, of course) in your honor!

Summary

You’ve learned several things in this project. You went from a simple SOQL query to a much more complicated one and you translated queries from SQL into SOQL. There is a lot more that you can do with SOQL, but you’re off to a great start! A great next step would be to do the badges in the Apply .Net Skills to Salesforce trail.

retargeting