Pages

Friday, November 30, 2012

Salesforce convert Apex integer to long

A quick snippet after I came across the following when modifying some apex:

integer someId = 12345;
aMethodThatTakesALong(Long.valueOf(someId.format()));

Note the transition via an intermediate string to get to the long required by the method.

Usually Apex will implicitly convert an integer to a long if required. E.g.

Integer testInt = 12345;
Long testLong = testInt;
System.assertEquals(12345L, testLong);

In the case of the method parameter above you can either rely on the implicit casting or, where there may be other overrides available, explicitly cast the integer to a long. E.g.

integer someId = 12345;
aMethodThatTakesALong((long)someId);

No comments:

Post a Comment