illegal assignment from string to datetime

Error while updating fields. line 326, column, illegal assignment from String

Example of Full Error Message: Error while updating fields. Error message: line 326, column, illegal assignment from String to Datetime

How this error may happen:  This error can happen when trying to generate a document and also trying to update Date field once document generation is done.

How to resolve: If had created a formula field and tried to remove the NOW() from the code, then you also need to copy new button code to the button. If you would like to apply value to some date field, we would suggest that you update some text field and then create Workflow that would update Date field.

automationanywhere-en Logo

  • Developers Forum

DateTime Assign: Cannot convert string to date time

  • 1 year ago 1 March 2023

Badge

  • Alessandro 3854

I have CSV file where I have a date/time field as “(1/30/2023 1:35:45 PM)”. What I want to do is figure out the Month and Year.

I am using the DateTime: Assign to convert the string into a datetime value. However, the only output I get from the bot is an unhelpful error “An exception occurred while parsing the date time”

No matter what string we enter, we get the same error. I’m not sure what we’re doing wrong. How have you made this work?

Best answer by Diksha.M1 1 March 2023, 09:41

Padmakumar

  • 786 replies

Hi @Alessandro 3854  ,

First please check what is the Date format you are getting on the System variable called Date. First assign it to a variable and display it after converting to String. You should follow the same format while assigning the Date from the CSV file and later you can choose you desired format while converting it to string.

Diksha.M1

Hi Alessandro 3854  ,

If you have the Date Time value in string format and while using DateTime: Assign to convert the string into a datetime value, Use the Custom Formats as M/d/y h:m:s a  , It will give you the datetime D1 in correct format. 

illegal assignment from string to datetime

Then you can use DateTime: To String action and use the variable D1 and the custom format as y or M to get the year and month respectively. Thanks,

Thanks for the help. Unfortunately I could not get it to work. I tried the following using your feedback.

Example #1: Date Time: 1/30/2023 1:35:45 PM Custom Format: M/d/y h:m:s a

Example #2 Date Time: 1/30/2023 Custom Format: M/d/y Example #3 Date Time: 1/30/2023 Custom Format: MM/dd/yyyy

Hi  Alessandro 3854 , 

Can you share more details about the issue.

I am using similar things and its working for me fine.

This is odd. Tried it again and it worked. No change from my side.

I believe the steps outlined were correct, so I will assume this was an issue with the environment.

Badge

  • 1 month ago 11 March 2024

I have CSV file where I have a date/time field as “(1/30/2023 1:35:45 PM)”. What I want to do is figure out the Month and Year How to do this Conversion...i tried with above steps mentioned but the error remains the same  dateTime' expecting 'ZonedDateTime' received 'String'

Hi @Prade 1645 ,

My solution was 1. Remove the first “(“ and last “)”. I used a replace function 2. Then use the DateTime:Assign

  • Custom format
  • M/d/yyyy h:mm:ss a

Already have an account? Login

Login to the Pathfinder Community

Enter your username or e-mail address. We'll send you an e-mail with instructions to reset your password.

Scanning file for viruses.

Sorry, we're still checking this file's contents to make sure it's safe to download. Please try again in a few minutes.

This file cannot be downloaded

Sorry, our virus scanner detected that this file isn't safe to download.

Salesforce Globe For You - Salesforce Shorts

A blog containing Salesforce coding examples in detail.It's a place where you can find solutions to the problems that you may face in your daily salesforce coding. Apex classes,visual force page,we services,Integration,plugins,extensions,Lightning web components,Aura components,Email Messages,Sales cloud,service Cloud,Marketing Cloud.Lightning Web components,salesforce.com, Salesforce Tips and Tricks,short cuts,ApexMocks,Stubs, visibility,Salesforce Shorts

Search This Blog

Friday, august 31, 2018, error: compile error: illegal assignment from datetime to date, no comments:, post a comment.

' border=

  • How to deploy/retrieve a visualforce page using package.xml file in salesforce Assume AccountPage is an apex class in salesforce. Sample Xml file : <?xml version="1.0" encoding="UTF-8"?> &l...

