Getting Started With SOQL: Chapter No. 1 "Introduction To SOQL"
Getting Started With SOQL: Chapter No. 1 "Introduction To SOQL"
Getting Started With SOQL: Chapter No. 1 "Introduction To SOQL"
Magulan D
First and foremost, I would like to thank Packt Publishing for giving me the opportunity to write this book. I would like to thank Joanne and Harshal for their continuous support in publishing this book. I would like to thank Satheesh Kumar for his continuous effort in reviewing the book in spite of his busy schedule. He supported and encouraged me throughout my writing. I would like to thank my wife Gowripriya for her support and motivation. She helped me a lot in my writing. She motivated me to write this book. Her reviews and comments helped me complete my writing. It was a long and difficult journey for her. I dedicate this book to her. I would like to express my gratitude to all the people in Packt Publishing who supported me in publishing this book.
Chapter 4, Functions in SOQL, shows all the functions that are available in SOQL. It discusses about the methods for translating the field values using toLabel(), which will be very useful when we want to translate the values and show them in a report. Chapter 5, Limitations and Best Practices, shows the standards to be followed when writing SOQL statements. The best practice explained here allows us to retrieve the required records by filtering well. As a developer or as an administrator, we should follow these standards and best practices. Chapter 6, Tools with Installation Guidelines, shows a few tools that are available to execute SOQL statements. The installation guidelines will also be discussed with step-bystep instructions.
Introduction to SOQL
You will be introduced to SOQL in this chapter. This chapter will also discuss the API names of standard objects, custom objects, standard elds, and custom elds. These API names are used while querying using SOQL statements. This chapter explains when and where we use SOQL statements in Salesforce. SOQL syntax will give us more information, such as reserved keywords in SOQL, how to write SOQL statements, and so on. We will get started by writing our rst SOQL statement in this chapter.
What is SOQL?
Salesforce Object Query Language (SOQL) is used to build queries for fetching data in the Force.com platform. Just as we write a query in Structured Query Language (SQL) with some columns and a table, here, in SOQL, we write a query with some elds and an object. However, SOQL does not support all the features of SQL. For example, the * character in the SELECT statement denotes all columns in a table in SQL, but it cannot be used in the SELECT statement in SOQL. So, to retrieve all elds in SOQL, we have to mention all the elds separated by commas. SOQL is case insensitive. For ease of use, we suggest you to maintain SOQL keywords in uppercase and elds in initial case (rst letter in uppercase and the rest in lowercase). Throughout this book, all SOQL keywords will be written in uppercase and object names, eld lists, conditions, and so on will be written in lowercase. SOQL is very easy to understand if you have prior knowledge in SQL. As mentioned earlier, however, it does not support all the features available in SQL. If we think of tables as objects and columns as elds in Salesforce, writing SOQL becomes easier. Salesforce has standard objects (objects dened by Salesforce) and custom objects (objects dened by the user). The custom object ends with __c for identication purposes.
Introduction to SOQL
Good knowledge of SOQL helps us to optimize our code. If we are looking for data from different objects, SOQL helps us a lot in accomplishing that. Instead of writing complex code to achieve this, an administrator or developer with vast knowledge of SOQL may easily accomplish these kinds of tasks. The functions available in SOQL reduce our workload and save time. The sample queries used in this book are real-time examples with step-by-step explanations. Beginners will gain condence as we go ahead. Administrators and developers can also get ideas on how to optimize their code for faster execution of queries. An administrator can easily build any kind of complex report in an Excel le by extracting data from the objects using SOQL and delivering it to the clients in a timely manner if he or she has good knowledge of SOQL. SOQL eases the tasks of administrators, who are always looking for data. A developer also faces many situations where they may have to write SOQL queries in Apex programming. If the developer has wide knowledge of SOQL, they can easily accomplish their task without reiterating again and again to form data for manipulation. Make use of the tools available at Salesforce.com to execute the query instantly to clarify any doubts that arise. Salesforce provides tools, and third-party tools are also available. Steps with installation procedures and guidelines are available in Chapter 6, Tools with Installation Guidelines. The Developer Console can also be used for the easier and instant execution of queries. To use SOQL, we need to know the API name of the objects. To know the API names of the standard objects in Salesforce, visit the following reference link provided by Salesforce:
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_ objects_list.htm
Since custom objects are user-dened objects, information about these objects will not be available under Customize in the Force.com setup.
The API names of custom objects always end with __C.
The steps to get the API name of the custom objects change from environment to environment. In my organization, it is Setup | Build | Create | Objects, as shown in the following screenshot. We can view an object's API name on selecting it.
[6]
Chapter 1
The Objects link displays all the custom objects available in our organization, as shown in the following screenshot:
[7]
Introduction to SOQL
The Employee link redirects us to the Employee object custom denition, as shown in the following screenshot:
The SELECT statement is used to retrieve data from objects. Relationships must exist among objects in case we want to retrieve data from two or more objects.
It is not possible to write a single SOQL query to fetch records from two objects without any relationship among the two objects in Salesforce.
Relationship queries (queries for fetching records from more than one object) will be discussed in Chapter 2, Basic SOQL Queries.
Purpose of SOQL
The main purpose of SOQL is to fetch data from Salesforce objects. SOQL can be used in the following places: The queryString parameter in the query() call Apex statements Visualforce controllers and the getter methods The schema explorer of the Force.com IDE
[8]
Chapter 1
SOQL syntax
Similar to SQL, SOQL also makes use of the SELECT statement to fetch data. Let us explore the following SOQL syntax:
SELECT fields FROM Object WHERE Condition Ordering LIMIT FOR VIEW Or FOR REFERENCE OFFSET UPDATE VIEWSTAT
(optional)
records (optional)
UPDATE VIEWSTAT: This updates the articles' view statistics for fetched
SELECT, fieldList, FROM, and Object are required. The others are optional in
SOQL.
We should use the API names of the elds in the SELECT statement. We should not use the labels of the elds. The API names are available in the object denition. For Standard Fields, the Field Name column refers to the API name, and for Custom Fields, the API Name column refers to the API name. To get the API names of standard objects in Salesforce, navigate to Setup | Build | Customize | Object | Fields.
[9]
Introduction to SOQL
In the Force.com setup, we can get all the information related to standard objects in Salesforce by navigating to Build | Customize.
Let us see how to get the API names of the Account object elds. To get the API names of the Account object elds, navigate to Setup | Build | Customize | Accounts | Fields as shown in the following screenshot. The Standard object elds are present under Customize and custom objects are present under Create | Objects in Salesforce.
In the Account Standard Fields section, the Field Name column refers to the API name of the standard elds, as shown in the following screenshot:
[ 10 ]
Chapter 1
In the Account Custom Fields & Relationships section, the API Name column denotes the API name of the elds, as shown in the following screenshot:
To get the API names of custom objects in Salesforce, navigate to Setup | Build | Create | Objects, as shown in the following screenshot, and select the object:
[ 11 ]
Introduction to SOQL
This Objects link displays all the custom objects available in our organization, as shown in the following screenshot:
The Field Name column in the Standard Fields section denotes the API names of the elds, as shown in the following screenshot:
[ 12 ]
Chapter 1
The API Name column in the Custom Fields & Relationships section denotes the API names of the elds, as shown in the following screenshot:
[ 13 ]
Introduction to SOQL
Let us see another example of how to fetch custom elds in standard objects. Refer to the following screenshot:
[ 14 ]
Chapter 1
Here, Id and Name are standard elds and Active__c and CustomerPriority__c are custom elds. A sample query is given as follows:
SELECT Id, Name, Active__c, CustomerPriority__c FROM Account
In the preceding example, we saw how to retrieve records from Account (standard object). Let us write a simple SOQL query to fetch records from a custom object. In this example, let us make use of a custom object, Employee__c, which has custom elds such as Employee_Name__c, State__c, City__c, and so on.
[ 15 ]
Introduction to SOQL
Here, Employee__c is a custom object; Name is a standard eld; and Employee_ Name__c, State__c, and City__c are custom elds. Each and every object in Salesforce has system elds. System elds are read-only elds. The following is a list of system elds:
Id IsDeleted CreatedById CreatedDate
[ 16 ]
Chapter 1
LastModifiedById LastModifiedDate SystemModstamp All system elds are not editable. Only a few system elds are editable. To get edit access to system elds, we have to contact Salesforce support.
IsDeleted
[ 17 ]
Introduction to SOQL
Here, CreatedDate and LastModifiedDate are system elds. Let us see another example to fetch FirstName and LastName from the User object.
The User object is also another standard object in Salesforce. The User object stores all the information about the users in the organization. The IsActive eld is used to check whether the user is active or inactive.
[ 18 ]
Chapter 1
Let us see another example to fetch Name and StageName from the Opportunity object.
Opportunity is an important standard object in the Sales application in Salesforce. Opportunity is a potential revenue-generating event.
[ 19 ]
Introduction to SOQL
Let us see another example to fetch Name and Status from the Lead object.
Lead is also a standard object in Salesforce. Lead is used to store information about an organization or individual persons who are interested in our product. A Lead can be converted into a single Account, multiple Contacts, and multiple Opportunities objects.
[ 20 ]
Chapter 1
Let us see another example to fetch Id and Name from the Product object. The API name of the Product object is Product2.
The Product2 object stores all the information about the products available in our organization.
[ 21 ]
Introduction to SOQL
Let us see another example to fetch Id and Name from the Price Book object. The API name of the Price Book object is Pricebook2.
Pricebook2 is another standard object in Salesforce. In the Pricebook2 object, we use stored information on the different prices of products.
[ 22 ]
Chapter 1
Let us see another example to fetch ProductCode, Product2Id, Name, and UseStandardPrice from the Price Book Entry object. The API name of the Price Book Entry object in Salesforce is PricebookEntry.
PricebookEntry is another Salesforce standard object. We store the list price of the product under the Pricebook2 object in the Pricebookentry object.
[ 23 ]
Introduction to SOQL
Summary
In this chapter, we learned what SOQL is and got to know its purpose. While discussing the purpose, we saw where exactly we use SOQL statements in Salesforce development and administration. We discussed the fetching of the API name of the custom object with detailed descriptions and steps. Moreover, we saw the steps for fetching API names of the standard and the custom elds. The usage of system elds and querying system elds, with a description of each system eld, was provided in a table. Basic syntax of SOQL statements with all reserved keywords was discussed. We also saw some examples for fetching records using the SOQL queries from standard objects and custom objects. Finally, methods to nd the difference between custom objects and standard objects and custom elds and standard elds were introduced.
[ 24 ]
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet book retailers.
www.PacktPub.com