No hardcode. Just labels

Sooner or later you will find youself in a position where it is so much easier to just hardcode a value rather than figuring out the dynamic solution. This can be hard in certain situations but most of the times the solutions is rather simple.

Does this sound familiar to you?

  • Only run this trigger if the profile name is ‘enter the profile name that surely never will change’
  • Make a callout to ‘enter the name of the endpoint that is 100% set in stone’
  • Name the subject of the to be created task always ‘enter value that changes two minutes after first user is confronted with it’

While this is easy and quick to implement by hardcoding it. It will be a pain when the requirements change later. And as we all know: There is nothing so stable as change.

So how can we deal with this in an appropriate manner? Custom Labels. They are great for: values that are reused, subject to change, and don’t need complex structure.

Where do we find custom labels in salesforce?

  • Setup – Custom Labels

How do we create a custom label?

  • New Custom Label
    • Name: What is this placeholder for?
    • Value: What value should it contain?

How do we use it?

Now instead of hardcoding a value in your code use the label that holds the value behind it and can be changed in the setup at any given time.

Example:

turns into

When developing a Callout on you might have a different endpoint for sandbox and production environment. When deploying you code you might have to switch it back and forth in order to call sandbox to sandbox and production to production. This can get annoying – or not.

‘Label’ is a set function in apex and you only need to add the name of your created label to get the value behind it.

Our label:

Value holds the endpoint. We can change it after deployment to the production value.

Custom labels can also be easily used inside a flow:

So in order to advance in the ranks of being an admin you have to ask yourself: Next time someone gives you a set value that surely will never change – how will you call the label?

Leave a Reply

Your email address will not be published. Required fields are marked *