Piotr Gajek

Type Casting in Apex

Hello devs,

What are Upcasting and Downcasting? How can you cast Apex Map, List, and Set? What is a supertype, and what is the difference between SObject and Object? Answers to these questions and much more can be found in this post!

Let's begin.

Before We Start

I am using different emojis to grab your attention:

  • 🚨 - Unexpected casting behavior.
  • ✅ - Code that works.
  • ❌ - Code that does not work.

What supertype is?

A supertype is an abstract/virtual class or an interface that is used by a child class.

The Parent interface is a supertype for the Child class.

A class can implements many interfaces, meaning that a class can have many supertypes .

The Parent abstract class is a supertype/superclass for the Child class.

A class can extends only one abstract class, but it can still implements many interfaces.

The Parent virtual class is a supertype/superclass for the Child class.

A class can extends only one virtual class, but it can still implements many interfaces.

Upcasting vs Downcasting

You know what supertype is, it's necessary to understand the difference between Upcasting and Downcasting .

Upcasting and Downcasting in Apex

To better understand upcasting and downcasting, we need some examples.

We have two classes (it can be also class and interface), Parent and Child .

  • Parent is a virtual class and has a virtual method.
  • Child extends the Parent class and override the virtual method.

If you don't understand what virtual means, you can refer to the Abstract, Virtual, Interface in Apex post.

  • Child extends the Parent class. It means that Parent is a supertype/superclass for Child . Because of this, the following syntax: Parent parent = new Child(); is valid.
  • Casting a subclass ( Child ) to a supertype/superclass ( Parent ) is called Upcasting .
  • Upcasting can be either explicit or implicit.

Implicit Upcasting

Upcasting can be done implicitly.

Explicit Upcasting

Code should be as simple as possible (KISS rule), so the better approach is to use implicit upcasting.

The parent variable has access to:

  • Parent public variables.
  • Parent public methods.
  • Child ONLY overriden methods.

Downcasting

  • Casting a supertype/superclass ( Parent ) to a subclass ( Child ) is called Downcasting .
  • Upcasting can be done ONLY explicitly.

Implicit Downcasting

Downcasting CANNOT be implicit. Why?

  • There can be many children of the Parent class. e.g public class Child2 extends Parent { ... } .
  • Apex needs to know to what type the variable should be cast to.

Explicit Downcasting

You have to cast explicitly (Child) so the compiler checks in the background if this type of casting is possible or not. If not, you will get System.TypeException: Invalid conversion from runtime type Parent to Child .

The child variable has access to:

  • Child ALL public methods and variables.
  • SObject Class

SObject is a supertype for:

  • all standard objects (Account, Contact, etc.)
  • all custom objects (MyCustomObject__c)

Object is a supertype for:

  • all standard objects
  • all custom objects
  • all apex classes
  • all collections (List, Set and Map)
  • all apex types (Integer, String, Boolean)
  • Methods inherited from Object class

SObject and Object

Sobject and object - instanceof.

  • SObject is an instance of Object . Object is a supertype for SObject .

SObject and Object - casting

Object to sobject.

  • Object needs to be explicitly cast to SObject .
  • Object is a supertype of SObject .
  • Conversion from Object to SObject is called Downcasting .
  • As you already know from Downcasting section. Downcasting needs to be explicit (SObject) .

SObject to Object

  • SObject can be explicitly or implicitly cast to Object .
  • Conversion from SObject to Object is called Upcasting .
  • As you already know from Upcasting section. Upcasting can be done explicitly or implicitly.

Inherited methods

  • Interestingly, Object is a supertype for SObject , but SObject does NOT inherit Object methods like toString(), equals(), hashCode(), clone() . SObject has all of the following methods SObject class methods. .

However, all Apex Classes inherit Object methods ( toString() , equals() , hashCode() , clone() ).

Standard/Custom Object

Standard/custom object - instanceof.

  • Standard/Custom Object is an instance of SObject and Object .
  • SObject and Object are supertypes for Standard/Custom Objects .

Standard/Custom Object to SObject - casting

Standard/custom object to sobject.

Why it works?

  • SObject is a supertype for Account , Contact , and other standard or custom objects.
  • Conversion from Standard/Custom Object to SObject is called Upcasting .

SObject to Standard/Custom Object

  • Conversion SObject to Standard/Custom Object is called Downcasting .
  • As you already know from Downcasting section. Downcasting can be done ONLY explicitly.

Standard/Custom Object to Object - casting

Standard/custom object to object.

  • Object is a supertype for Account , Contact , and other standard or custom objects.
  • Conversion from Standard/Custom Object to Object is called Upcasting .

Object to Standard/Custom Object

  • Conversion Object to Standard/Custom Object is called Downcasting .

Primitive Types

Primitive types - instanceof.

  • All Primitive Data Types are instanceOf Object class.
  • Object is a supertype for all primitve types.

Primitive Types - casting

  • Primitive Types can be implicilty cast to Object .
  • Object is a supertype of all Primitive Types.
  • Conversion from Primitive Type to Object is called Upcasting .

List<SObject>

List<sobject> - instanceof.

  • List<SObject> is an instance of concrete Standard/Custom Object!
  • List<SObject> is NOT an instance of List<Object> .

List<SObject> - casting

List<sobject> to list<object>.

  • List<SObject> is NOT an instance of List<Object> (based on instanceOf ), but you can still assign List<SObject> to List<Object> . Do not trust instanceOf !

List<Standard/Custom> to List<SObject>

  • List<SObject> is a supertype for List<Standard/Custom> .
  • Conversion from Standard/Custom Object to List<SObject> is called Upcasting .

List<SObject> to List<Standard/Custom>

Upcasting/Downcasting (?)

  • List<Standard/CustomObject> is an instance of List<SObject> , and List<SObject> is an instance of List<Standard/CustomObject> .
  • List<Standard/CustomObject> is a supertype for List<SObject> , and List<SObject> is a supertype for List<Standard/CustomObject> .
  • You can do implicit casting.

List<Object>

List<object> - instanceof.

  • SObject is always an instance of Object , hovever List<SObject> is NOT an instance of List<Object> .

List<Object> - casting

  • We can cast List<ConcreteType> to List<Object> ,
  • List<Object> is a supertype of List<ConcreteType> .
  • Conversion from List<ConcreteType> to List<Object> is called Upcasting .
  • There is no need to cast explicitly by adding (List<Object>) .
  • List<SObject> is NOT an instance of List<Object> (based on instanceOf ), but you can still assign List<SObject> to List<Object> .

List Summary

List casting in Apex is a bit unusual.

What is common:

  • List<ConcreteType> is an instance of List<Object> .
  • List<Object> is a supertype for List<ConcreteType> .

What is unexpected:

  • Based on instanceOf . List<SObject> is not an instance of List<Object> , but you can still assign List<SObject> to List<Object> .
  • You can even assign List<SObject> to List<Object> . Do not trust instanceOf !
  • Even more unexpectedly, List<SObject> is an instance of concrete List<Standard/Custom> Object!

Set<SObject>

Set<sobject> - instanceof.

  • Set<Standard/CustomObject> is NOT an instance of Set<SObject> .

Set<SObject> - casting

Set<sobject> to set<object>.

  • You CANNOT cast Set<SObject> to Set<Object>

Set<Standard/Custom> to Set<SObject>

  • You CANNOT cast Set<Standard/Custom> to Set<SObject>

Set<SObject> to Set<Standard/Custom>

  • You CANNOT cast Set<SObject> to Set<Standard/Custom>

Set<Object>

Set<object> - instanceof.

  • Other than List , Set<Object> is NOT a supertype for Set<ConcreteType> .

Set<Object> - casting

  • We CANNOT cast Set<ConcreteType> to Set<Object> ,

Even if you explicitly add casting (Set<Object>) it will not work.

Set Summary

Map<sobject>, map<sobject> - instanceof.

I skipped cases where SObject is a key. SObject should never be a key in the Map.

  • Map<Object, Account> is an instance of Map<Object, Object> , which means that Map<Object, Object> is a supertype of Map<Object, Account> .
  • Map<Object, Account> is an instance of Map<Object, SObject> , which means that Map<Object, SObject> is a supertype of Map<Object, Account> .

