You need to sign in to do that
Don't have an account?

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
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
check how to compare dates in javascript
http://stackoverflow.com/questions/338463/how-do-i-do-a-date-comparison-in-javascript
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?
have a look at this article, your problem will solve
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
var CloseDate, January, February;
Hope this helps...
thanks. I'm getting "unexpected token if" message so it would be probably something small with the condition statement.
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.