Hi folks,
When developing data driven applications it is mandatory to retrieve only the data which is require for that operation. Retrieving all or data which is not needed will cost performance and time. Therefore when dealing with data in Salesforce, you can always test your queries like you test data with SQL Server Management Studio. Like that tool, Salesforce has also created a nice online tool called Workbench
There are 2 methods that you can use to query your data.
1. Salesforce Object Query Language (SOQL) - A SOQL query is the equivalent of a SELECT SQL statement and searches the org database.
2. SOSL - SOSL is a programmatic way of performing a text-based search against the search index.
Use the REST and SOAP protocols to execute queries and searches:
SOSL is used for search records and it acts similar to 'like' in SQL.
Let's try it.
I can remember I have a book which has a title of 'ddd' and I want to find it.
I entered the 'dd' as search keyword and I have selected field 'Name' in Book object using the given dropdown lists.
Then Workbench has created the query for me and I have executed it.
So I thing now you have the basic idea of SOQL and SOSL. Now it's your time to play with it...
When developing data driven applications it is mandatory to retrieve only the data which is require for that operation. Retrieving all or data which is not needed will cost performance and time. Therefore when dealing with data in Salesforce, you can always test your queries like you test data with SQL Server Management Studio. Like that tool, Salesforce has also created a nice online tool called Workbench
There are 2 methods that you can use to query your data.
1. Salesforce Object Query Language (SOQL) - A SOQL query is the equivalent of a SELECT SQL statement and searches the org database.
2. SOSL - SOSL is a programmatic way of performing a text-based search against the search index.
Decide which to use when?
Use SOQL when you know which objects the data resides in, and you want to:- Retrieve data from a single object or from multiple objects that are related to one another.
- Count the number of records that meet specified criteria.
- Sort results as part of the query.
- Retrieve data from number, date, or checkbox fields.
- Retrieve data for a specific term that you know exists within a field. Because SOSL can tokenize multiple terms within a field and build a search index from this, SOSL searches are faster and can return more relevant results.
- Retrieve multiple objects and fields efficiently where the objects might or might not be related to one another.
- Retrieve data for a particular division in an organization using the divisions feature.
- Retrieve data that’s in Chinese, Japanese, Korean, or Thai. Morphological tokenization for CJKT terms helps ensure accurate results.
Sending Queries
- Query (REST) and query() (SOAP)—Executes a SOQL query against the specified object and returns data that matches the specified criteria.
- Search (REST) and search() (SOAP)—Executes a SOSL text string search against your org’s data.
NOTE: Apex requires that you surround SOQL and SOSL statements with square brackets to use them on the fly. You can use Apex script variables and expressions when preceded by a colon (:).
Ok, Lets try something with Workbench.
I have created a custom object called Book and I inserted some records to this object.
Now, I going to retrieve the Names and Prices of all the books.
You can do this by executing the following query:
SELECT Name, Price__c FROM Book__c
NOTE: SOQL doesn’t support all advanced features of the SQL SELECT command. For example, you can’t use SOQL to perform arbitrary join operations, use wildcards in field lists, or use calculation expressions.
Actually, you can select the Object and fields that you want to retrieve from the dropdown list and then the Workbench will create the query for you.
Then click on 'Query' button to execute the query.
Ok, Lets try something with Workbench.
I have created a custom object called Book and I inserted some records to this object.
Now, I going to retrieve the Names and Prices of all the books.
You can do this by executing the following query:
SELECT Name, Price__c FROM Book__c
NOTE: SOQL doesn’t support all advanced features of the SQL SELECT command. For example, you can’t use SOQL to perform arbitrary join operations, use wildcards in field lists, or use calculation expressions.
Actually, you can select the Object and fields that you want to retrieve from the dropdown list and then the Workbench will create the query for you.
Then click on 'Query' button to execute the query.
SOSL is used for search records and it acts similar to 'like' in SQL.
Let's try it.
I can remember I have a book which has a title of 'ddd' and I want to find it.
I entered the 'dd' as search keyword and I have selected field 'Name' in Book object using the given dropdown lists.
Then Workbench has created the query for me and I have executed it.
So I thing now you have the basic idea of SOQL and SOSL. Now it's your time to play with it...
Comments
Post a Comment