Avoiding & Decoupling Hardcoded Values When Building Workflows – Best Practice
Ahmed Anwar, 05 August 2014
Let’s assume a customer wants to upload a document to a remote server when a field change. Connecting to that server requires some credential information.
Since these type of information usually change from different environments, then we have to update these information every time we export a solution and import it to another environment.
The worst case of storing credential information is to set it as magic strings like this:
As you know that magic strings can lead to painful problems. For example, these credentials might only work in certain environments but will not work on others. So we need a better way of changing the credentials without opening the plugin and recompiling every time before deployment.
Some developers might hard code the values in the InArgument when building the Workflow process and linking the custom workflow like this.
Actually, this is not a solution; you just moved the problem –hard coded values- from your plugin to the CRM. And again every time you deploy this workflow you have to change the values.
The best practice is to create a new entity called Settings, this entity will hold all the hard coded values for you. For example, if you need to hard code the value of ServerUrl, then create a new Single Line of Text field and added to the form.
After adding all the necessary fields on the Settings entity form, create a new custom Workflow as follow:
Simply, this workflow retrieves the first and the only record in the CRM organization and expose it as an OutArgument.
Compile the project and register it.
Now let’s get back to our previous Workflow Process.
In the workflow process builder, add the new custom workflow and put it as the first step.
As I said before the Settings record can be consumed as an OutArgument, right?
At this point I can update my existing custom workflows and remove any hard coded values like this.
In the Process Builder:
Awesome, now we have decoupled our business logic from any data that depends on environments.
You can safely export your custom workflows without worrying about updating values anymore.