SOQL and SOSL Difference

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

SOQL and SOSL Difference in Salesforce

The following table describes about the SOQL and SOSL difference

SOQL SOSL

1. SOQL (Salesforce object Query Language) retrieves The records from the database by using “SELECT” keyword

 

2. By using SOQL,we can know in which objects or fields the data resides

 

3. We can retrieve data from single object or from multiple objects that are related to each other

  4. We can query on only one table

1. SOSL(sales force object search language) retrieves the records from the data base by using “FIND” key word

 

2. By using SQSL,we don’t know in which object or field the data resides

 

3. We can retrieve multiple objects and field values efficiently when the objects may or may not be related to each other

  4. We can query an multiple tables.

 

SOQL Query Examples:-

-A SOQL query is enclosed between square brackets the following query retrieves an subjects (a record from the database) that has the name field value equal to “Osmania University” Account a = "select Id name FROM account where name = 'Osmania University' "           Return type     Query (Applicable for only one record) Note:- In the above the “a” variable stores the ID ,Name of all accounts with name=’Osmania University’ Note:- SOQL Statement evaluate to a list of SObjects single SObjects or an integer for count method queries. LIst of SObject à List < Position_c Position = "select name from position_c shwre type_c='part time'"; Single sumbject: Postion_c p="select name from position_c where id='xxxxx'" Integer: Integer i="select countc,from position_c where c type_c='part time'" SOQL Bindings:-Square bracketed queries support binding to Apex variable String jobtype='Full Time'; List<position_c> S="select name from position_c where type_c=:job type;"

Enthusiastic about exploring the skill set of Salesforce? Then, have a look at the blog post to gather additional knowledge on Salesforce Training and Certification.

SOSL Queries

SOSLà sales force object search language

  • SOSL statements evaluate to a list of lists of SObjects where each list contains the search results for a particular sobject type
  • SOSL queries are only supported in Apex classes and anonymous blocks.
  • We cannot use a SOSL query in a trigger

The folloing exapmle that searches all fields across all account and contact objects. Ex:- LIst<List<Sobject>> searchist="Find 'Text*' IN ALL FIELDS RETURNING Account, Contact Systerm.debug(Serachlist);" This query will search the account and contact records that field values starts with test in all fileds.

  • The following example returns a list of accounts, contacts, opportunities and leads that begin with the phrase map.

LIst<List<Sobject>> searchist=[Find 'map*' IN ALL FIELDS RETURNG Account(ID, Name),contact,opportunity,Lead]; System.debug(searchist); Account(ID, Name)--> this means it returns the account records with ID, name values Note:-   If a SOSL query does not return any records for a specified SObject type, the search results include an empty list for that SObject Updated Operation: The update DML operation modifies one or more existing SObject records, DML (update) statement syntax Update SObject; Update Subject[ ]; The following example describes us about an updating an account field through an existing contact that has also been modified here two update statements are required // use a SOQL Query to access data for a contact Contact C=[SELECT Account.Name FROM contact where Last Name ='Anil' LIMIT 1]; C.Account.Name='Capital info Solutions'; C.Lastname='sateesh'; --->The above two statements says that change in fields for both contact and its associated account Upadate C:--> updates the contact. Update C.Account: --> Update the Account. System.Debug(c); System.debug(c.Account.Name);

  • The following example updating account fields through the API

Access a="select name,Rating,Industry FROM Account Where id='xxxxx';" /*This query retrieves the Name, Rating, Industry fields values of one particular account*/ a.Name='Osmania University'; a.Rating='Hot'; a.Industry='Electrical'; Update a; System debug(a) Note:- Account a ="select name; Phone FROM Account where id='xxxx';" System.debug(a.Industry) No compilation error, but it gives run time error like SObject row was retrieved via SOQL without querying the requested filed.

Delete Operation:-

 The delete DML operation deletes one or more existing SObject records, such as individual accounts or contacts, from organization’s data. The DML (Delete) statement syntax is Delete SObject; Delete SObject[]; The following example is to delete the account record through the API //First get the account record which we want to delete Account a =" select ID from account where name='Osmania University' LIMIT 1;" Delete a;* The DML Operation deletes the above record.*/

  • Deleting a record places it in the recycle bin from where we can restore it. Records in the Recycle bin are temporarily stored for 15 days before they are permanently deleted.
  • To restore just use the undelete DML statement .Notice that we used the ALL ROWS keywords in the SOQL query to be able to retrieve the delete record.

Account a =" select name FROM account where name='Osmania University' ALL Rows;" UnDelete a; Note:- Comments:-: - Comments are lines of text that we add to code to describe what it does comments are not executable code. It’s a good practice to annotate code with comments as necessary. Apex has two forms of comments

  1. The first uses the // token to mark everything on the same line.
  1. The second encloses a block of text, possibly across Multiple lines between the /* and */ tokes

The following example describes only the debug statement runs: System.debug (‘capital info solutions ‘); //This comment is ignored. /* Hi All, All the Best From capital info solutions */

Case Sensitivity:-

  • Apex is case insensitive this means that all apex code, including method names class names, variable names and keywords, can be written without regard to case

EX:- Integer Myvar; IntegER MYVAR;     --->both are equivalent statements The following statements print out today’s date using the System today method when we execute them in the Developer Console. System.debug(System.today()); System.debug(System.Today()); System.debug(System.TOday()); Note- A good practice is for class names to start with an uppercase letter and method names to start with a lowercase letter.

For indepth understanding on Salesforce click on

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox