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
Kenji775Kenji775 

Javascript date object to Apex datetime, apex remoting

Hey all,

Just a quick simple question.

Trying to update an object through Apex remoting. One of the fields I am attempting to update is a date time. In what format do I need to pass a datetime for it to work? I've tried just passing a date object, and date.toUTCString(). How do I need to format the datetime in javascript for apex to be able to deal with it? Thanks!

 

 

 

 

Kenji775Kenji775

The exact error is

Visualforce Remoting Exception: Upsert failed. First exception on row 0 with id a06S0000003yxF8IAI; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, Start Date: value not of required type: 2012-02-03T14:46:00Z: [Start_Date__c]

and here is the javascript method I am using to create and send the object with the date to my method.

 

function rescheduleEvent(event,dayDelta,minuteDelta,allDay)
{
	console.log(event);
	var addSeconds = ((dayDelta * 86400) + (minuteDelta * 3600 ) + minuteDelta) * 1000;      
	
	var updateObject = new Object();        
	updateObject.id = event.detailData.id;
	updateObject.start_date__c = toSFDate(new Date(parseInt(event.eventStartEpoch*1000+addSeconds,10)));
	updateObject.end_date__c = toSFDate(new Date(parseInt(event.eventEndEpoch*1000+addSeconds,10)));


	fullCalendarController.saveObject(updateObject, function(result, event)
	{
		if(event.status)
		{                        
			console.log(result);     

			/*
			Error gets returned.
	
			*/      
		}
	}, {escape:true});           
}

function toSFDate(dateObj)
{
	// string format is YYYY-MM-DDThh:mm:ssZ           
	var dateStr = dateObj.getFullYear()+'-'+pad2(dateObj.getMonth()+1)+'-'+pad2(dateObj.getDate()) +'T'+pad2(dateObj.getHours())+':'+pad2(dateObj.getMinutes())+':'+pad2(dateObj.getSeconds())+'Z';
	return dateStr;
}

function pad2(number) 
{      
	 return (number < 10 ? '0' : '') + number       
}

 

And not that it really matters, but this is my controller method.

 

@RemoteAction
global static remoteObject saveObject(sObject saveObject)
{
    remoteObject returnObject = new remoteObject();
    upsert saveObject; 
    returnObject.Sobjects.add(saveObject);
    return returnObject;
}        

 

QingsongQingsong

Constructs a Datetime from Integer representations of the year, month (1=Jan), day, hour, minute, and second in the local time zone. For example:
Datetime myDate = datetime.newInstance(2008, 12, 1, 12, 30, 2);

 

 

 

JeffTrullJeffTrull

I too am struggling with this issue.  Date fields inside SObjects don't seem to get serialized properly, and the format used for plain Date parameters (UTC string) doesn't work when it's a field of an SObject parameter.  I've turned this into a StackExchange question, maybe it will get some traction there.