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
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7 

Formula to SOQL Query

Hi Everyone,

I need to convert opportunity formula into SOQL Query. Can anyone help me on this.

My Opportunity formula is,

AND(
  AND(
       ISBLANK(Last_Stage_Change__c),
       NOW() - LastModifiedDate > 30,
       Probability < 0.91,
       Probability > 0.01
      ),
   AND(
       NOW() - Last_Stage_Change__c  > 30,
       Probability < 0.91,
       Probability > 0.01
      )
  )

My query is that,


select Id,StageName,LastModifiedDate from Opportunity Where "Formula Condition"

Thanks in advance.
 
Arunkumar RArunkumar R
Hi Selva,

I would like to suggest create two formula fields,

In FormulaField1__c use the below formula with return type number,
NOW() - LastModifiedDate
In FormulaField2__c use the below formula with return type number,
NOW() - Last_Stage_Change__c

Build the SOQL query like below,
 
select Id,StageName,LastModifiedDate from Opportunity Where ( (Last_Stage_Change__c = NULL AND FormulaField1__c > 30 AND Probability < 0.91 AND Probability > 0.01) OR (FormulaField2__c > 30 AND Probability < 0.91 AND Probability > 0.01))

If this information helps, mark this as solution.

Thanks.