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
selva kumar 14selva kumar 14 

Urgent need for SOQL Query

Hi Everyone,

I need to convert this formula to SOQL Query. I have tried in many ways. But it shows parsing error.
Can anyone write me a SOQL Query for this. It would be great help to me.

AND (
  AND(
       ISBLANK(Last_Stage_Change__c),
       NOW() - LastModifiedDate > 30,
       Probability < 91,
       Probability > 1
      ),
   AND(
       NOW() - Last_Stage_Change__c  > 30,
       Probability < 91,
       Probability > 1
      )
  )



Please help. Thanks in advance.

 
pconpcon
Unfortunately there is no way via SOQL to get the granularity you want for NOW() - LastModifiedDate or NOW() - Last_Stage_Change__c

To accomplish this via a query you'd need to add two new formula fields:

TimeSinceLastModified__c: NOW() - LastModifiedDate
TimeSinceLastStageChange__c: NOW() - Last_Stage_Change__c

Once you have done this then you can execute the following SOQL query (This is against best practices [1], your best option is to use dynamic SOQL [2] and generate the DateTime you are expecting these fields to be greater than)
 
select Id
from MyObject__c
where Last_Stage_Change__c == null and
    TimeSinceLastModified__c > 30 and
    TimeSinceLastStageChange__c > 30 and
    Probability < 91 and
    Probability > 1

[1] https://developer.salesforce.com/blogs/engineering/2013/02/force-com-soql-best-practices-nulls-and-formula-fields.html
[2] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm
selva kumar 14selva kumar 14
Hi pcon,

Thanks for your effort.

But still throws me parsing error on TimeSinceLastModified__c field.

Can any other way to acheive this.
 
pconpcon
What kind of parsing error are you getting?