Map<SObject> - casting

  • Map<Object, Object> is a supertype for Map<Object, Standard/CustomObject> .
  • Conversion from Map<Object, Standard/CustomObject> to Map<Object, Object> is called Upcasting .

Even explicit casting will not work. The error is different.

How to fix Map downasting?

Map<Object>

Map<object> - instanceof.

Maps are the same instance only in the following cases:

  • new Map<MyType, MyType>() instanceOf new Map<MyType, Object>
  • new Map<Object, MyType>() instanceOf new Map<Object, Object>

Key Type must be the same.

Map<Object> - casting

You can cast implicitly only when:

Map Summary

Starting from Summer 23', Set implements Iterable interface as List does.

Iterable List

Iterable list - instanceof.

Not surprisingly, a List of concrete types is also an instance of Iterable<Object> .

  • Instance of List<Object> and Set<Object> are always an instance of System.Iterable<Object> .

Iterable List - casting

It's really interesting.

e.g List<String> is an instance of Iterable<Object> , but you CANNOT use Iterable<Object> as a supertype .

You need to assign List<String> to List<Object> and after it to Iterable<Object> .

Iterable Set

Iterable set - instanceof.

  • An interesting thing is that you're getting an error Operation instanceOf is always true... .
  • Set<ConcreteType> is also an instance of Iterable<Object> , but not Set<SObject> .

Iterable Set - casting

  • You CAN use Iterable<Object> as a supertype for Set , which you CANNOT do with a List .
  • You CANNOT assign Set<SObject> to Iterable<Object> .

It's quite a big deal. Let's check an example:

