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
venkateshvenkatesh 

Error in SOQL query!!!!!!!!!

 

 Hi,

 

  Please help me to resolve the query error. :smileysad:

 

 String purgeType = String.valueOf(bs.Purging_Type__c);
            if(bs.Purging_Type__c == 'LAST_N_DAYS') {
                 Integer nDays = Integer.valueOf(bs.Last_n_days__c);
            }
     if (purgeType == 'LAST_N_DAYS') {
                 List<ConnectCompiere_Log__c> lastNDays = new List<ConnectCompiere_Log__c>([SELECT Id FROM ConnectCompiere_Log__c WHERE CreatedDate = LAST_N_DAYS : nDays]);
                 for(ConnectCompiere_Log__c rec:lastNDays) {
                      delete rec;
                 }

 

I am getting Error: Compile Error: expecting a number, found 'nDays' at above marked line.

 

How i can use custom varaiable(like nDays) instaed of using some number in place of nDays. Values for nDay, I will be getting from front end as Integer values.

 

Please help me. 

 

Thnks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
WhyserWhyser

Actually, this is an interesting problem...

The regular SOQL syntax would be something like,

 

LAST_N_DAYS : n

 

Where n would be a number.

 

I've tried this in APEX, and one would think that the syntax would be LAST_N_DAYS : :nDays

However, compiling this will produce an error, because it's expecting a number.

 

I would also like to know the solution on how to overcome this SOQL syntax error.

 

The above suggestion should work though, where you say CreatedDate > :System.now().addDays( -nDays )

All Answers

Scott.MScott.M

Hi,

 

Couldn't you do something like the following

 

 

[SELECT Id FROM ConnectCompiere_Log__c WHERE CreatedDate >: System.Date().addDays(-nDays)]

 

Cheers,

Scott

 

HarmpieHarmpie
the : should be attached to the nDays .... :nDays, not : nDays
WhyserWhyser

Actually, this is an interesting problem...

The regular SOQL syntax would be something like,

 

LAST_N_DAYS : n

 

Where n would be a number.

 

I've tried this in APEX, and one would think that the syntax would be LAST_N_DAYS : :nDays

However, compiling this will produce an error, because it's expecting a number.

 

I would also like to know the solution on how to overcome this SOQL syntax error.

 

The above suggestion should work though, where you say CreatedDate > :System.now().addDays( -nDays )

This was selected as the best answer
venkateshvenkatesh

Hello Whyser,

 

 You did it. You are exactly correct.  Great Idea....

 

I am very thankful to you and all.

 

:smileytongue: :smileytongue: