6

I'm trying to deserialize a received user record on a JSON format. TO achieve this I'm using JSON.deserialize() but when I specify the User class the compiler throws the following error:

Save Error: variable does not exist User.class

This is my code:

    @RemoteAction
    global static String updateTotalAccompByUserId(String userJSON, String userCategory, String totalAccomp, String currentQuarter, String currentYear){
        ...
        User user = (User) JSON.deserialize(userJSON, User.class); /*ERROR*/
        system.debug('%%% user: ' + user);
        ...
    }
2
  • 1
    Usually such things happens when you have created a class with same name as object. In that case the compiler is confused.
    – Raul
    Commented Dec 12, 2017 at 12:21
  • No idea if it works, but did you try to use the Type.forName('User') alternative?
    – itsmebasti
    Commented Dec 12, 2017 at 12:22

1 Answer 1

6

I was able to reproduce the issue in my DEV org by creating a class named User.

Another class has following code:

User user = (User) JSON.deserialize('{}', User.class); 

Which yeilds the same error as you have mentioned:

Variable does not exist: User.class

Solution: Delete or rename User custom class and try again.


Better Solution mentioned by Adrain in comment: Use Schema.User.class:

Schema.User user = (Schema.User) JSON.deserialize('{}', Schema.User.class); 
3
  • 6
    Better solution, use Schema.User.class instead.
    – Adrian Larson
    Commented Dec 12, 2017 at 13:28
  • Agreed. Naming a class with object is bad as it causes unnecessary problems in many places. If very much required I use inner wrapper classes instead of global ones.
    – Raul
    Commented Dec 12, 2017 at 13:32
  • 4
    No I 'm saying you don't need to delete the problem class, just be more explicit when declaring the type to deserialize into.
    – Adrian Larson
    Commented Dec 12, 2017 at 13:33

You must log in to answer this question.

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