{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var accountid= "{!Account.Id}";
var nowDate = new Date();
var d= (nowDate.getFullYear()-1)+'-'+(nowDate.getMonth()+1)+'-'+nowDate.getDate();
alert(d);
var RelatedOpps= sforce.connection.query("SELECT Id,Cohort_Date_Value__c,StageName from Opportunity WHERE Accountid ='{!Account.Id}' AND Cohort_Date_Value__c<"+d);
if(RelatedOpps==null){
alert("No Opps "+RelatedOpps);
}
else {
alert("List of Opps "+RelatedOpps);
}
What I am doing: I am trying to query the Opportunity records based on Cohort_Date_Value__c value. Trying to query records where Cohort_Date_Value__c<(Today()-1 year)
Cohort_Date_Value__c: Datatype: Formula(Date): Ex:2017-02-04 d: Ex:2017-2-4
Any idea how to compare these dates?
UPDATE: (Working)
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var accountid= "{!Account.Id}";
var todaysDate = new Date();
function convertDate(date) {
var yyyy = (date.getFullYear()-1).toString();
var mm = (date.getMonth()+1).toString();
var dd = date.getDate().toString();
var mmChars = mm.split('');
var ddChars = dd.split('');
return yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]);
}
var d = convertDate(todaysDate);
alert(d);
var RelatedOpps= sforce.connection.query("SELECT Id,Cohort_Date_Value__c,StageName from Opportunity WHERE Accountid ='{!Account.Id}' AND Cohort_Date_Value__c < " + d);
if(RelatedOpps==null){
alert("No Opps "+RelatedOpps);
}
else {
alert("List of Opps "+RelatedOpps);
records = RelatedOpps.getArray("records");
for (var i=0; i< records.length; i++) {
var record = records[i];
alert("Opportunity name"+ record.Name + "Contact Id -- " + record.Cohort_Date_Value__c);
}
}