Saturday, November 3, 2018

Learn SOQL query in Salesforce

SOQL query in Salesforce:


SOQL(Salesforce object query language) is used to search data in Salesforce. With use of SOQL you can search data based on conditions.You can query data from Parent-Child object as well.Within apex
you can use SOQL to query data from object to identify the records on which processing needs to be done.

LIMIT and OFFSET keyword:

Limit keyword specifies the number of records which needs to be queried.
Offset keyword specifies the starting point from where records needs to be queried.

ORDER BY clause is used to arrange data in ascending and descending order.

Syntax:

ORDER BY FIELD_NAME ASC OR DESC

Below query are tested in developer console under debug,

1)Simple SOQL query to fetch Account information in apex.

Account obj=new Account(); // Declaring instance for account. 

obj=[select id,name from Account ];                                                                                

2)Simple SOQL query to fetch Account information in apex based on condition.

Account obj=new Account();

obj=[select id,name from Account  where id='SomeID'];

3)Query to fetch Account with contacts.

List<Account> accList=new list<Account>();

accList=[select id,name,(select id,name from contacts) from Account  ];

4)Query to fetch account from contact.

Contact obj=new contact();

obj=[select id,name,Account.name from contact limit 1  ];

5)LIMIT KEYWORD

Account obj=new Account();

obj=[select id,name from Account limit 1];  // Limit keyword specifies the number of records which
                                                                          needs to be queried.

6)OFFSET KEYWORD

list<Account> obj=new list<Account>();
list<Account> obj1=new list<Account>();


obj=[select id,name from Account order by id asc limit 10 ];
obj1=[select id,name from Account order by id asc limit 10 offset 1 ];

**************************************************************************************************************

TAGS:  SOQL query in Salesforce,soql query,soql query examples,soql,apex soql,soql query examples in salesforce,what is soql in salesforce,soql limit offset,soql offset example,soql order by,soql limit example,soql asc desc.

**************************************************************************************************************

No comments:

Post a Comment