Soql where clause chatters salesforce

Executing SOQL and SOSL queries

Run a SOQL query

Before we start writing and running queries, you need to put data in your Salesforce org that we can then search for. We are therefore now adding the contact details of three technicians sent by the control center to steer your spaceship away from the asteroid 2014 QO441.
  1. Choose Debug | Open Execute Anonymous Window out.
  2. Copy, paste and run the following code. This code adds the contact details of three technicians to the "Contact" object in your database. // 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='[email protected]'); 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='[email protected]'); 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='[email protected]'); insert contact3; Now we can use the Query Editor to write and run a SOQL query looking for these technicians. SOQL queries look for data in specific objects. You can include an optional condition in the WHERE clause to narrow the search.
  3. Enter the following query in the Query Editor tab: SELECT Name, Phone, Email, Title FROM Contact WHERE (Department = 'Specialty Crisis Management')
  4. click on Execute.

The result is the data 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 if someone else has created records for additional technicians. To run a query again, click in the Query Results area Refresh Grid.

A SOQL query that you run using Apex code is known as an inline SOQL query. Let's see how you can use the Developer Console to search for contacts in the Specialty Crisis Management department with an inline SOQL query.

Let's also try to sort the results alphabetically by name.
  1. Choose Debug | Open Execute Anonymous Window out.
  2. Enter the following code: 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);
  3. After the code runs, open the log.
  4. To only display the USER_DEBUG messages, choose Debug Only out. The results show the number of accounts returned and the contact details in alphabetical order.

If you want to deepen your knowledge of SOQL queries, take a look at the Apex Basics and Database module.