I have a batch class where would like to add few condition in the query,how to add them.Any Suggestion please.

1)I would like to add more roles (such as EB ,TA ,RA etc where role is a picklist field) in this query .

SELECT  Contract__c FROM Contract_role__c WHERE Role__c = 'SA'

2)In getquerylocator i would like to add few condition such as

When "Renewed" is Checked AND

When "SRR" is YES AND  

when the "Status Renewed" is equal to "Status renewed for next quarter " or "Pipeline" AND 

when "Status Renewed next Year" is not equal to "Renewed" or"Renewed lost" 

i have the query as

return Database.getQueryLocator('Select id, Contract_Name__c , EndDate ,ownerId FROM Contract WHERE Id IN: setContractIds');

Any Suggestion please.

  • What problem are you experiencing when you add those conditions to your Where clause?
    – Doug B
    Apr 18 '16 at 12:59
  • @ Doug B:Thanks for your response.When i use the above get query locator ,i get the email notification.But when i start adding the condition to the query as return Database.getQueryLocator([Select id, Contract_Name__c , EndDate ,Status Renewed FROM Contract where Status Renewed IN ('Status renewed for next quarter','Pipeline') OR Status Renewed next Year IN ('Renewed','Renewed lost')]) , the code gets saved when i execute ,i dont get any email notification.Any help very much appreciated. Apr 18 '16 at 13:38
  • Status Renewed isn't a proper field name. Is that a custom field or is that a custom picklist value for another field? Your fields and values are unclear. What does your debug log show? Try adding debug statements to your batch class.
    – crmprogdev
    Apr 18 '16 at 13:48
  • @crmprogdev,@cropredy:Status_Renewed__c and Status_Renewed_next_Year__c are the custom picklist value.I tried using this query return Database.getQueryLocator([Select id, Contract_Name__c , EndDate ,Status_Renewed__c FROM Contract where Status_Renewed__c IN ('Status renewed for next quarter','Pipeline') AND Status_Renewed_next_Year__c NOT IN ('Renewed','Renewed lost')]);it works I have a formual field as "Renewable" on contract object and this field is used only for few products which gets renewed.Now how can i query this formula field and make it checked in the record. Apr 19 '16 at 11:28
  • @crmprogdev,@cropredy:For Example : The Scenario is when the renewable product is checked ,then only the email notification is to be sent before 100 days of the contract end date.When i create an record i dont find the field on the edit page (as we know its an read only field) but how to make it checked .Do i need to do some field update or any formula where the value gets updated.Any suggestion please. Apr 19 '16 at 11:29

A typical start() method query locator would look like

return Database.getQueryLocator([select contract__c, ... from Contract
                                 where role__c IN('role1','role2', ...) and
                                       renewed__c = true and
                                       srr__c = 'Yes' and
                                       status_renewed__c IN (
                                            'Status renewed for next quarter',
                                            'Pipeline') and  
                                       Status_Renewed_next_Year__c NOT IN (
                                            'Renewed','Renewed Lost'))];
  • :Thanks for the reply.I tried using the query as suggested .The sysetm throws an error as expecting a semi-colon, found '[' at line 14 .Any suggestion please. Apr 19 '16 at 7:11
  • Sorry typo. Edited answer
    – cropredy
    Apr 19 '16 at 13:57

You can pass parameters to a batch class by setting some property values.

public class SomeBatch implements Database.Batachable
  public String someFilter {get; set;}

  global List<sObject> start(Database.BatchableContext bc)
    String queryStr = 'select Id from Object where SomeField = 'XYZ';

    if(someFilter != '')
      queryStr += ' and SomeOtherField = : someFilter';

    return Database.query(queryStr);

  // etc.

Then you can invoke this batch like so:

SomeBatch b = new SomeBatch();
b.someFilter = 'FilterByThis';


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.