SOQL GROUP BY ROLLUP Clause

In our previous Salesforce Tutorial we have learned about some advanced SOQL statements in salesforce. In this salesforce Training Tutorial we are going to learn about SOQL GROUP BY ROLLUP Clause, FOR REFERENCE Clause and FOR VIEW Clause. Each of these clauses allows for unique data insights, whether it’s generating subtotals, tracking record references, or viewing history.


What is the SOQL GROUP BY ROLLUP Clause?

The GROUP BY ROLLUP clause in SOQL is a powerful tool for generating subtotals and grouped data summaries in query results. Similar to the regular GROUP BY clause, GROUP BY ROLLUP goes further by providing multiple levels of subtotal rows. This feature is particularly useful when working with hierarchical data where insights at each level are required. With GROUP BY ROLLUP, you can add up to three fields in a comma-separated list to create these subtotals.

Example:

SELECT City__c, State__c, COUNT(Employee_Name__C) Counts 
FROM Employee__C 
GROUP BY ROLLUP (City__c, State__C)

In this example, the query groups data first by State__c and then by City__c. It also calculates the count of Employee_Name__C for each group, providing totals for each City within a State as well as a grand total for the entire dataset. This helps in analyzing data by city and state, while also seeing cumulative numbers.

Example Output:

When you execute this query, you will see subtotals for each city within a state, and then a grand total row for each state. This hierarchical grouping can reveal patterns in your data and give quick aggregate insights.


SOQL FOR REFERENCE Clause

The FOR REFERENCE clause is used to track the date and time a record was last referenced in Salesforce. When you retrieve a record with this clause, it updates the LastReferencedDate field automatically, reflecting when the record was accessed. This is useful for tracking user interactions with records and identifying which records are frequently accessed.

Example:

SELECT City__c, State__c, LastReferencedDate 
FROM Employee__C 
FOR REFERENCE

In this example, each time a record from the Employee__C object is retrieved using this query, Salesforce updates the LastReferencedDate field to the current date and time. This feature can be leveraged to monitor which records are regularly referenced, providing insights into usage patterns.

Example Output:

Upon running this query, you will see LastReferencedDate for each record. If the query is executed multiple times, the LastReferencedDate for all retrieved records will be updated to the most recent access time.


SOQL FOR VIEW Clause

The FOR VIEW clause allows you to track when a record was last viewed. Salesforce records contain a standard field called LastViewedDate, which records the last time the record was viewed in any capacity. The FOR VIEW clause updates this field automatically, making it ideal for tracking record views and understanding user interaction with specific records.

Example:

SELECT City__c, State__c, LastViewedDate 
FROM Employee__C 
FOR VIEW

In this example, the query retrieves records from Employee__C and updates the LastViewedDate field for each record. The first time you run this query, LastViewedDate reflects when each record was viewed. Upon subsequent executions, the field is updated to the latest view time for all retrieved records.

Example Output:

The LastViewedDate field shows when each record was last accessed. If you run the query multiple times, LastViewedDate will reflect the most recent query execution. This can help administrators identify which records are actively being viewed over time.


Summary of SOQL Clauses

ClausePurposeExample
GROUP BY ROLLUPAdds multiple levels of subtotals for grouped data in query results.SELECT City__c, State__c, COUNT(Employee_Name__C) Counts FROM Employee__C GROUP BY ROLLUP (City__c, State__C)
FOR REFERENCETracks the last referenced date of a record, updating LastReferencedDate when accessed.SELECT City__c, State__c, LastReferencedDate FROM Employee__C FOR REFERENCE
FOR VIEWTracks the last viewed date of a record, updating LastViewedDate when accessed.SELECT City__c, State__c, LastViewedDate FROM Employee__C FOR VIEW

Each of these clauses serves a specific purpose in Salesforce SOQL, allowing you to generate deeper insights from your data, track record interaction, and manage records more effectively. Use these clauses to enhance your data analysis capabilities in Salesforce and create meaningful reports and insights.