How to Change Greyed Out Status Reason Values in Dynamics 365

Dominic Jarvis, 27 September 2017

Recently I’ve been working on a system where I’ve had to recreate a fair few entities, and have needed them to work with an existing code base. The issue I came across was that in the original versions of the entities, the status reasons on different entities had been made with inconsistent values. When remaking these, in order to have the reference in the code remain correct, I needed to remake the entities keeping the value of each of the status reasons the same. This was an issue, because in the web client it is not possible to manually edit the value of a status reason option.

image

My solution was to create the options in Dynamics 365 programmatically from a console app.

How can we achieve this?

As it turns out, it’s rather simple to create a status reason option programmatically. It simply requires executing a SDK message with the appropriate inputs. The basic code required is given below, but it’s a good idea to add some console output to ensure that the code is working as expected.

clip_image002

Using the above code in a console application can allow you to populate status reason options quickly, and allows greater flexibility than creating the values in Dynamics 365. In the below example, I add a status reason option to a custom ‘mag_event’ entity.

clip_image002[6]

The above code creates a new status reason option called “Registrations Closed”, with a value of 12. This is just an arbitrary value, and can be set to any whole number. This is very useful when needing to recreate status reasons, or in the instance you accidentally delete a status reason, and you need to recreate it with the same value in Dynamics 365.

Using this approach with a console app, error handling is advised, as the code will throw an error if you attempt to create a status reason with a value that is already being used by something on the same entity (ie. Creating a status reason with a value of 1 while the default ‘Active’ status reason still exists).