CRM 2013 – How add a status reason using the CRM SDK

I recently had a tricky problem created by myself when I deleted a Status reason without fully engaging my brain.   Read more about it in the link below

CRM 2013 – Understanding Status and Status Reason – think before deleting them

Adding a Status Reason

If you try to add a status reason in CRM you will quickly find you can specify the label but not the value

optionSets

I’m not sure why it works like this but it was very frustrating when I was trying to add a Status Reason and wanted to specify the value.

Unsupported Database change

Someone did murmer the words

“Direct Database update”

My advice to CRM developers is to not even think about unsupported changes because Microsoft will un-support your CRM if they find out

Why you shouldn’t put unsupported customizations in Microsoft Dynamics CRM

Don’t do unsupported CRM customizations, 99.9 percent of the time there is another way and for the 0.1 percent of the time you should go back and tell the customer you can’t do it because it’s unsupported– Hosk Quotes

Start with the CRM SDK

When using the CRM SDK it usually a journey of discovery, reading one page, which leads you to another page, slowly pick up the clues, information and examples until you have enough information to tackle the problem.

I say it so often it’s made it to my Hosk Wisdom quotes page

Always start with the CRM SDK, you will find the answer to your current problem and solutions to your future CRM problems  – Hosk

 

I knew status reasons fields are option sets and option sets store information

Value (int number)

Text (description)

The description is held as Metadata.  If you want to learn more about metadata read my blog post Understanding CRM Metadata.

Using the Metadata services it’s possible to add, update and retrieve Metadata fields.

The Work with attribute metadata page had a section called Insert a new status value.  This was good but in the example it didn’t specify the value.  I needed to specify the value because I was trying to add back a previously deleted status reason, read this blog post to find out why I was trying to do that.

When using the CRM SDK you often need to earn the answer, this means you need to read, analyse and understand how parts of the CRM SDK work.   This is the sample code

InsertStatus

It’s adding a Statecode (e.g. Active/In Active) but I want the StatusCode.

I look at the msdn page for InsertStatusValueRequest Class, this links to the same code as above but on the left you can see more pages you can look at.

InsertStatus 1

It’s often worth looking at the related documentation for classes in CRM. In this case I look at the InsertStatusValueRequestProperties

InsertStatus 2

There it is Value, yippeee, that’s what I want to set. I can see from the documentation it’s optional, which would explain why it wasn’t included in the sample code.  Most people are not worried what the value of a new status reason is.

I have the theory, now it’s time to put it into practice.

CODE

Here is the code I used to create a status reason and specify the value of the status reason to be 100000001.  Quite how the number got created like that puzzled me, unless a previous developer used the same code!

 // Use InsertStatusValueRequest message to insert a new status 
 // in an existing status attribute. 
 // Create the request.
 InsertStatusValueRequest insertStatusValueRequest =
 new InsertStatusValueRequest
 {
 AttributeLogicalName = "statuscode",
 EntityLogicalName = Incident.EntityLogicalName,
 Label = new Label("Correspondence Withdrawn", 1033),
 StateCode = 1,
 Value = 100000001
 };

 // Execute the request and store newly inserted value 
 // for cleanup, used later part of this sample. 
 int _insertedStatusValue = ((InsertStatusValueResponse)this.crmService.Execute(
 insertStatusValueRequest)).NewOptionValue;

One last point of interest was I hard-coded the UK locale, Microsoft example just specified a variable but didn’t show where it was assigned.  I looked up the value on this page – Locale ID’s assigned by Microsoft.

As this was a one-off, I was worried about the hard coding

5 thoughts on “CRM 2013 – How add a status reason using the CRM SDK

  1. Joseph Gershgorin February 24, 2017 / 10:44 am

    I went a hackier/easier route:

    Once the add status dialog was up I used Chrome developer tools to find the “value” field in the Elements tab and:

    1.) Delete the disabled=”true” attribute (double click into it, select and remove via Chrome dev tools).

    I was then able to enter any number I wanted into the value field and it saved just fine.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.