In this second post about using Google Charts to visualize Salesforce data, I’ll show how to use the AJAX Toolkit to query data in related objects and display a summary in a chart. In the first example, I created a column chart with data from custom fields on the Account. In this next example, let’s assume we have an object called AccountHistory which has the sales by Product and Account. On the Account page layout, we want to be able to see a summary of sales by Product Family.
In order to show this, we need to run a SOQL query summarizing sales by Product Family. I’ll take advantage of the AJAX Toolkit to query the data without having to use a custom controller. Using the AJAX toolkit is pretty simple to query data. First we query data using SOQL and then iterate over the results, adding each record returned to the chart.
Once we have the Visualforce page created, it can be added to an Account page layout. Remember to make the height the same as defined on the page (in this case 300 px). The result should look something like this:
I’ve updated my unmanaged package to include this example. It includes the custom object and Visualforce page as well as the example from my previous post. You can install it into your org by clicking on the following link: https://login.salesforce.com/packaging/installPackage.apexp?p0=04t80000000xviT. Make sure you have some products with Product Families assigned and then populate some Account History records. Add the new Visualforce page to an existing page layout and you should be up and running.
Obviously, this is a very simple example, but I hope it illustrates how this can be a great addition to any page layout.