Intermediate 30 min read

Query the Catalog

By the end of this lesson, you'll be able to:

  • Write SOQL queries inside LibraryService to answer real catalog questions
  • Query only available (not checked out) books
  • Return query results in a form useful to a caller

Prerequisites: "Build the Core Classes"

Listing every book

public List<Book__c> getAllBooks() {
    return [SELECT Id, Title__c, Author__c, Is_Checked_Out__c FROM Book__c ORDER BY Title__c];
}
  1. public List<Book__c> getAllBooks() { — the simplest catalog-browsing method, and the foundation the next two build on.
  2. return [SELECT Id, Title__c, Author__c, Is_Checked_Out__c FROM Book__c ORDER BY Title__c]; — sorted alphabetically by title (Module 20's ORDER BY).
  3. } — closes the method.

Filtering to available books only

public List<Book__c> getAvailableBooks() {
    return [
        SELECT Id, Title__c, Author__c
        FROM Book__c
        WHERE Is_Checked_Out__c = false
        ORDER BY Title__c
    ];
}
  1. public List<Book__c> getAvailableBooks() { — answers "what can a member actually borrow right now?" directly.
  2. return [ — opens the query.
  3. SELECT Id, Title__c, Author__c — the fields needed.
  4. FROM Book__c — the object.
  5. WHERE Is_Checked_Out__c = false — this is Module 20's WHERE clause, without pulling every book and filtering in Apex afterward.
  6. ORDER BY Title__c — sorted alphabetically.
  7. ]; — closes the query.
  8. } — closes the method.

Finding books by a specific author

public List<Book__c> getBooksByAuthor(String authorName) {
    return [
        SELECT Id, Title__c, Author__c
        FROM Book__c
        WHERE Author__c = :authorName
        ORDER BY Title__c
    ];
}
  1. public List<Book__c> getBooksByAuthor(String authorName) { — takes the author name as a parameter.
  2. return [ — opens the query.
  3. SELECT Id, Title__c, Author__c — the fields needed.
  4. FROM Book__c — the object.
  5. WHERE Author__c = :authorName — :authorName is Module 20's bind variable pattern — the value flows straight into the query safely, without any raw string concatenation.
  6. ORDER BY Title__c — sorted alphabetically.
  7. ]; — closes the query.
  8. } — closes the method.

Exercise

Add a getCheckedOutBooks() method to LibraryService querying Book__c where Is_Checked_Out__c is true.

Show hint

WHERE Is_Checked_Out__c = true

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Query the Catalog Quiz

1. Why is filtering to available books directly in SOQL (WHERE Is_Checked_Out__c = false) better than querying all books and filtering in Apex?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

This lesson adds catalog-browsing methods to LibraryService, using the SOQL patterns from Module 20 to answer real questions like "which books are available right now?"