The views and opinions expressed in this blog are those solely of the author(s) and do not necessarily reflect Microsoft’s current policy, position, or branding. For official announcements and guidance on Dynamics 365 apps and services, please visit the Microsoft Dynamics 365 Blog.
Personalized Community is here!
Quickly customize your community to find the content you seek.
Choose your path Increase your proficiency with the Dynamics 365 applications that you already use and learn more about the apps that interest you. Up your game with a learning path tailored to today's Dynamics 365 masterminds and designed to prepare you for industry-recognized Microsoft certifications.
Visit Microsoft Learn
2020 Release Wave 2Discover the latest updates and new features to Dynamics 365 planned through March 2021.
Release overview guides and videos Release Plan | Preview 2020 Release Wave 2 TimelineWatch the 2020 Release Wave 1 virtual launch event
Ace your Dynamics 365 deployment with packaged services delivered by expert consultants. | Explore service offerings
Connect with the ISV success team on the latest roadmap, developer tool for AppSource certification, and ISV community engagements | ISV self-service portal
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Program | Finance TechTalks | Customer Engagement TechTalks | Upcoming TechTalks
What are enumerations for option sets I hear some of you thinking?
I mentioned the benefits of early bound classes in my blog post CRM 2015 – Typescript is ready to go, definition files available on NuGet
Early bound code is creates a wrapper class round your entities. It includes all the fields in your entity. The benefits of early bound classes
Early bound code means a lot of the potential errors in the CRM code are found at compile time instead of run time. The big advantage of this is most of the errors are found and fixed by the developer rather than being found by the end customer.
Microsoft have a good post – Use the early bound entity classes in code
The advantages to using early-bound entity classes is that all type references are checked at compile time. The compiled executable contains the code necessary to invoke the types’ properties, methods, and events. For more information, see Use the early-bound entity classes for create, update, and delete.
Early bound classes create an entities file which includes all the names and types of the fields
The benefit of early bound code is you remove syntax errors and bring all type errors into compile time and not runtime, so your customers don’t see them.
You can type things like this
Contact contact = new Contact();
contact.FullName = "Marvellous Hosk"
Entity contactEntity = New Entity()
contactEntity["Fullname"] = "Marvellous Hosk"
You will notice full name is typed incorrectly, this error would only appear when the plugin is run because the compiler cannot check the late bound code for syntax errors.
Enumerations for option sets are what early bound code is to entities.
Creating enums for option sets helps get rid of magic numbers which is a code smell . A magic number is where code uses a number value with no explanation what the number is. The end result is confusing code which is hard to understand.
More reasons why magic numbers are bad
CRM options set and statuses are a classic breeding ground for magic numbers to magically spring up in code.
A kind-hearted developer who hasn’t learnt thought about using enums for optionsets might create a field called StatusReasonClosed = 9100000 or they might put a comment.
This is not a bad way but it involves manually creating the variables and brings in the possibility of different naming conventions, it’s better to automate the process and use enums. Below you can see an example of a magic number and a enum.
//do something incident cancelled
//do something incident cancelled
You have a few options for creating option set enums. The easiest tool to use is
CRM Early Bound Generator
I reviewed the tool in the link below
CRM 2013 Tool – CRM Early Bound Generator
The screen shot below shows you why it’s great, it has a lovely GUI interface.
The option set values will create a file enums like this
public enum incident_statuscode
InProgress = 1,
OnHold = 2,
WaitingforDetails = 3,
Researching = 4,
ProblemSolved = 5,
InformationProvided = 1000,
IncidentComplete = 100000000,
IncidentWithdrawn = 100000001,
Canceled = 6,
To use the optionset.cs file you need to copy it into your visual studio project with your CRM plugins. You can then use the enums instead of statecode and statuscode which will make your code easier to understand.
I added some status reasons, the next step was to regenerate the OptionSet.cs file with the enums in.
I did this using the CRM early bound generator tool, I copied the file to the project in visual studio, rebuilt the project and then I got loads of errors.
The CRM early bound generator create enum with this title
but the enums used in the code was like this
The question was how did they previously create the optionset enums? To the internet
Searching the internet I came to the this page
Create extensions for the code generation tool
It seems Microsoft had create a class library you could call via the command line to create option set enums.
The code is bundled with the CRM SDK
You have to open the project in visual studio, build it
This will then put the files into the debug\bin
You then need to modify the bat file called GenerateOptionSets.bat and change the url setting to point to your CRM instance (don’t forget to add the orgname of the CRM organisation you want to create the optionset enums for)
/codewriterfilter:”Microsoft.Crm.Sdk.Samples.FilteringService, GeneratePicklistEnums” ^
/codecustomization:”Microsoft.Crm.Sdk.Samples.CodeCustomizationService, GeneratePicklistEnums” ^
/namingservice:”Microsoft.Crm.Sdk.Samples.NamingService, GeneratePicklistEnums” ^
This created the OptionSet file in the same format as the previous.
If I was starting a new project I would use the CRM early bound generator
Business Applications communities