Someone asked me how to find the StateCode value on a custom entity today, so the first thing I did was to search the Hosk CRM Blog.
A lot of people get confused between Status Reason and State code, if you do then read this blog post
CRM 2013 – Understanding Status and Status Reason – think before deleting them
quick guide
- StateCode = Status
- controls if the record is active/inactive
- StatusCode = Status Reason
- Status reason is linked to the state and gives the ability to different status reasons to a status.
CRM Developers point of view
Status and Status reason are both optionsets. Option set have an int value and a text value. The text value is meta data and the int value is held in the database on the database field.
Default entities have two status values
State
- Active = 0
- Inactive = 1
Status reason have different default values
Status Reason
- Active = 1
- Inactive = 2
The key piece of information is State and Status Reason have different values and this can be confusing, usually it goes down the lines of
I know State or Status Reason Active = 0 but which one is it.
A quick way to confirm what the Status or Status reason is, is to do an advanced find and use Status or Status Reason in a conditional. If you then click the download Fetch XML button you can see the values being used

No Magic Numbers
Magic numbers in development is when developers use number values for status or any option sets but give no indication what the values mean. This makes reading/understanding the code difficult and extremely frustrating because you often have to go and look up the values.
Wiki gives a good description of Magic numbers in code
here is a good explanation on stackoverflow – What is a magic number, and why is it bad?
Below you can see an example of magic number being used in code, what status is number 6! If you are lucky the developer will add a comment
if (incident.StatusCode.Equals(6)) { //do something incident cancelled }
if (incident.StatusCode.Equals(incident_statuscode.Canceled)) { //do something incident cancelled }
Don’t be lazy, create the Option Set enums and make your CRM code more readable. I have written a blog on why you should create Enumerations for optionsets.
CRM 2013 – Create Enumerations for option sets
There is a great tool to help you called
I reviewed the tool in the link belowCRM 2013 Tool – CRM Early Bound Generator
Filed under: CRM 2015
