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
Chad.PfrommerChad.Pfrommer 

Class using "THIS_FISCAL_YEAR" not compiling in one particular org.

Salesforce provides date literals you can use in your SOQL queries.  Here's their documentation - see page 20:

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm

When I run:
SELECT Id FROM Opportunity WHERE CloseDate = THIS_FISCAL_YEAR
through Dev console in my prod org, I get an invalid date error.  When I run queries in the same org using other date literals, like the example queries from the docs, they work fine.  That query executed in a partial sandbox doesn't work either.  That query executed in my own developer org works fine.

Anyone else ever experienced this?

There is an apex class in production that is using that query - and it's currently running fine.  Unfortunately, when we try to push changesets to prod and run all our unit tests, we get compilation errors on that class.  Our current workaround is to select specific unit tests to run when deploying to prod, however this solution is far less than ideal, and won't help if we need to make changes to that class.
Best Answer chosen by Chad.Pfrommer
Chad.PfrommerChad.Pfrommer
Okay - I just figured this out.  The Orgs where I was having the issue had custom fiscal years defined, and the current date didn't fall within any of those custom fiscal years.  I added a new custom fiscal year and the query now works.

All Answers

GauravGargGauravGarg
Hi Chad.

I have being stuck in same error earlier. In Apex class we need to use reference ":"  try adding this after "=" and before date literal.
 
SELECT Id FROM Opportunity WHERE CloseDate = : THIS_FISCAL_YEAR

Hope this helps!!!

Thanks,
Gaurav
Skype:gaurav62990
Takes Freelance Job
Chad.PfrommerChad.Pfrommer
No luck Gaurav.  When I do that I get "Variable does not exist: THIS_FISCAL_YEAR"
GauravGargGauravGarg
Hi Chad,

This line works perfectly in my org:
List<Opportunity> oppList = [SELECT Id FROM Opportunity WHERE CloseDate = THIS_FISCAL_YEAR];


Try luck with above code. 

Thanks,

Gaurav

Chad.PfrommerChad.Pfrommer
What instance are you on?  If you have access to an org on cs67, cs9, or na23 I'd be curious to see if you that query/code works for you on one any of those. 
Chad.PfrommerChad.Pfrommer
I just found another sandbox on cs9 to which I have access, and that code/query works fine there.

There must be some Org specific setting that is causing this issue.

 
GauravGargGauravGarg
Is it possible you could provide me access over your org, so that I could analyse the issue over there. 
Chad.PfrommerChad.Pfrommer
Okay - I just figured this out.  The Orgs where I was having the issue had custom fiscal years defined, and the current date didn't fall within any of those custom fiscal years.  I added a new custom fiscal year and the query now works.
This was selected as the best answer
Chad.PfrommerChad.Pfrommer
Thanks for taking the time to back and forth with me on this, Gaurav!
GauravGargGauravGarg
No Problem