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
Steven BensonSteven Benson 

addMonths sytax error

HI guys im trying to create a forumla on a VP page that shows an image based on a date

value="{!If(Account.LatestCompaintDate__c = System.now().addMonths(-6), $Resource.xxx,"")}"

but for some reason i get a missing ')' error

any help would be great!
Best Answer chosen by Steven Benson
SravsSravs
HI Steven, 

Sorry I miss this earlier,

We can not use addMonths() method in Visualforce. 
  1. We can use an approximation such as {!If((Account.LatestCompaintDate__c <= (now() - 180)), 'true','false')}
  2. if the page has a contoller / extenction we can calculate the date in the class and compare it in the page. 

All Answers

SravsSravs
hi Steven, 

Try this. :)
value="{!If(Account.LatestCompaintDate__c = System.now().addMonths(-6), $Resource.xxx,'')}"

Just chnged to singlequotes in if method. 
James LoghryJames Loghry
Your statement will evaluate to true only if LatestCompaintDate__c is exactly 6 months out.  If you want something else.. such as all LatestCompaintDate__c values in the past 6 months or LatestCompaintDate__c values that are at least 6 months old, then you'll want to do a < comparison.  If you want all LatestCompaintDate__c values from 6 months ago, then you'll need to add additional checking around the month and the year of the LatestCompaintDate field.  My point being is that date checking isn't always as straight forward as you may think it is.
Steven BensonSteven Benson
hi
sorry i changed to single quotes and i still get the error!

james, yes sorry typing error i am acutally using <=

thanks
James LoghryJames Loghry
Steven,

System.now() is a method defined in Apex, but it doesn't translate to Visualforce's NOW() function well.  See more on the NOW() Visualforce function here: https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_functions.htm

Y
our next best bet is to create a variable in your Apex class / controller that does the Date calculation, and then display that variable in your value attribute above.
Steven BensonSteven Benson
strange, I can get now to work fine its just when i seem to add the .addmonths if doesnt!
SravsSravs
HI Steven, 

Sorry I miss this earlier,

We can not use addMonths() method in Visualforce. 
  1. We can use an approximation such as {!If((Account.LatestCompaintDate__c <= (now() - 180)), 'true','false')}
  2. if the page has a contoller / extenction we can calculate the date in the class and compare it in the page. 
This was selected as the best answer