SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex SOQL Simplified

Let’s understand APEX SOQL(Salesforce Object Query Language) with a little intro about the data itself.

Any meaningful information is called data. This data is stored in a database for storage. The database is a very big container that holds all this information which we referred to as data.

If you have stored anything in a container then you need certain ways to fetch that too. To facilitate this Database Query Language comes to the rescue which lets you fetch data stored in the Database in a certain number of rows.

Apex SOQL is also a kind of Database Query Language that lets you pull the data from the Salesforce in a row format that matches pre-defined criteria. It’s much similar to SQL which is used in other databases such as MySQL.

The basic code block of SOQL looks like the below. This block returns a list of records that are iterated or passed to other methods for further processing :

[SELECT Id, Email FROM Contact WHERE Name = 'Tony Stark'];

You can also pass parameters stored in variables in the Where clause by the below way :

String name = 'Tony Stark';
Contact conObject = [SELECT Id, Email FROM Contact WHERE Name = :name];

Once records are returned by Apex SOQL they are mostly assigned to an object or Collections based upon the objective. In the below block we are assigning it to an object. Here is an interesting behavior to know that if the query returns a single row then all is well otherwise if it returns more than 1 row or no rows at all it will throw an error – ‘List has more than 1 row for assignment to SObject error’

Contact conObject = [SELECT Id, Email FROM Contact WHERE Name = 'Tony Stark'];

Thus, it’s recommended to use a List while assigning back the SOQL results. This list can be later checked if it’s empty or not and based upon this further processing can be done. Feel free to take a quick peek at Apex Collections to know more about the list or map before proceeding below 🙂

List<Contact> conList = [SELECT Id, Email FROM Contact WHERE Name = 'Tony Stark'];

if(!conList.isEmpty()) {
    // lots of processing here
}

You can also do an inline initialization of a Map with Apex SOQL using the below approach. This comes in handy when you need to do a quick check if certain fields have what values for a given record id.

Map<Id, Contact> mapOfContact = new Map<Id, Contact>([
    SELECT Id, Email FROM Contact WHERE Email = 'salesforceblue@gmail.com'
]);

Sometimes query strings are prepared at runtime to determine which fields to query and which to exclude in some scenarios. You are then required to prepare a dynamic query string and use a Database.query() method to do an Apex SOQL. Refer to the code block below

String fieldName = '';
// Lots of Cool Operation
fieldName = 'Id, Email';
List<Contact> contactLits = Database.query('Select ' + fieldName + 'From Contact');

Now you are aware of the Database and how to fetch your rows of data from it using Apex SOQL.

There are also further interesting add-ons for Apex SOQL known as Aggregate Functions to aggregate query results, SOQL For Loop to deal with a very large SOQL query, SOQL Relationship Queries for querying related objects and SOQL For Update to lock records.

You can take a quick peek to know more about its usage and application over here – Apex Aggregate Functions, Apex SOQL For Loop, Apex SOQL Relationship Queries & Apex SOQL For Update.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *