function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Emmanuel B.Emmanuel B. 

Id.valueOf static method not working


I have a custom field where is stored a 15 digits Id.
I need to build a String csv with the 18 digits version of this Id.
In my Execute anonymous window, I tried a simple



and got



Wonderful, that's what I need !  But when I try to write in my class

csv += Id.valueOf(var);


I get an ugly

Save error: variable does not exist: Id


I finally did what I need by creating a variable with

Id myId = String.valueOf(var); 
csv += myId;


But I don't understand why the documented static method Id.valueOf works in anonymous code and not in a class ?



There is not any method like id.valueOF(). You can apply String.valueof('id here') method.

For example


String s = '101c00000001GWe';

ID =s;


Emmanuel B.Emmanuel B.



Sure there is, in addition of what I wrote about the working anonymous code (but there was a mistake, I wrote string instead of ID), here is a paste of my Apex Code Developer's guide :


ID Methods
The following are the static methods for ID.

Method : valueOf
Arguments : String s
Return Type : ID

Description : Convertst he specified String into an ID
and returns the ID.


Douglas AyersDouglas Ayers
Thanks @Emmanuel for the workaround! Although the method Id.valueOf( str ) is documented, the developer console "anonymous apex" execution obviously doesn't understand the syntax (I'm thinking a bug here...).  The Id myId = String.valueOf(var); did the trick!
Emmanuel B.Emmanuel B.
I've not tried, but I think System.Id.valueOf(var) might work...
Eli Flores, SFDC DevEli Flores, SFDC Dev
What sticks out to me is the left side of the argument:

csv += Id.valueOf(var);

Is csv an ID field? Maybe it's getting confused because you're treating it as a string?

I've used this syntax  in some of classes and  never  encountered the issue. Though my code doesn't use it with a string literal.

Also, mainly I ask because i'd do it but is it possible that you type Ld instead of Id?
Emmanuel B.Emmanuel B.

Well it's a long time ago but I must admit that, even if I hate this idea, I could have written LD instead of Id because I tried again and now it works perfectly, and if I write Ld I have the error message described up there.

However I still doubt it because I probably pasted the error message in my post and it actually is a I in the message.

Maybe the last revisions of salesforce fixed it ? @DouglasCAyers : did you find the same problem  ?

Anyway, it works, that's what matters :)

Douglas AyersDouglas Ayers
I did finally get it to work, I had to ensure the argument being passed into Id.valueOf( str ) was explicitly a String and not an "inferred" string. More specifically in my case, I was looping through a result set of AggregatedResult and getting the "id" look up field from the result and adding it to a Set<ID> as acctIds.add( result.get( 'account__c' ) ); but to fix it I needed to case the generic SObject to either (String) or (ID).  But I got the same error message that led me here: Save error: variable does not exist: Id
Emmanuel B.Emmanuel B.
So we got the same problem, I just found the part of the code where I got the problem :
Id myId = String.valueOf(obj.get(field)) ;
csv += myId + + ';';

Where obj is a generic SObject and field a String (the name of the object field).

But why this message ?... At least I didn't type a L for a I :)

Douglas AyersDouglas Ayers
The error message is confusing and leaves much to be desired. I ended up using this syntax:
Set<ID> myIds = new Set<ID>();

for ( AggregatedResult result : [ SELECT field, count(id) FROM object GROUP BY field ] ) {
    myIds.add( (ID) result.get( field ) );

Prakhar KPrakhar K
The trick is that if one uses  ID.valueOF(18 digit Id) in Anonymous Window and pass 18 digit Id then it will work perfectly BUT won't WORK IN CLASS.For making it work in class , PASS 15 DIGIT ID while working in APEX class  by removing the last 3 digits.
Everything will work like a charm!.

Mark it as the Best answer if it helps!!!!