I am trying to map a custom object's CreatedDate to another custom object's custom date field. But I am getting this error:

Illegal assignment from Datetime to Date

I am doing something like this:

Assume that I assigned a variable already to Object1__c, and I want to map the value of its CreatedDated to Date__c field of Object2__c.

Object2__c ob2 = new Object2__c();
ob2.Date__c = ob1.CreatedDate; // error here

You can get the Date value from a Datetime instance by calling the date() method:

obj2.Date__c = obj1.CreatedDate.date();

If you're calling this method on a Datetime instance where you are not sure if it can be null, make sure to add a null check:

obj2.Date__c = (obj1.CreatedDate == null) ? null : obj1.CreatedDate.date();

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.