SOQL Query to Group By Month and Year
SELECT Count(Id), Calendar_Year(CloseDate), Calendar_Month(CloseDate) FROM Opportunity WHERE Stage = 'Closed Won' GROUP BY Calendar_Year(CloseDate), Calendar_Month(CloseDate) ORDER BY Count(Id) DESC
SELECT Count(Id), Calendar_Year(CloseDate), Calendar_Month(CloseDate) FROM Opportunity WHERE Stage = 'Closed Won' GROUP BY Calendar_Year(CloseDate), Calendar_Month(CloseDate) ORDER BY Count(Id) DESC
The INCLUDES
and EXCLUDES
operators are used to filter the multiselect picklist field. The multiselect picklist field in Salesforce allows the user to select more than one value from the list of values provided.
There are use-cases where we want to use a SOQL query and filter by multi-select picklist values. The picklist values can be specified with the AND/OR logic.
We use the Semicolon(;) and Comma(,) characters to add filter values to multi-select picklist fields. A semicolon is used as a special character to specify AND. A comma is used as a special character to specify OR.
Here is the example of filtering the multiselect picklist values. I’ve a custom object “Mobile__c” multiselect picklist field “Brand__c”. The values are in this field are Apple, Samsung, Nokia and HTC.
Select Id, Name From Mobile__c Where Brand__c INCLUDES('Apple;Samsung');
Select Id, Name From Mobile__c Where Brand__c INCLUDES('Apple;Samsung','HTC');
Select Id, Name From Mobile__c Where Brand__c INCLUDES('Samsung', 'Nokia','HTC');
Using UPDATE keyword in SOQL helps us to lock the records from being updating from another request.
Sample Code:
Account [] objAccts = [SELECT Id, Name FROM Account where Country='India' FOR UPDATE];
Note: We can’t use the ORDER BY
keywords in any SOQL query that uses locking.
String name = [SELECT Name FROM Account].Name;
Sample Code:
public class DynamicSOQL { public static sObject getAllFieldsData(String objTypeName) { sObject sObj; Schema.DescribeSObjectResult[] descSObj = Schema.describeSObjects(new String[]{objTypeName}); Map<String, Schema.SObjectField> fieldMap = descSObj[0].fields.getMap(); List<String> fieldNames = new List<String>(fieldMap.keySet()); system.debug('Fields-' + String.join(fieldNames, ',')); String queryString = 'SELECT '+ String.join(fieldNames, ',') +' FROM '+ objTypeName +' LIMIT 1'; sObj = Database.query(queryString); return sObj; } }