19

Say I have one string String strId = 001xa000003DIlo and I want to typecast it to an Id. So I have two options to do so:

  1. Id idVal = (Id)strId;
  2. Id idVal = Id.valueOf(strId);

Which is the best way to achieve this purpose?

What exactly is the difference between these two approaches?

Is the difference same for typecasting a String, Decimal or any other datatype?

2 Answers 2

25

The first is casting. The second is calling a static method on the Id class.

One major advantage of the former is that it is more null safe.

Id idVal1 = (Id)null; // works
Id idVal2 = Id.valueOf(null); // blows up

One advantage of the latter is that you can one-line more of your code by calling instance methods on the cast result. You can get one-liners by adding some parentheses to the casting approach, though some might object on stylistic grounds. I find it a little more visually confusing.

SObjectType t1a = (Id)'001000000000000AAA'.getSObjectType(); // blows up
SObjectType t1b = ((Id)'001000000000000AAA').getSObjectType(); // works
SObjectType t2 = Id.valueOf('001000000000000AAA').getSObjectType(); // works

The above pros/cons are independent of type, so you can carry these conclusions over to String, Decimal, etc.

1
  • 4
    Just FYI you can still one line while casting like this ((Id)idPropertyVal).getSObjectType(); - You cannot do (Id)'001000000000000AAA' anyway as it throws an error....
    – Eric
    Commented Jul 20, 2016 at 20:55
13

Casting is not required to convert a String to an Id:

String strVal = '001000000000000AAA';
Id idVal1 = strVal;
Id idVal2 = '001000000000000AAA';

Id is a specialization of String.

This can be shown by:

Id idVal = '001000000000000AAA';
System.assert(idVal instanceof String); // ERROR: Operation instanceof is always true since an instance of Id is always an instance of String

There is a slight difference in heap space used for Id.valueOf vs cast:

Id id1 = '001000000000000';
Id id2 = (Id) id1;
System.debug(Limits.getHeapSize()); // 1047

vs

Id id1 = '001000000000000';
Id id2 = Id.valueOf(id1);
System.debug(Limits.getHeapSize()); // 1051
8
  • I am aware of that @Alan, However I wanted to know the difference between these two approaches and their proper usages which Adrian has described. Thanks anyways. Cheers !!!
    – Abbas
    Commented Jul 20, 2016 at 21:10
  • 1
    Sometimes it's required. For instance, you may want to use Id instance methods.
    – Adrian Larson
    Commented Jul 20, 2016 at 21:17
  • 2
    Probably should do many runs to analyze heap usage. It tends to vary slightly each execution.
    – Adrian Larson
    Commented Jul 20, 2016 at 21:19
  • 1
    @AdrianLarson No, heap size is always consistent between runs of the exact same code in the exact same environment. The four byte difference means that a new object is being allocated on the heap. I also personally know that casting is faster than Id.valueOf by a small margin, so casting actually uses less memory, less CPU time, and less code characters. It is therefore essentially a useless function to have exposed at the user level.
    – sfdcfox
    Commented Jul 21, 2016 at 1:44
  • 3
    @AdrianLarson Just did some testing... casting is about 3x times faster than Id.valueOf. The More You Know.
    – sfdcfox
    Commented Jul 21, 2016 at 2:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .