+ Start a Discussion
EnryEnry 

Get object Id

Hi,

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 ];
        post.CreatedById=u.id;

 

 

or

 

  post.CreatedById='005E0000003XNVp';

 

are there other way?

Which is the best practise?

 

Thanks in advantage.

BR

 

 

SLockardSLockard

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.

EnryEnry

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.
EnryEnry

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

Do you know if is also a good solution?

 

 

Thank you very much!