Method does not exist or incorrect signature: void getUserName() from the type UserInfo

apexexecute-anonymoususeruserinfo

I dont know what is the problem. When I am trying to use UserInfo.getUserName() in my main org it doesnt work and gives me error like in title, but in another org it works perfectly. What can I do? So maybe there are some settings enabled?

UserInfo.getUserName()

Best Answer

This error, in respect to standard library methods, is always a result of name shadowing, assuming you have the correct parameters; the only other time you should get this error is if you have the wrong number or data types of parameters (e.g. passing in a String when an Integer is expected).

Basically, you have either a variable, class, or interface named UserInfo (case insensitive) in the org where you're having the problem. The fix is to either (a) refactor or remove the UserInfo class/variable/interface so it's no longer conflicting, or (b) use System.UserInfo.getUserName() instead, which is always guaranteed to reference the library method, since System is a reserved keyword.

As a matter of practicality, I advise refactoring over using the System namespace, because this is likely to come up over and over again as more developers work on your code.