Saturday, October 13, 2012

Salesforce Sub Queries created by the Force IDE Schema Browser


Today my goal was to finally begin to understand Salesforce Sub queries which offer a very powerful way to fold multiple queries into one single query which helps reduce the number of SOQL queries in any given transaction so as to avoid bumping into the SOQL query limit. I finally figured out how to use the Force IDE schema browser to write queries. Once I had working queries, I started understanding what I was looking at.

So: how do we create a query that has a sub query with the Force IDE schema browser?

  1. Fire up the Force IDE. In my case, I start the Eclipse IDE with the Force IDE plugin installed, drill down to "salesforce.schema" in a Salesforce project and double click on it.
  2. The Opportunity object tends to be the master of many other objects and is a good object to start looking at. On the right hand side of the dialog under "Schema" find "Opportunity" and click on the triangle icon to expand. Next drill down into "Child Relationships."
  3. Find a child like "Note" and expand that drilling down to "Fields" and select one like "Note Id."
  4. Under "Query Results" to the left of the "Schema" window you've been working on, you'll see a query like:
    1. Select id,name, (Select Id From Notes) From Opportunity o
  5. Here is a query that accesses the Opportunity and three other detail objects for which Opportunity is the master:
    1. Select id, name, (Select Role From Partners), (Select CreatedDate From OpenActivities), (Select Id From Notes) From Opportunity o

If you're writing some apex code to access such queries, the sub queries become lists, so you might write something like this for the above example with notes and opportunity objects:

for ( Opportunity o: [Select id,name, (Select title From Notes) From Opportunity] ) {
    for ( Note n: o.Notes ) {
        System.debug(n.title);
    }
}

The schema browser offers a powerful way to start writing SOQL queries and create useful examples to work from.

Wednesday, October 10, 2012

Salesforce Sub Queries created by the Force IDE Schema Browser

Salesforce Sub Queries created by the Force IDE Schema Browser

Today my goal was to finally begin to understand Salesforce Sub queries which offer a very powerful way to fold multiple queries into one single query which helps reduce the number of SOQL queries in any given transaction so as to avoid bumping into the SOQL query limit. I finally figured out how to use the Force IDE schema browser to write queries. Once I had working queries, I started understanding what I was looking at.

So: how do we create a query that has a sub query with the Force IDE schema browser?
1.     Fire up the Force IDE. In my case, I start the Eclipse IDE with the Force IDE plugin installed, drill down to "salesforce.schema" in a Salesforce project and double click on it.
2.     The Opportunity object tends to be the master of many other objects and is a good object to start looking at. On the right hand side of the dialog under "Schema" find "Opportunity" and click on the triangle icon to expand. Next drill down into "Child Relationships."
3.     Find a child like "Note" and expand that drilling down to "Fields" and select one like "Note Id."
4.     Under "Query Results" to the left of the "Schema" window you've been working on, you'll see a query like:
1.     Select id,name, (Select Id From Notes) From Opportunity o
5.     Here is a query that accesses the Opportunity and three other detail objects for which Opportunity is the master:
1.     Select id, name, (Select Role From Partners), (Select CreatedDate From OpenActivities), (Select Id From Notes) From Opportunity o

The schema browser offers a powerful way to start writing SOQL queries and create good examples to work from.