Get Books with Missing Author Data
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 10:53 minute mark, in case you want to rewind and watch the beginning of the step again.)
Introduction
Books4Everyone has recently discovered they have many books without an author listed. They want a component on the lower right corner of the home page to make sure these books can be updated quickly. Just like before, they provide you with the SQL.
Write a Right Anti Join
Here's the SQL query they gave you.
SELECT Name, Description FROM Book WHERE Author IS NULL
You know that in SOQL we will write it like this.
SELECT Name, Description__c FROM Book__c WHERE Author__c = null
Write the Apex and the New Component
Now, add the SOQL query to the Apex controller.
- In the Developer console, open the Books4EveryoneHomeController Apex class.
- Add the following block of code after the first method.
@AuraEnabled public static List<Book__c> getBooksWithoutAuthors(){ return [SELECT Name FROM Book__c WHERE Author__c = null]; }
Once again, create a new Lightning component for the Home page.
- Close any open tabs in the Developer Console and then choose File > New > Lightning Component.
- Name your new Lightning component
Books4EveryoneMissingAuthors
. - In the component configuration section, select Lightning Page and click Submit.
- Replace the code in the .cmp file with the following code.
<aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="Books4EveryoneHomeController"> <aura:attribute name="Books" type="Book__c" /> <aura:handler name="init" action="{!c.doInit}" value="{!this}"/> <lightning:card title="Books4Everyone Missing Author List"> <aura:set attribute="body"> <table class="slds-table slds-table_bordered slds-table_cell-buffer"> <thead> <tr class="slds-text-title_caps"> <th scope="col">Book Title</th> </tr> </thead> <tbody> <aura:iteration items="{!v.Books}" var="books"> <tr scope="row"> <td> {!books.Name}</td> </tr> </aura:iteration> </tbody> </table> </aura:set> </lightning:card> </aura:component>
- Click Controller on the right side of the page to create a controller for your component.
- Replace the controller’s JavaScript with the following code.
({ doInit: function(component, event, helper) { var action = component.get("c.getBooksWithoutAuthors"); action.setCallback(this, function(data) { component.set("v.Books", data.getReturnValue()); console.log(data.getReturnValue()); }); $A.enqueueAction(action); } })
- Save all your open tabs.
Let’s See This Component in Action!
Our final step is to ensure our query worked as expected by adding our component to our homepage.
- In your org, navigate to the Books4Everyone app.
- Click Home.
- In the upper right hand corner, click and select Edit Page.
- On the left side of the Lightning app builder, find Books4EveryoneMissingAuthors in the list of custom components.
- Drag the component to the top right panel on the page.
- Click Save and then to return to the Home page.
- The final step is to check out the data on your Home page. It should look something like this.