How to fix it?

  • You need two methods ( isIn(Iterable<Object> inList) and isIn(List<Object> inList) ) in the interface that will be covered by one method ( isIn(Iterable<Object> inList) in concrete class!

Iterable Summary

  • You CANNOT use Iterable<Object> as a supertype for List<ConcreteType> . You need to assign List<ConcreteType> to List<Object> and after it to Iterable<Object> .
  • Set<ConcreteType> is instance of Iterable<Object> .
  • Set<Sobject> is NOT instance of Set<SObject> .

Casting Rules

Cast to everything.

The problem with the solution below is performance.

Casting Cheat Sheet

Do not trust instanceof.

Based on the type system is broken with regards to collections :

Pretty much the entire "Type" system that governs Maps, Sets, and Lists is broken. Do not trust instanceOf, use your own logical assessment to determine if something is safe or not.

As shown in the post, there are cases where instanceOf does not work correctly.

List<SObject> and List<Object>

instanceOf says that " List<SObject> is never an instance of List<Object> ", but you can assign List<SObject> to List<Object> .

List<ConcreteType> and Iterable<Object>

instanceOf says that List<ConcreteType> is an instance of Iterable<Object> , but you CANOT assign List<ConcreteType> to Iterable<Object> .

SObject Casting

  • Casting from Standard/Custom Object to SObject is called upcasting and can be implicitly.
  • Casting from SObject to Standard/Custom Object is called downcasting and it needs to be explicit.
  • SObject is instance of Object . Object is a supertype for SObject .
  • Casting from SObject to Object is called upcasting and can be implicit.
  • Casting from Object to SObject is called downcasting and it needs to be explicit.

Object Casting

Primitive types casting.

  • All Primitive Data Types are instanceOf Object . Object is a supertype for all primitve types.
  • Casting from Primitive Data Types to Object is called upcasting and can be implicit.

List Casting

  • All List<ConcreteType> are instance of List<Object> . List<Object> is a supertype of List<ConcreteType> .
  • Based on instanceOf method List<SObject> is NOT an instance of List<Object> , but still you can assign List<SObject> to List<Object> . Do not trust instanceOf .
  • Casting from List<ConcreteType> to List<Object> is called upcasting and can be implicit.
  • List<Standard/Custom> is a supertype for List<SObject> .

Set Casting

  • You CANNOT cast Set<ConcreteType> to Set<Object> .

Map Casting

  • Map<Object, Account> is instance of Map<Object, Object> , it means that Map<Object, Object> is a supertype for Map<Object, Account> .
  • Map<Object, Account> is instance of Map<Object, SObject> , it means that Map<Object, SObject> is a supertype for Map<Object, Account> .
  • To fix System.TypeException: Invalid conversion from runtime type Map<ANY,SObject> to Map<ANY,Account> dynamic Map initiation is needed.

Iterable Casting

  • Set<ConcreteType> is instance of Iterable<Object> , but Set<SObject> is NOT instance of Iterable<SObject> .
  • Unexpected Iterable behavior in Apex
  • Classes and Casting
  • Using the instanceOf Keyword
  • Upcasting Vs Downcasting in Java

Buy Me A Coffee

You might also like

logo

Advanced Salesforce LWC debugging with Chrome Developer Tools

LWC debugging and development with Chrome Developer Tools – learn how Salesforce LWC developer can debug and develop Lightning Web Components effectively without console.logs.

logo

Beyond If Statements: Ways to avoid IFs – Polish Dreamin’ 24

Code is a representation of business requirements. Business requirements vary for each company, but most of them have an IF-THEN structure. It’s quite common to see IF statements in our code. However, an increasing number of IFs can make our code hard to read and understand.

illegal assignment from string to datetime

Illegal assignment from String to Decimal

  • 役に立った順で並び替え

illegal assignment from string to datetime

Spot on for Amit

String temp = '12.4567';

Decimal myDecimal = Decimal.valueOf(temp);

System.assertEquals(12.4567,myDecimal);

IMAGES

  1. How To Convert A String To DateTime In Python?

    illegal assignment from string to datetime

  2. Salesforce: Illegal assignment from List<SObject> to List<String> (3

    illegal assignment from string to datetime

  3. How To Convert A String Datetime In Python Guides Using Strptime() 5

    illegal assignment from string to datetime

  4. Salesforce: Illegal assignment from List to String (2 Solutions

    illegal assignment from string to datetime

  5. Convert String To Datetime Python Multiple Formats

    illegal assignment from string to datetime

  6. Salesforce: Illegal assignment from object to string Aggregate Result

    illegal assignment from string to datetime

VIDEO

  1. BIAFRA SØĻƊÍËŘS CÃPŤÙRÈD NIG MILITARIES ON AN ILLEGAL ASSIGNMENT IN ENUGU STATE

  2. NIG SOLDIERS BEGGED B L A IN THE FOREST IN ORLU AS 65 OF THEM WERE CAPTURED FOR ILLEGAL ASSIGNMENT

  3. String of illegal dumping incidents leads to closure of Avra Valley area

  4. Python 04G1 Café Invoice with code review

  5. ILLEGAL ACTIVITY

  6. Video Poker Machines Seized In Gambling Bust

COMMENTS

  1. Cannot implicitly convert type 'string' to 'System.DateTime'

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams Create a free Team

  2. How to convert string to Time(dataType) field in apex

    I was trying to convert string to Time datatype in apex, any one help on this would be great. ... Illegal assignment from String to Datetime. 0. Unable to convert a particular string format to DateTime. Hot Network Questions Optimal Solution for the Four Divisors Problem on LeetCode

  3. Format specific date in Apex as dd/mm/yyyy

    I tried to find an existing question/answer for my problem, but didn't see anything so I'm trying to format a date in apex as "mm/dd/yyyy", but get the error: "Illegal assignment from String to Date" My code is below.

  4. How to assign string to date type in apex code [closed]

    In my code i am getting this error: Illegal assignment from String to Date Kindly anyone explain how to assign a string to date field in List (APEX code). Here is my code: for (Integer i=1; i...

  5. Salesforce: Format specific date in Apex as dd/mm/yyyy

    Salesforce: Format specific date in Apex as dd/mm/yyyy - illegal assignment from string to dateHelpful? Please support me on Patreon: https://www.patreon.co...

  6. Illegal assignment from Datetime to Date

    10. 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();

  7. Salesforce: Illegal assignment from Datetime to Date

    Salesforce: Illegal assignment from Datetime to DateHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWith thanks & praise to God,...

  8. How To Convert a String to a datetime or time Object in Python

    Converting a String to a datetime object using datetime.strptime() The syntax for the datetime.strptime() method is: datetime.strptime(date_string, format) The datetime.strptime() method returns a datetime object that matches the date_string parsed by the format. Both arguments are required and must be strings.

  9. salesforce

    parse(stringDate) Constructs a Date from a String. The format of the String depends on the local date format. valueOf(stringDate) Returns a Date that contains the value of the specified String. What I wanted was the parse: String inputDate = date.today().format(); / Date dateFromInput = date.parse(inputDate);

  10. Error while updating fields. line 326, column, illegal assignment from

    Example of Full Error Message: Error while updating fields. Error message: line 326, column, illegal assignment from String to Datetime. How this error may happen:

  11. DateTime Assign: Cannot convert string to date time

    Hi Alessandro 3854 ,. If you have the Date Time value in string format and while using DateTime: Assign to convert the string into a datetime value, Use the Custom Formats as M/d/y h:m:s a , It will give you the datetime D1 in correct format.. Then you can use DateTime: To String action and use the variable D1 and the custom format as y or M to get the year and month respectively.

  12. put(field, value) method from SObject class throws System

    System.SObjectException: Illegal assignment from String to Datetime. I don't understand the reason. Deserialization in the wrapper class is correctly performed (checked) The get method of the wrapper class RequestData returns a value of type Object.

  13. Error: Compile Error: Illegal assignment from Datetime to Date

    Datetime,Error: Compile Error: Illegal assignment from Datetime to Date ,Date field error in salesforce

  14. Type Casting in Apex

    Parent parent = new Child(); Child child = parent; // Illegal assignment from Parent to Child Explicit Downcasting. You have to cast explicitly (Child) so the compiler checks in the background if this type of casting is possible or not. If not, you will get System.TypeException: Invalid conversion from runtime type Parent to Child.

  15. Illegal pattern character 'T' when parsing a date string to java.util

    java.util.Date date = java.util.Date.from( instant ); // Pass an `Instant` to the `from` method. Time Zone. If needed, you can assign a time zone. ZoneId zoneId = ZoneId.of( "America/Montreal" ); // Define a time zone rather than rely implicitly on JVM's current default time zone.

  16. Illegal assignment from String to

    Illegal assignment from String to Decimal. Hello, I need to convert string to decimal Code //upsert the existing case to add the fields Case newC = new Case (); newC.Amount_of_Bill__c = AmountofBill1; I thought I was converting to String... //fetch all the fields from the first block Map<String,Object> BCB1 = (Map<String,Object ...

  17. System.SObjectException: Illegal assignment from String to Date

    2. What happened is you tried to get CLDate from the targetFields map, which was null, so no conversion from String to Date occurred (you basically went down a default when else path, since the described type was null). You need to change:

  18. Unable to convert string to date type using Import-Csv

    System.InvalidOperationException: The given value of type String from the data source cannot be converted to type date of the specified target column. ---> System.FormatException: Failed to convert parameter value from a String to a DateTime. ---> System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse ...

  19. How to Fix the "Illegal String Offset" Error

    Solution 1: Check if the Key Exists. To avoid the "Illegal String Offset" error, you can check if the key exists before attempting to access its corresponding value. One way to achieve this is by using the hasOwnProperty() method to verify if the key exists within the array or object. By checking if the users object has the userName as a valid ...

  20. Illegal assignment from SObject

    3. In Apex, I cannot successfully execute this anonymous block without casting on Ln 2 below: List<SObject> lst = new List<SObject>{ new Custom_SObject__c() }; Custom_SObject__c cstom = lst[0]; It will complain with this error: Illegal assignment from SObject to Custom_SObject__c. But I can execute this without an error:

  21. What is that the Error of Illegal assignment and how to correct it?

    t[min] := aux; end; end; end; That's supposed to be a correct and well-known code to arrange integers from inferior to superior but compiler still insists saying "illegal assignment to for loop 'j' variable".