Advertisement
martyychang

SoqlExample

Jan 31st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. /*
  2.  * Example class demonstrating how a hypothetical <code>SoqlQuery</code>
  3.  * class may be used to more consistently construct a dynamic SOQL query
  4.  *
  5.  * @version idea
  6.  * @author  Marty Chang (Slalom Consulting)
  7.  */
  8. public class SoqlExample {
  9.    
  10.     /*
  11.      * Retrieve a list of <code>Part__c</code> records based on known
  12.      * conditions, demonstrating two ways of constructing WHERE clauses
  13.      *
  14.      * @return A list of Parts
  15.      */
  16.     public static List<Part__c> retrievePartsWithSoql() {
  17.  
  18.         // We want to set up a new query to get Part__c records
  19.  
  20.         SoqlQuery query = new SoqlQuery('Part__c');
  21.  
  22.         // Now, we're going to add all of the fields we want
  23.  
  24.         query.select(new Set<String> {
  25.             'Id',
  26.             'Name',
  27.             'Opportunity__c',
  28.             'Opportunity__r.Id',
  29.             'Opportunity__r.Name',
  30.             'Opportunity__r.Reason_for_Forecast_Change__c',
  31.             'Opportunity__r.AccountId',
  32.             'Opportunity__r.Account.Name',
  33.             'Opportunity__r.Account.End_Customer__c',
  34.             'Opportunity__r.SAP_End_Customer__c',
  35.             'Product__c',
  36.             'Product__r.Id',
  37.             'Product__r.Name'
  38.         });
  39.  
  40.         // We also want to select related records via a subquery, to bring
  41.         // in R90_Demand_Forecast__c records related to each Part__c. This
  42.         // means
  43.  
  44.         SoqlQuery subquery = new SoqlQuery('Demand_Forecast__r');
  45.        
  46.         subquery.select(new Set<String> {
  47.             'Id',
  48.             'Part__c',
  49.             'Account__c',
  50.             'R90_Opportunity_ID__c',
  51.             'R90_Forecast_Date__c',
  52.             'R90_Quantity__c',
  53.             'R90_Unit_Price__c',
  54.             'R90_Plan__c'
  55.         });
  56.  
  57.         SoqlConditionExpression isActivePlan = new SoqlConditionExpression(
  58.             'R90_Plan__c',
  59.             Soql.Operator.EQUALS,
  60.             ForecastSettingUtil.getInstance().ActivePlan__c);
  61.  
  62.         SoqlConditionExpression isAfterStartDate = new SoqlConditionExpression(
  63.             'R90_Forecast_Date__c',
  64.             Soql.Operator.GREATER_THAN_OR_EQUALS,
  65.             ForecastSettingUtil.getStartDate());
  66.  
  67.         SoqlConditionExpression isBeforeEndDate = new SoqlConditionExpression(
  68.             'R90_Forecast_Date__c',
  69.             Soql.Operator.LESS_THAN,
  70.             ForecastSettingUtil.getEndDate());
  71.  
  72.         SoqlConditionExpression isInSubqueryScope =
  73.             Soql.and(new Set<SoqlConditionExpression> {
  74.                 isActivePlan, isAfterStartDate, isBeforeEndDate
  75.             });
  76.  
  77.         subquery.where(isInSubqueryScope);
  78.  
  79.         // Now we just select the subquery like we would any regular field
  80.  
  81.         query.select(subquery);
  82.  
  83.         // Next, we want to set up our Part__c filters. We'll show a different
  84.         // way to more efficiently construct the filters without having to
  85.         // instantiate a new expression each time, as long as we build the
  86.         // final expression in order
  87.  
  88.         query.where(
  89.             new SoqlConditionExpression(
  90.                 'Product__r.Forecastable__c',
  91.                 Soql.Operator.EQUALS,
  92.                 true));
  93.  
  94.         query.andWhere(
  95.             new SoqlConditionExpression(
  96.                 'Opportunity__r.RecordTypeId',
  97.                 Soql.Operator.EQUALS,
  98.                 OpportunityUtil.getRecordTypeId(OpportunityUtil.RECORD_TYPE_FORECASTED_OPPORTUNITY)));
  99.  
  100.         query.andWhere(
  101.             new SoqlConditionExpression(
  102.                 'Opportunity__r.Account.RecordTypeId',
  103.                 Soql.Operator.EQUALS,
  104.                 AccountUtil.getRecordTypeId(AccountUtil.RECORD_TYPE_SAP_CUSTOMER)));
  105.  
  106.         query.andWhere(
  107.             new SoqlConditionExpression(
  108.                 'Stage__c',
  109.                 Soql.Operator.IN,
  110.                 ForecastSettingUtil.getForecastablePartStages()));
  111.  
  112.         // Away we go with executing the query!
  113.  
  114.         return Database.query(query);
  115.     }    // public static List<Part__c> retrievePartsWithSoql()
  116. }    // public class SoqlExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement