+ Start a Discussion
Martin VyskocilMartin Vyskocil 

IF condition on OnClick JavaScript

Hey folks,

I've just started with JS and need a little bit of help..
Need to write a condition evaluating Opportunity Close Date for monthly fields.
Ie.
for Jan:  IF Jan-15-2015 > CloseDate  THEN {!Opportunity.Revenue__c} ELSE null
for Feb:  IF Feb-15-2015 > CloseDate THEN {!Opportunity.Revenue__c} ELSE null
and so on till Dec

Could you please advise what is wrong with the code below?
---------------------------
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var c = new sforce.SObject("IYNB__c"); 
c.id = "{!IYNB__c.Id}"; 
c.Jan_FC__c = IF(DATE(year(Opportunity.CloseDate),1,15) > DATE(year(Opportunity.CloseDate),month(Opportunity.CloseDate),day(Opportunity.CloseDate)) {'{!Opportunity.Revenue__c}'}
else {null};
c.Feb_FC__c = IF(DATE(year(Opportunity.CloseDate),2,15) > DATE(year(Opportunity.CloseDate),month(Opportunity.CloseDate),day(Opportunity.CloseDate)) {'{!Opportunity.Revenue__c}'} 
else {null};

result = sforce.connection.update([c]); 
window.location.reload()
----------------------------------
Thanks in advance,
Martin
BalajiRanganathanBalajiRanganathan
You are mixing Javascript syntax with SFDC Formula syntax. ie DATE(2014,2,15) funciton is not an javascript function. it can be used only in validation rules and formula.

check how to compare dates in javascript

http://stackoverflow.com/questions/338463/how-do-i-do-a-date-comparison-in-javascript
Dushyant SonwarDushyant Sonwar
Hi Martin,
c.Jan_FC__c ='{!If(Date(year(Opportunity.CloseDate),1,15)>Opportunity.CloseDate,Opportunity.Revenue__c,Opportunity.CloseDate)}';
c.Feb_FC__c='{!If(Date(year(Opportunity.CloseDate),2,15)>Opportunity.CloseDate,Opportunity.Revenue__c,Opportunity.CloseDate)}';

Are you trying to do something like this?
 
Waqar Hussain SFWaqar Hussain SF
http://www.w3schools.com/js/js_date_methods.asp

have a look at this article, your problem will solve
Martin VyskocilMartin Vyskocil
Thanks all for your hints. I think I'm a step closer but still missing something.
The below is updated script 
----------------------------------------------------------
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var CloseDate, January, February
var c = new sforce.SObject("IYNB__c");

CloseDate = new Date('{!Opportunity.CloseDate}');
January = new Date();
January.setFullYear(2015, 0, 15);
February = new Date();
February.setFullYear(2015, 1, 15);
 
c.id = "{!IYNB__c.Id}"; 
c.Jan_FC__c = if (CloseDate > January){null;} else '{!Opportunity.Revenue__c}';
c.Feb_FC__c = if (CloseDate > February){null;} else '{!Opportunity.Revenue__c}';

result = sforce.connection.update([c]); 
window.location.reload()
-------------------------------------
Would you help please?
Thanks in advance
Dushyant SonwarDushyant Sonwar
I think you are missing semicolon in the above script

var CloseDate, January, February;
Hope this helps...
Martin VyskocilMartin Vyskocil
Dushyant,
thanks. I'm getting "unexpected token if" message so it would be probably something small with the condition statement.
Dushyant SonwarDushyant Sonwar
Hey Martin,
I think you should change this line
c.Jan_FC__c = if (CloseDate > January){null;} else '{!Opportunity.Revenue__c}';
c.Feb_FC__c = if (CloseDate > February){null;} else '{!Opportunity.Revenue__c}';

replace with this
if (CloseDate > January){c.Jan_FC__c = null;} else c.Jan_FC__c ='{!Opportunity.Revenue__c}';
c.Feb_FC__c = if (CloseDate > February){c.Feb_FC__c=null;} else c.Feb_FC__c='{!Opportunity.Revenue__c}';

and
semicolon missing
window.location.reload();

=======
Hope this helped you out.