Tuesday, January 22, 2013

[APEX] Strange automatic casting error from String to ID

Developing some APEX for a customer, I saw this error:
System.StringException: Invalid id: -1

I checked the code and it was something like this:
     if(aString == anSObject.Id){
So a String object cannot be compared to an ID type if the first is not an ID?
I switched the members:
     if(anSObject.Id == aString ){
And no error is thrown.

In my experience it is a strange behavior, because I would have expected that the ID field whould have been casted to String (being the second operand) rather then the String object been casted to ID type...but maybe my whole life is a lie!!

No comments:

Post a Comment