SOQL stands for Salesforce Object Query Language. You can use SOQL to read information stored in your org’s database. SOQL is syntactically similar to SQL (Structured Query Language).
You can write and execute a SOQL query in Apex code or in the Developer Console’s Query Editor.
// Add first contact and related details Contact contact1 = new Contact( Firstname='Quentin', Lastname='Foam', Phone='(415)555-1212', Department= 'Specialty Crisis Management', Title='Control Engineer - Specialty - Solar Arrays', Email='qfoam@trailhead.com'); insert contact1; // Add second contact and related details Contact contact2 = new Contact( Firstname='Vega', Lastname='North', Phone='(416)556-1312', Department= 'Specialty Crisis Management', Title='Control Engineer - Specialty - Propulsion', Email='vnorth@trailhead.com'); insert contact2; // Add third contact and related details Contact contact3 = new Contact( Firstname='Palma', Lastname='Sunrise', Phone='(554)623-1212', Department= 'Specialty Crisis Management', Title='Control Engineer - Specialty - Radiators', Email='psunrise@trailhead.com'); insert contact3;
SELECT Name, Phone, Email, Title FROM Contact WHERE (Department = 'Specialty Crisis Management')
The results display the details of the contacts who work in the Specialty Crisis Management department.
You can use another SOQL query to find contacts in other departments, or to see whether anyone else has created records for more Control Engineers. To rerun a query, click Refresh Grid in the Query Results panel.
A SOQL query that you execute using Apex code is called an inline SOQL query. Let’s see how you can use the Developer Console to search for contacts working in the Specialty Crisis Management department using an inline SOQL query.
Contact[] theseContacts = [SELECT Name, Phone, Email, Description FROM Contact WHERE (Department='Specialty Crisis Management') ORDER BY Name]; // Log a count of how many contacts were found System.debug(theseContacts.size() + ' contact(s) returned.'); // Log all values in the array of contacts System.debug(theseContacts);
To delve deeper into SOQL queries, check out the Apex Basics & Database module.
SOSL (Salesforce Object Search Language) is a language that performs text searches in records. Unlike SOQL, SOSL can query multiple types of objects at the same time. SOSL can also use a word match to match fields, while SOQL needs the exact phrase.
When you run a SOSL search for contact records using the word “Crisis,” your search looks through all contact fields and returns any record containing that word. But if you try the same in a SOQL query, you need to specify the fields to search and a complete word or phrase to search for. You can also use LIKE or wildcards to narrow down SOQL or SOSL searches.
FIND IN ALL FIELDS RETURNING Contact(FirstName, LastName, Phone, Email, Title)
The Execution Log lists the names of the Control Engineers.
To learn more about what makes SOSL searches tick, check out the Apex Basics & Database module.
While you were playing with SOQL and SOSL, the Control Engineers whose records you were looking up steered your spaceship out of the asteroid’s path. It turns out that commanding a spaceship isn’t so hard after all: You just need to have a good console, and to learn to delegate!
Now that you have avoided a collision with asteroid 2014 QO441, you decide to land at the Neptune Space Station to take a well-deserved break. With the knowledge of the various functions and features of the Developer Console, you can steer your org through many missions with success.