Friday, June 1, 2012

SOQL Advance Features


1. Group by feature:
Salesforce Developers are the aware of with “GROUP BY” word in SQL (Stucture Query Language)/SOQL (Salesforce Object Query Language). This key word returns count of records which are selected.
We can put the count to filter the records. My requirement is that if email is duplicates in contact’s record then I need to exclude those records. Following is the code for that, may it will help you too.
SOQL Code:
AggregateResult[] groupedRs = [Select c.Email from Contact c where email =emailed@gmail.com’ group by Email having count(Email)<2];
Description:
As above SOQL checks the email address and group by Email field count is less than 2. So using the above SOQL we could checks how much records are fetching in result set.
2. Salesforce Object initialization feature:
Up till now process to update any sobject was to first query that object using its Ids, and than we used to update that list.
Now if we are having Id of sobject than we need not do any SOQL. For ex-
Code:
Opportunity tempObj = new Opportunity (id = '0063000000Xh92q');
tempObj.name='Test 123';
Update tempObj;
Description:
This above code will update opportunity with Id=’ '0063000000Xh92q'’. Using this approach we can save many SOQL queries.

No comments:

Post a Comment