Compile Error: Illegal assignment from String to Datetime in Salesforce Apex

Our Salesforce org has a custom form field where users manually enter the date and time for follow-up meetings. The input is captured as a string from a custom form field. We need to convert this string input into a Datetime format to create a follow-up event in the system.

While converting the string to a datetime, I faced a compilation error, “Illegal assignment from String to Datetime,” because I had tried to directly convert the string to a datetime value. Then, I found the correct method for string-to-datetime conversion, which I’ll explain in this Salesforce tutorial.

Cause of Illegal assignment from String to Datetime Error in Apex

In Salesforce Apex, the “Illegal assignment from String to Datetime” error occurs during data type conversion because Apex is a statically typed language, meaning variables are expected to follow strict data types. A String and a Datetime are different types of data, and Apex requires explicitly converting a String to a Datetime rather than assigning it directly.

Illegal Assignment error in Salesforce Apex

Direct Conversion from String to Datetime

The code below explains the incorrect and direct method of string-to-datetime conversion, which generally returns the compilation error “Illegal assignment from String to Datetime.”

String myDate = '2024-09-05 14:30:00'; 
Datetime dt = myDate;

In the above code, we are trying to assign the String value directly to the Datetime variable dt, which is not allowed in Apex.

How to Fix Error: Illegal Assignment from String to Datetime

Now, we will discuss the following correct methods to convert a string to a datetime to avoid the compilation error: “Illegal assignment from String to Datetime.”

In Apex, we have two valid methods to correctly convert the String data type to Datetime. The first method is Datetime.ValueOf(), and the second method is Datetime.newInstance.

Use valueOf() Method to Fix Illegal Assignment from String to Datetime

The Datetime.valueOf() method converts a string to a datetime when the string is in the standard format of ‘yyyy-MM-dd HH:mm:ss‘.

Now, open the Salesforce developer console and enter the code below in the Apex anonymous window.

String strDate = '2024-11-05 14:30:00'; 
Datetime dt = Datetime.valueOf(strDate); 
System.debug('Converted String to Datetime: ' + dt);

After entering the code, click on the Execute button, and then in the execution log window, you will see the String values converted to datetime in the debug.

Salesforce apex Illegal Assignment from String to Datetime

This way, we can convert a String to a datetime using the valueOf() method in Salesforce Apex and avoid the compilation error of string to datetime conversion.

Use newInstance() Method to Fix Illegal Assignment From String to Datetime

In Salesforce Apex, the newInstance() method is used to create a Datetime from a specific Date and Time by passing in Date and Time values separately.

Now, open the Salesforce developer console and enter the code below in the Apex anonymous window.

Date myDate = Date.newInstance(2024, 10, 3);
Time myTime = Time.newInstance(14, 30, 0, 0); 
Datetime dt = Datetime.newInstance(myDate, myTime);
System.debug('Converted String to Datetime: ' + dt);

In the above code, we have taken separate values for the Date and time instances. Then Datetime dt will add both strings and convert them to the datetime format.

After entering the above code, execute it. Then, in the execution log window, you will see the string values converted to the datetime type, as shown in the image below.

Illegal Assignment from String to Datetime in Salesforce Apex

This way, we can convert the string to a datetime using the newInstance() method and can effectively handle the occurrence of Illegal assignment errors.

Conclusion

In this Salesforce tutorial, we discussed the apex compilation error “Illegal assignment string to date.” Then, we learned how to efficiently convert a string to a datetime in Salesforce Apex using the valueOf() and isnewInstance() methods and handle the occurrence of compilation errors in Salesforce Apex.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.