I am having an issue with a button that converts a Deal Object to an Opportunity Object.

The field I am using from Deal is '{!Deal_Registration__c.Close_Date__c}' which in this format returns 22/10/2014 .

The code I use is :


    var dealCloseDate = new Date ('{!Deal_Registration__c.Close_Date__c}');
    newOpportunity.CloseDate =dealCloseDate;

When putting this in a date variable I get the alert

invalid date

and the error:

{faultcode:'soapenv:Client', faultstring:''NaN-NaN-NaNTNaN:NaN:NaN.NaN-NaN:NaN' is not a valid value for the type xsd:date', }

I have found a way around this by doing a substring and getting the month day year and setting a new date this way.. .but it seems a little overkill.

Does anyone have a suggestion on the proper way of getting this bit of code to work?

P.S. when looking at the field setup they both show as Date and not Datetime..

  • what's the output of alert('{!Deal_Registration__c.Close_Date__c}'); ?
    – highfive
    Jul 24 '14 at 12:34
  • The output is 22/10/2014
    – jnoel10
    Jul 24 '14 at 12:35

It is best to avoid any dependency on date formats. One way to do that is this:

var dealCloseDate = new Date({!YEAR(Deal_Registration__c.Close_Date__c)}, {!MONTH(Deal_Registration__c.Close_Date__c)} - 1, {!DAY(Deal_Registration__c.Close_Date__c)});

i.e. using the JavaScript date constructor that takes year/month/day. Note that the months are counted from 0 not 1 in JavaScript but from 1 in Salesforce.

  • Hi Keith, thanks for the answer, this is the current method that I am using to get the proper date.. it Just seems weird that I would need to do this when I am just copying one date field to another
    – jnoel10
    Jul 24 '14 at 12:54
  • 1
    @jnoel10 Dates are often awkward because there are so many representations: strings in various formats (that vary per user) that are really meant for end users, milliseconds since 1/1/1970 best for calculations, day/month/year in various orders. And Datetimes are even worse because then timezones are involved too.
    – Keith C
    Jul 24 '14 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.