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
LakshmanLakshman 

How do I store a date field in onClick Javascrpt function for a Custom button

Hi All,

 

Below is my script used for a custom button. I am unable to store the date and get error like below:

 faultstring:''0.0001823305154980938' is not a valid value for the type xsd:date', 

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
var connection = sforce.connection;

var ex = new sforce.SObject("CustomF1__c");
ex.DateF1__c = {!DATEVALUE(TEXT(CustomF2__c.DateF2__c))};
ex.Name = "{!CustomF2__c.Name}";

var rs = connection.create([ex]);
if (rs[0].success=='false')
alert("Message"+rs[0].errors.message);

 CustomF2__c is the object where I am creating this button and CustomF1 is the object which I want to create when user hits this button.

Please help me on this. Thanks in advance!

 

Regards,

Lakshman

 

IspitaIspita

Hi Lakshman,

Try the following :-

  • try alerting - {!DATEVALUE(TEXT(CustomF2__c.DateF2__c))}
  • also try alerting - {!DATEVALUE(CustomF2__c.DateF2__c)}
  • The value you get will tell you the cause of the error it seems the function text() is causing the issue.
  • Also ensure that the datatype of  CustomF2__c.DateF2__c and  CustomF1__c.DateF1__c are same

Hope this helps.

Sankalp JhingranSankalp Jhingran

Hi Lakshman,

You should first check the value that is being passed using an alart box.

 

Apparently, ajax toolkit uses API calls in the form of SOAP messages to update data. As per the API docs, salesforce accepts date values in the following format "YYYY-MM-DDThh:mm:ssZ"

 

Add the following function to format the date value as per Salesforce:

 

function fixTime(time){
   if(time < 10) {time = "0" + time};
   return time;
}


function fixDate(date){
  var Month = fixTime(date.getMonth() + 1);
  var Day = fixTime(date.getDate());
  var UTC = date.toUTCString();
  var Time = UTC.substring(UTC.indexOf(':')-2, UTC.indexOf(':')+6);

  var Minutes = fixTime(date.getMinutes());
  var Seconds = fixTime(date.getSeconds());
  return date.getFullYear() + "-" + Month + "-" + Day + "T" + Time;
}

 

You can then use the value as follows:

ex.DateF1__c = fixDate(new Date("{!CustomF2__c.DateF2__c}"));

 

Let me know if that helps and mark it as a solution so that others get benefitted :)

 

Regards,

Sankalp

forcesecrets.blogspot.com 

LakshmanLakshman

I resolved this by querying my date field for that particular record and assigned it to the destination date field record.

I did like below:

 

{!REQUIRESCRIPT("/soap/Ajax/22.0/connection.js")}
var connection = sforce.connection;
results = connection.query("Select DateF2__c from CustomF2__c where Id=\'{!CustomF2__c.Id}\'";
records = result.getArray("records");
//Now create the CustomF1__c
var ex = new sforce.SObject("CustomF1__c");
ex.DateF1__c = records[0].DateF2__c;
ex.Name = "{!CustomF2__c.Name}";
var rs = connection.create([ex]);
if (rs[0].success=='false')
alert("Message"+rs[0].errors.message);

 

BTW in your apporach adding a function in onClick Javascript button, will give an error!!!

 

Regards,

Lakshman