Chapter 7 Working With the Query Language : Simple Snapshot Query Example

Simple Snapshot Query Example
The following example code could be placed in a preprocessor rule function that receives an event called requestEvent. It includes all steps from creating to closing the query.
The example is simplified for clarity. In a real-world use case, the creation step could be performed in a startup rule function, and the output could be sent in an event to an inference agent or other destination.

 
Query.create("report853", "select agent_name, total_sales, zipcode from /Concepts/Sales");
 
String id = requestEvent@extId;
String stmt = "S" + id;
String rset = "R" + id;
Query.Statement.open("report853", stmt);
Query.Statement.execute(stmt, rset);
while(Query.ResultSet.next(rset)) {
    String agent = Query.ResultSet.get(rset, 0);
    double sales = Query.ResultSet.get(rset, 1);
    String zip = Query.ResultSet.get(rset, 2);
    System.debugOut(rset + ": Agent " + agent
        + " sold $" + sales
        + " in " + zip
        + ".");
}
System.debugOut(rset + ": ========");
Query.ResultSet.close(rset);
Query.Statement.close(stmt);
Query.Close("report853");

 
The last three lines are provided for completeness. However, if the Query.Close() function is used, you would not need to include the Query.ResultSet.close() or Query.Statement.close() functions. See Closing a Statement and Deleting a Query Definition for details about these hierarchical relationships.
Sample Output

 
R123: Agent Mary Smith sold $15063.28 in 94304.
R123: Agent Robert Jones sold $14983.05 in 94304.
R123: ========