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
S MS M 

Using LAST_N_DAYS: with an Integer variable

I need to have a select Query in my Apex method that has this line - 

AND Pending_Agreement_DateStamp__c <= LAST_N_DAYS:pendingDays

The pendingDays in an Integer value that is passed to this method
I tried using Sting.valueOf or Integer.valueOf but they dont seem to work. Please let me know what I should use there?
Raj VakatiRaj Vakati
You need to do it like this i guess 
 
Date startDate = Date.today().addDays(-Integer.valueOf(pendingDays));
AND Pending_Agreement_DateStamp__c <= :startDate];

 
S MS M
Hi Raj,

I still get the error Missing INTEGER_LITERAL at startDate
and expecting a Number, found startDate as errors
Any work arounds?
Raj VakatiRaj Vakati
can u give me complete code ?
Deepali KulshresthaDeepali Kulshrestha
Hi S M,

Please go through with below formula, it may be helpful to you.

Date startDate = Date.today().addDays(-Integer.valueOf(numDays));
List<Account> accs = [select id from account where createddate >= :startDate];

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Meer ZamanMeer Zaman

Hi S M

You can use Database.query(soql) method instead of SOQL,  no need to use '<= '. see code for details 

Integer pendingDays = 10;
String soql = 'SELECT Id, Name FROM Account '+
              'WHERE Pending_Agreement_DateStamp__c = LAST_N_DAYS:'+pendingDays;
for(Account account :Database.query(soql))
{
    //you code here 
}


please let me know if you are still facing any issue 

 

Thanks,
Meer Zaman Khattak