function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Date Check in a batch apex

Hi All,


 I have written a query in a batch apex. It was working correctlt until i added

 Date_Opened__c>(today -(5*Average_Sales_Cycle__c)).   It is giving an error  "First error: unexpected token: '(' " when i run the batch apex.


Please let me know what is the right way to query a date field (Date_Opened__c) which meet the above criteria.


q= 'Select id, name,StageVFlag__c, StageBFlag__c,StageAFlag__c,StageEFlag__c,StageCFlag__c,StageGFlag__c,ownerid, status__c, type, amount,  from opportunity where  (amount>=0.00 and amount<=100000.00) and Date_Opened__c>(today -(5*Average_Sales_Cycle__c)) and type =\'Non-Recurring\'';}




I'd say that this is because you are attempting to use a field on the right hand side of the field comparison.  According to the docs:


--- snip ---


You must supply a native value—other field names or calculations are not permitted


--- snip ---


I've worked around this kind of thing by creating a formula field that encapsulates the condition, and I just then query all records where the formula evaluates to true.


HI ,


I guess you need to use a date variable  & use it in Query.


Steps should like


1.take a integer variable to store your calculation say numDay  = (5*Average_Sales_Cycle__c).

2. create a date variable initialise to today say Date tod =;

3. Now create one more variable Date beforeDate  = tod.addDays(-numDay);

3. Now use this beforeDate in Query like


q= 'Select id, name,StageVFlag__c, StageBFlag__c,StageAFlag__c,StageEFlag__c,StageCFl​ag__c,StageGFlag__c,ownerid, status__c, type, amount,  from opportunity where  (amount>=0.00 and amount<=100000.00) and Date_Opened__c>:beforeDate and type =\'Non-Recurring\'';}


Let me know if this works ...