SOQL and SOSL Difference in Salesforce
The following table describes about the SOQL and SOSL difference
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à 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];
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
The update DML operation modifies one or more existing SObject records, DML (update) statement syntax
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’;
—>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.
- 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*/
Note:- Account a =”select name; Phone FROM Account where id=’xxxx’;”
No compilation error, but it gives run time error like SObject row was retrieved via SOQL without querying the requested filed.
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
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;”
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
- The first uses the // token to mark everything on the same line.
- 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 */
- 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.
Note- A good practice is for class names to start with an uppercase letter and method names to start with a lowercase letter.