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

Get object Id


I have a doubt.

When i have to use an id which is the best way to put it in a variable.


user u=[Select id,name from user where... limit 1 ];;







are there other way?

Which is the best practise?


Thanks in advantage.





You should never really hard code in an Id in the code because if you ever change / migrate to a new sandbox that users' Id will change, and then you will have to change it in the code everytime you hardcoded it in.


Thanks for the reply!

okkay hard coding an ID is bad.



Actually i'm doing:

user u=[Select id,name from user where name=... limit 1 ];

 But make a query for me is also a bad solution.

If you after change the name of the user(the same for other resources or objects) , get id won't work again.

This is a problem for me ever, i have this doubt everytime i'm getting an id from an object,resource..


the best solution is use custom setting?

Bhawani SharmaBhawani Sharma
Yes, the best solution is to use custom setting. Anytime your useranem changes, you just need to adjust your custom setting and you are all set.

Another solution that i 've found is use the custom labels.

Do you know if is also a good solution?



Thank you very much!