Skip to main content

Add Author Data to Your Book Ratings Component

Follow Along with Trail Together

Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.

(This clip starts at the 12:48 minute mark, in case you want to rewind and watch the beginning of the step again.)

Introduction

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 getBookRecommendationsmethod 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. Make sure you have selected the Component file on the right side of the console.
  5. Locate the section of the component file that starts with <table and ends with </table> and replace it 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>
  6. Save the files.

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.

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities