+ Start a Discussion
jvolkovjvolkov 

BEST PRACTICE: IDs v. Names

Is it best practice to use names when referencing fields such as user roles, profiles, or record types?  I've been told that it is and although I can see the value in this for something such as a username that will not change, I'm wondering why it is known as as best practice for something that can change such as user roles.

 

It seems that using the user role id with a note of the present name would be more beneficial since the ID will not change.  For example, if SOQL is writen with a WHERE statement that filters on the UserRole.Name called "Salesperson", and down the road that role name is changed to "Account Executive", won't that cause the SOQL query to return a null value?  Where is if the WHERE statement instead filters on UserRoleId even though the role name has been changed the ID remains the same value thus the SOQL query will still return the expected values.

 

Is this just a preference?  What is the main reason to use names instead of IDs?

 

Thanks

DodiDodi

You are generally safe if using id's and the record was created from the production instance, as the Ids will be copied to the sandbox environments. If you change the name, ids will be the same as long as they were oringally created in production. If a role is created in a sandbox, then when it is recreated in production, it will most likely have a new generated id...

 

I like to put things into maps<name, id>...then you can call it via the name and it will return the id.

 

Regards

dkadordkador

Certain objects have what we call a developer name.  In this case, filtering on the name is appropriate since it serves as a unique identifier for that row.

 

Other objects have name fields but they aren't developer names and need not be unique.  In that case you should use ID.

jvolkovjvolkov

Both good replies, thanks.