CRM 2013 – Investigation into replacing the Assign functionality

I had a small one line change request for a CRM 2013 implementation

Replace Assign functionality with will show users of the current users team.

I thought it would be easy to write a technical specification for this requirement.

First thoughts

Assign form is a special CRM form, I know this cannot be edited or manipulated (in a supported way).

This was much trickier than I initially thought it would be.  As an added bonus finding what users are in a team is not straight forward and you have to use a link entity when you do it in an advanced find.

Here are my thoughts of possibles solutions

Method 1

Create a new form to replace the Assign form.

Work

  • Create ASP page to include a filter lookup to CRM
  • Interact with CRM SDK to retrieve rows and update values
  • Ribbon button change

Pro’s

It would offer the functionality required

Con’s

It would take quite a bit of effort for a seemingly small change

 

Method 2

Open a standard lookup form, calling the lookup form in CRM and filter it

I researched this idea (which I found very difficult to find on the internet)

I go this page

HowTo: Open standard lookup window and get selected record(s)

CRM 2013 Open lookup as dialog in Custom HTML

This method wasn’t suitable because it’s unsupported and I couldn’t see how to filter the lookup.

Method 3

Using a dialog to first return the a list of teams the user is a member.

The user would select a team, press next

This would then return a list of users

Work

Create a dialog

Create custom workflows 1.  retrieve the users team, 2.  Retrieve users in team.

A ribbon button could call the dialog (This can be done using the Ribbon workbench Create a Dialog Short-Cut Ribbon Button)

Pro’s

It should deliver the functionality required

Con’s

Dialog’s are not the quickest

There is still quite a bit of work, two custom workflows and a dialog

 

Method 4

The easiest solution would be to add a user lookup on the form and if the set a value I could trigger a plugin or workflow to assign the records.

The lookup would need be filtered to the team.

I had heard CRM 2013 had prefilters, which allowed you to add filter to a lookup.  When investigating this it turns out you cannot create a filter with a linked entity and can only filter on values which exist on the SystemUser (user) entity.

This excellent blog post

CRM 2013 Using addCustomFilter() to get Filtered Lookup Field based on Linked Entity

Mentions the problem and you can get around it by using the addcustomview but this means you have to create a whole view.

Instead I tried out the functionality in Miss Dynamics CRM blog and use the addCustomFilter()

I thought I would try this out because I had not yet used the AddCustomFilter() solution and this seemed the easiest and quickest

First I created a view in the Advanced find (Why the advanced find is a CRM Developers best friend)

Assign Team problem

 

I then got downloaded the Fetch XML and put it into the FetchXML Builder

Assign Team problem 1

I could then get the guids, Aileen Gusni mentioned some workarounds in her blog to get  AddCustomFilter() to work you could use OData to retrieve the data you needed and


var Hosk = Hosk || { __namespace: true };
Hosk.Functions = Hosk.Functions || { __namespace: true };
Hosk.Functions.HoskTest = Hosk.Functions.HoskTest || { __namespace: true };

; (function () {

this.formLoad = function () {
addEventHandler();

};

function addEventHandler() {
// add the event handler for PreSearch Event
Xrm.Page.getControl("hosk_userassignid").addPreSearch(addFilter);
};

function addFilter() {
//create a filter xml
var filter =
"<filter type='and'>" +
"<condition attribute='systemuserid' operator='in'>" +
"<value>{9fed63fe-c0cc-e411-80c7-000c292122be}</value>" +
"<value>{cba24eea-bacc-e411-80c7-000c292122be}</value>" +
"<value>{2ed69167-0bcf-e411-80c7-000c292122be}</value>" +
"<value>{92c6d456-7bdf-e411-80c8-000c292122be}</value>" +
"</condition>" +
"</filter>";

//add filter
Xrm.Page.getControl("hosk_userassignid").addCustomFilter(filter);
}
}).apply(Hosk.Functions.HoskTest);

The code work, I had filtered the User lookup to those records.

Assign Team problem 2

The filtering kept true even when you clicked look up more records.

The next step I could retrieve the data from an OData call and Dynamically create the line

“<value>{cba24eea-bacc-e411-80c7-000c292122be}</value>” +

Then have a workflow/plugin to assign the records

Work

new field

Javascript change to add filtering

plugin/workflow to assign records.

PRO’s

The quickest solution in time to create and running time

Con’s

It’s different from the current assign button, this may confuse users

The option chosen was….

After spending time thinking about the problem and coming up with some choices the customer decided they didn’t want it.

This is something which happens to developers, one day you can create a change and the next day a customer decides they don’t want it and you have to take it out.

The customer didn’t want the change but I did get to investigate some CRM functionality I hadn’t used yet.

If you have a better solution please leave it in the comments.

CRM 2015 – How to change the option set default value

I recently had a few issues with Status Reasons and the number associated with a status reason

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

This lead me to have to add a Status Reason into CRM using CRM SDK because I couldn’t figure out where the default number of a new status reason was being calculated.

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

Default option set value

Today I figured out where the number was coming from when you create a new option set.  This value is set in the publisher.  There is a field called Option Value Prefix

OptionSet default value

When I go into my Hosk Solution, which uses the Hosk Publisher and create a new option set, it takes the default value from the publisher

OptionSet default value 1

The default value is also used for new Status Reasons

OptionSet default value 2

Customize from the default solution

The default solution includes all the customizations in CRM and this is the solution you access when you choose customize the system.

To understand solutions read more in the blog post below

CRM 2013 – Understanding Solutions and how they work

What values will be used when you add a new option set or status reason when editing the default solution

The status reason seemed to still use the default value in my new publisher.  This was unusual because I would have expected it to use the value in the Default publisher which was 10000 because this is the publisher specified in the Default Solution?  The default publisher is read only so this cannot be changed.

OptionSet default value 4

I did a bit more digging and found CRM seems to calculate which solution the entity is in and then uses the solution option value prefix even if you edit it using whilst in the default solution.

When I added a new optionset it did use the value in the default publisher

OptionSet default value 3

Don’t just test the happy path

The original title of this blog post was – Developers should stop being so happy.  I found the old title amusing because over the years I have met some extremely grumpy developers.  Classic old school developers who liked to sit in dark corner and not be disturbed.

The point of this article is grumpy developers can still skip and dance down the happy path when it comes to writing and testing their code.

I am continuing to read Code Complete 2 (it’s over 900 pages long it will take me a while), today I came to an interesting paragraph.

Avoid Failure

Petroski argues that many spectacular bridge failures have occurred because of focusing on previous successes and not adequately considering possible failure modes. He concludes that failures like the Tacoma Narrows bridge could have been avoided if the designers had carefully considered the ways the bridge might fail and not just copied the attributes of other successful designs

 

Developers are too happy

The process of development usually goes

  • Requirements gathered
  • Functional requirement specified
  • Technical requirements specified
  • Code written to deliver requirements
  • Test –> fix bugs cycle

Developers often focus on delivering the functionality, which is often known as following the happy path

Wiki describes as the Happy Path

In the context of software or information modeling, a happy path is a default scenario featuring no exceptional or error conditions, and comprises the sequence of activities executed if everything goes as expected.[1][2] For example, the happy path for a function validating credit card numbers would be where none of the validation rules raise an error, thus letting execution continue successfully to the end, generating a positive response.

 

Focusing on the happy path and creating the required functionality leaves room for bugs in production, when end users stray from the happy path into the alternative path or the exception path.

Developers naturally gravitate towards the happy path, often focusing on happy path testing.  One possible reason for this behaviour is the developer doesn’t see the functionality from the end users point of view, instead the functionality is viewed primarily from the code point of view.

It isn’t obvious to developer why end users would supply different values to what they are expecting, how the end users will use the functionality or what they are trying to achieve using the functionality.

Happy path testing can leave gaps for nulls, incorrect values and a host of error checking/validating errors to slip into a production system because no one has tested these paths.

The other area bugs come in is, is developer under estimate the random actions of end users.

Developers are not good testers

Developers often are poor at testing, this is a badly kept secret of the programming world (developers often dislike testing as well).  Developers are so poor at testing their own code it’s considered good practise to get other people to test the code.

One  reason CRM developers miss bugs in CRM is they test everything using the System Administrator role which avoids many permission bugs, I have written about in this blog

The System Administrator role is a benefit and a curse to CRM developers

Developers are not good at testing because after writing the code it’s difficult to step back and test the functionality rather than test the code.  This is similar to writers editing their own writing, they feel attached to their creation to the extent they find it hard to evaluate it dispassionately.

How to avoid failure

An effective way of avoiding failure is writing unit tests for the code.  The act of writing unit tests make the developer think

  • What should this functionality do e.g. what am I testing
  • What values (min, max) are allowed
  • What alternative values might be presented
  • What should happen when an error happens

Thinking before coding is always beneficial, sometimes developers are eager to get coding they miss this step out.

Once the code is written is unappealing for developers to write unit tests, so the best method of unit testing is to write unit tests while you create the code and use those unit tests to make sure it’s working as expected.

Thinking about errors and failures is very beneficial.  You will capture and avoid many errors.  The developer will decide how to handle errors and its usually a choice

  • Fail fast, abort action
  • continue and warn the user

Errors are not good but code which has blundered on and changed half the values it was expecting can make a greater mess and take longer to clean up, so it’s better to have a plan up front.

Don’t let these often simple errors slip out of your development environment to the end user, make sure you harden your code.

If you don’t write unit tests you should consider doing it

Why CRM Developers should unit test their code

Experiences of Unit testing with Microsoft Dynamics CRM Projects

If you still are not persuaded to write unit tests make sure you go through you code and look for potential errors, validation checks.  Make sure you have decided how you will handle errors in the code.  It’s much better/quicker/easier to manage errors in a dev environment than having to add it in at a later date.

More reading for you

TEST YOUR SAD PATH FIRST

Happy Path Testing

Experiences of Unit testing with Microsoft Dynamics CRM Projects

Many moons ago when I was a Java developer and learning how to code, I remember two things which had a big impact on my coding.  One was learning about Design patterns and the other was learning about Test Driven Development.

Writing unit tests for your code before you actually write the code seems like a strange thing to do and I didn’t meet many developers who did this or in fact bother writing unit test at all.

I found writing code which you knew was going to be tested with units tests forced me to write modular/reusable code.  It makes you write code which can be tested and usually this means each function does only one thing (because that’s easier to test).

The code is more likely to Single responsibility principle and each class and method doing one thing, the reason for this is because it’s easier to test something which only does one thing.

Pro and Con’s of Unit Testing

When unit tests and writing unit tests are mentioned this question is not usually far behind

What’s the benefit of writing extra code to test your code?

This can be a hard sell to some developers, it would be similar to trying to explain to a messy teenager the benefits of cleaning your room.   One the major complaints raised against writing tests is it takes extra time and we developers don’t have time to do it.

Writing unit tests involves thought and effort creating dummy data, deciding where to mock or stub to get enough data to test the code.  This can feel like a lot of wasted time when you can spend half a day getting the data created to test your code.

This effort can be too much for some developers and they will never like writing units and always view it as a waste of time.  This point of view is a common initial thought until the developer tries it and starts to see the benefits with the improved confidence of their code.

The irony of the aversion to unit testing is developers test their code by creating console apps or debugging the code. Unit testing is taking this process and making it reusable and self-contained

When writing unit test you stop and think about how the code and objects are going to be used.

Below is my experiences of unit testing and CRM, there are probably lots of companies and CRM developers are are unit testing their code

PRO’s of Unit Testing

  • confident the code works because it has been tested
  • Higher quality code (more modularised/reusable) to enable tests to be written
  • Any changes to the code can be tested quickly with no extra effort once tests have been written
  • unit tests can help developers new to a project understand how the code (should) work
  • very useful for testing refactored or bugfixed code
  • It’s easier to debug unit tests than deployed CRM code

CON’s of  Unit Testing

  • It takes extra time to write the unit tests and prepare the data
  • Unit tests have to be maintained and updated
  • You have to learn the testing framework and how to write tests (this is non-revenue generating time)
  • A feeling of not moving forward

My Experience of CRM Unit Testing

Unit tests and CRM in my experience have never really happened for a number of reasons/excuses, I have listed them below

Smaller projects

Smaller projects usually remove all non development tasks and reduce everything to the bar bones.  Unit tests were seen as non productive time the customer didn’t want to pay for.

Often small projects had a very limited about of code customizations

Time

Developers never have enough time to do everything the customer wants and usually the first thing to get snipped out is testing and unit tests.  See Why rushed projects/code doesn’t save time and reduces quality

Best Practise/Best Testing framework

Most CRM projects I have worked on haven’t used unit testing.  below I detail some of the reasons this (I’m not saying I agree with them but stating them).  I would say I wasn’t always keen to unit test my code but part of the reason was because the code in the projects wasn’t unit tested.

Cost

Some unit test frameworks do have a cost associated with them.  Microsoft Fakes used to only be included in the Ultimate version of Visual studio (which was very expensive) but it got voted into visual studio premium on Jan 30th 2013.

CRM developers didn’t seem to know (or maybe care) about this because I didn’t meet any CRM developers who liked or did any unit testing of the plugins/custom workflows until recently when I joined my current company.

Joining a project with no unit tests

The decision to unit test usually happens at the start of the project.  Many of the projects I have worked on didn’t have unit tests.

Legacy code impossible to test

Most people don’t bother to try and write unit tests for legacy code.  I can understand this because it would be very difficult

Plugin/Custom workflows hard to test

What do I mean hard to test?  Plugins/custom workflows need a IPluginExecutionContext context so you can test the plugins.  So to properly test a plugin you would need to create a IPluginExecutionContext with the correct attributes.

This would take quite a bit of effort.  To get an idea of all the data created you can use the debugging with the plugin profiler as an example.  When you debug with plugin registration tool, you set it to record the data for a certain plugin, run through triggering it in CRM.  This will create an xml file with all the fields and values.

You can then use this file to recreate the scenario you ran through in CRM and it will pass all those field choices into your Visual studio debugger.   This blog runs through the process

No insistence of unit tests

The projects I worked on, no one insisted unit tests were created, apart from one project.   Instead the code would be tested by each developer and then put forward to an integration test.
Unit testing has to be a company or project policy or something which developers of a company are doing.

Senior developers weren’t doing it

Junior developers learn good and bad habits from senior developers.  In most of the projects I have worked on there were no unit tests.  I haven’t met any (1 or 2) CRM developers who write unit tests and most do not promote unit testing.

Examples to copy

When you type CRM and unit tests you do not get many results back and this suggests unit testing CRM code is not common practise.  More examples and blog posts would make it easier for CRM developers to use this code in their projects.

How Hosk tested his Code

The way I developed my code customizations was to create the code in separate class outside of the plugin code.  I would then create a console app and test the code by passing in a IOrganisationservice, TracingService and Entity.  This allowed me to walk through the code and retrieve data from the development CRM organisation.

I’m not entirely sure how other developers would test their code, I have a sneaky suspicion most would use integration testing (deploy and test it using CRM) and look at the log files.

This article was in draft status for quite a while and in the mean time I have started writing unit tests. I first tried using RhinoMocks and then found Microsoft fakes was free in the premium edition.

I have written the article

Why CRM Developers should unit test their code

I am now a firm believer CRM developers should be writing units for their customizations and there isn’t any excuses why unit tests are not written.  The lack of unit tests is not limited to CRM Development but wide spread in a lot of programming.

There are some difficulties in writing unit tests in CRM but it is achievable using Microsoft fakes.

CRM Project with Tests

There has been a drive to test the code in a project and the developers are writing tests for code which already exists.

I won’t lie this is a tricky a task.  When code hasn’t been written with testing in mind it can be very difficult to test.

I will say some tests are definitely better than no tests and the code was decently structured so a lot of the code plugins are using is now separately tested.

It has taken some time for developers to get to grips with the testing framework and the testing methodology.  Testing is a different mindset and it takes  bit of time to learn the tips and tricks and common testing methods.

The benefits have been

  • Tests any developer can run
  • The business logic is quite complex in the project, so having a method to step through parts of the code is very useful.
  • Tests can be run be developers who didn’t create the code, much quicker than trying to test the code yourself.
  • Easier to see if your code has broken other parts of system

Plugins and testing

Some developers can have a blinkered view of testing the plugins themselves.  Plugins are not always easy to test and sometimes a plugin doesn’t do much.

A create plugin might validate values

A plugin could update related records

I personally remove all the code from the plugin and create separate classes where the constructors take IOrganisationService and TracingService.  The method can then take an entity record which has triggered the plugin.

Unit tests can be run against the code which is split up into small focused classes/methods.

At the end you plug all the small tested parts together and link it to the plugin.

Why have I changed my mind about unit tests and CRM

 

Looking at the list of reasons why unit testing was done on the CRM projects I worked, I can see they are not reasons they are excuses.   Most disappointingly is some of the excuses for not writing unit code are excuses I have been making to myself.

I have been reading  Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin) and Uncle Bob mentions we as developers/programmers should be testing our code and our code should be good.

He has this quote in Clean Code

Why does good code rot so quickly into bad code? We have lots of explanations for it. We complain that the requirements changed in ways that thwart the original design. We bemoan the schedules that were too tight to do things right. We blather about stupid managers and intolerant customers and useless marketing types and telephone sanitizers. But the fault, dear Dilbert, is not in our stars, but in ourselves. We are unprofessional.

This year I have been thinking about the quality of CRM code/customizations and have been coming to the conclusion the quality of code in CRM projects need to be better.

I re read my post –  The Frustrations of a CRM Developer many of the frustrations come from poor code.

Quite a few of the CRM Developers articles I have written this year are focused on creating quality customizations

Before reading Clean code I knew I should be writing unit tests but took the easy option of not doing it.  After writing some unit tests for my current development, it has been so much easier refactoring my code and making changes.  Having 40 unit tests testing code change gives me confidence the refactored code works.

Unit testing helps projects from falling into a state of Legacy code because writing code you can tests makes the developer

  • Think about the code design
  • Think about what the functionality should do
  • Test the code more thoroughly
  • Creates reusable tests to all developers on the project

I hope this and future blog posts will get more CRM developers to start unit testing their code.

The biggest driver for unit testing code should come from the developer, who has pride in their work and wants to deliver high quality code.  This is an attitude, it’s a desire to create quality code.

The benefits of unit testing are undeniable and if you start unit testing your work you will soon enjoy the benefits.  If you are already writing unit tests then keep up the good work and write a blog about it to encourage other CRM developers.

CRM 2013 – Create Enumerations for option sets

What are enumerations for option sets I hear some of you thinking?

start with early bound classes.

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

  • No typos
  • no type errors
  • easy to read and understand code

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"

Rather than


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.

What are Enumerations for option sets?

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

  • It’s hard to maintain
  • Difficult to understand
  • change nightmare if numbers are used in many places

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.

if (incident.StatusCode.Equals(6))
{
//do something incident cancelled
}

or

if (incident.StatusCode.Equals(incident_statuscode.Canceled))
{

//do something incident cancelled

}

Creating the option set enums file

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


[System.Runtime.Serialization.DataContractAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "6.1.0001.0123")]
public enum incident_statuscode
{

[System.Runtime.Serialization.EnumMemberAttribute()]
InProgress = 1,

[System.Runtime.Serialization.EnumMemberAttribute()]
OnHold = 2,

[System.Runtime.Serialization.EnumMemberAttribute()]
WaitingforDetails = 3,

[System.Runtime.Serialization.EnumMemberAttribute()]
Researching = 4,

[System.Runtime.Serialization.EnumMemberAttribute()]
ProblemSolved = 5,

[System.Runtime.Serialization.EnumMemberAttribute()]
InformationProvided = 1000,

[System.Runtime.Serialization.EnumMemberAttribute()]
IncidentComplete = 100000000,

[System.Runtime.Serialization.EnumMemberAttribute()]
IncidentWithdrawn = 100000001,

[System.Runtime.Serialization.EnumMemberAttribute()]
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.

Potential problems with Existing Option set enums

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

new_contributionrequest_new_ContributionStatus

but the enums used in the code was like this

new_contributionrequestnew_ContributionStatus

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

SDK\SampleCode\CS\CrmSvcUtilExtensions\GeneratePicklistEnums

You have to open the project in visual studio, build it

This will then put the files into the debug\bin

SDK\SampleCode\CS\CrmSvcUtilExtensions\GeneratePicklistEnums\bin\Debug

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)

CrmSvcUtil.exe ^
/codewriterfilter:”Microsoft.Crm.Sdk.Samples.FilteringService, GeneratePicklistEnums” ^
/codecustomization:”Microsoft.Crm.Sdk.Samples.CodeCustomizationService, GeneratePicklistEnums” ^
/namingservice:”Microsoft.Crm.Sdk.Samples.NamingService, GeneratePicklistEnums” ^
/url:http://CRMSERVER/ORGNAME/XRMServices/2011/Organization.svc ^
/out:OptionSets.cs

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

 

 

What is max degree of parallelism option setting in SQL Server?

A ticket opened recently with Microsoft support involved changing a the  max degree of parallelism on the SQL server.

I had not used or heard of this setting before, which isn’t a surprise I tend to leave SQL Server settings to the people who know more about the consequences.

Early in the year I did look at effects of changing the SQL Server trace flag 8780 which resulted in reports running faster but is unsupported unless Microsoft support teams advices you to do it!

CRM SQL Query go faster trick – SQL Server trace flag 8780

My goal in this blog is to give an overview about max degree of parallelism Option and some links to more information.

Remember to always consider the effects of changing flags and options, they can change results/efficiency in other SQL queries

What is max degree of parallelism Option 

The place to start is the MSDN article

max degree of parallelism Option

When SQL Server runs on a computer with more than one microprocessor or CPU, it detects the best degree of parallelism, that is, the number of processors employed to run a single statement, for each parallel plan execution. You can use the max degree of parallelism option to limit the number of processors to use in parallel plan execution. The default value of 0 uses all available processors up to 64 CPUs

You can learn more about Degree of Parallelism in this specific article

This setting is sometimes known as MAXDOP (max degree of parallelism)

Hosk understanding

This max degree of parallelism controls how many processors in the SQL server can be used on any one single query.

The default value for Max degree of parallelism is set to 0, this means there is no limit to the number of processors.

If you change the option to a number e.g. 1, you are then limiting the SQL server to use one processor for each query.

This setting seems to be changed when users want to limit the number of processors used in single queries to stop SQL server taking all the processors for single queries.

The setting is also used to increase the number to improve performance in single queries.

This is a server side setting so you need to consider how changing this setting could effect the performance of all SQL queries on the SQL server (and maybe not just CRM)

More information

This article is very useful because it mentions limitations and restrictions and recommedations

Configure the max degree of parallelism Server Configuration Option

This recommendations I found interesting

  • This option is an advanced option and should be changed only by an experienced database administrator or certified SQL Server technician.
  • To enable the server to determine the maximum degree of parallelism, set this option to 0, the default value. Setting maximum degree of parallelism to 0 allows SQL Server to use all the available processors up to 64 processors. To suppress parallel plan generation, set max degree of parallelism to 1. Set the value to a number from 1 to 32,767 to specify the maximum number of processor cores that can be used by a single query execution. If a value greater than the number of available processors is specified, the actual number of available processors is used. If the computer has only one processor, the max degree of parallelism value is ignored.
  • You can override the max degree of parallelism value in queries by specifying the MAXDOP query hint in the query statement. For more information, see Query Hints (Transact-SQL).

It seems to suggest you can set it on an individual query basis.

Here is another article

Recommendations and guidelines for the “max degree of parallelism” configuration option in SQL Server

How to change the Max degree of parallelism Option

It’s an SQL option so set the value using an sql statement.

I took the code from this page

https://msdn.microsoft.com/en-us/library/dd979074(v=crm.6).aspx


exec sp_configure 'show adv', 1;

RECONFIGURE WITH OVERRIDE;

exec sp_configure 'max degree', 1

RECONFIGURE WITH OVERRIDE;

Exec sp_configure

This is a server wide setting, so it would effect all the CRM organisations you have on the SQL Server and other databases.

How does is this setting used with CRM

Take into account I have not had any personal experience changing this setting, I saw it was mentioned as a possible resolution, I decided to investigate.  

This setting usually effects CRM if you have multiple long running queries.  It can cause the CRM performance to slow.

This article talks about CRM 4

Microsoft Dynamics CRM Server performance is less than expected

In the example above it seems the SQL server was using all the processors on one long running query which caused performance of CRM in general to slow down.  It limits sets the MAXDOP to 1 to stop this.

This article is very good and the blog looks into the setting in detail

Max degree of Parallelism in SQL Server

The article concludes it’s probably best to tinker with this setting only if your having slow performance from your SQL server/CRM.

Other articles

Case Study: Part 1: CXPACKET Wait Stats & ‘max degree of parallelism’ Option: Introduction to Using Wait Stats to Identify & Remediate Query Parallelism Bottlenecks

Case Study: Part 2: CXPACKET Wait Stats & ‘max degree of parallelism’ Option: Suppressing Query Parallelism Eliminated CXPACKET Waits & Liberated 30% of CPU

Interesting forum discussions

Max degree of parallelism setting

Hosk’s Top CRM Articles of the week – 24th April

Article of the Week

New SDK Capabilities – CRM Online 2015 Update 1 release

Fantastic articles from CRM MVP Daniel Cai on the new features in CRM 2015 SP 1

Best of the Rest

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

How to add a status reason using the CRM SDK so you can specify the state

Where to learn about data visualisation and building better charts

CRM MVP Adam Vero shares some great resources to for making graphs

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

Understanding status and status reasons in CRM and be careful when deleting them.

What’s new in CRM 2015 SP1 for developers, customizers and admins

Hosk takes a look at what’s new for CRM 2015 SP1

Microsoft Dynamics CRM Customization: Copy Address Dialog

Copy the address dialog

Building Rich UI Extensions for Dynamics CRM using SparkleXRM

two videos created to show yyou how to use SparkleXRM

How to gather and document a CRM requirements specification – The ebook

A good free ebook on gathering requirements for CRM projects

Microsoft.Xrm.Client Part 1: CrmOrganizationServiceContext and when should I use it?

An article from Scot Durow from a while ago but I only read this week

Dynamics CRM Spring Wave Update 1: Folder Level Tracking

How folder level tracking will work

How to “Convert” Checkboxes into a Percentage Value in 2015 Dynamics CRM #CRM2015

A neat solution to converting checkboxes into percentages

Understanding the new UI in Microsoft Social Engagement

A detailed post on the new UI for the social engagement functionality

Best Practice: Using Built-in Address versus Custom Entity in Dynamics CRM

CRM 2013 – How to add reports to the sitemap

How to add reports to the sitemap, use XRMToolbox

PROGRAMMING LINKS

Just Say No to More End-to-End Tests

Non Code

The Last Thing We Need Right Now is a Vision Statement

Miracle Grow for Your Brain

Defending Darwin – some students will never accept human evolution

Last Weeks Top CRM Articles

Hosk’s Top CRM Articles of the week – 17th April

Useful Hosk Links

Hosk list Of CRM 2013 Tools

A list and review of CRM 2013 tools, this will probably work in CRM 2015 as well

Hosk’s CRM Developer Articles

A collection of my favourite CRM Developer articles I have written

MB2-703 – CRM 2013 Customization and Configuration Certification Information

All the CRM 2013 content to help you pass the exam

HoskWisdom – Hosk Developer Quotes

 Words of Wisdom from the Hosk.  I have written over 900 articles, surely I should have said a few memorable things

 

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

What’s new in CRM 2015 SP1 for developers, customizers and admins

When there is a new release for CRM the SDK publishes a what’s new.  I recommend CRM developers read this, I have written a blog post about the subject

Why developers should read the CRM SDK what’s new

Here is my quick review of the last one

CRM 2015 – Hosk takes a peak at the CRM 2015 SDK

CRM 2015 and this update and with this particular update Microsoft seem to be adding lots of useful functionality for developers rather than front end functionality.  This is one of the major development releases for Microsoft Dynamics CRM since I have been a CRM developer.

The consolidation of functionality CRM 2015 is a good idea and will be beneficial to CRM suppliers and CRM developers.

The documentation for CRM 2015 SP1 is excellent.  There are a lot of developer and subtitle updates and added features, all of which are well documentation so thumbs up Microsoft.

My goal of this blog is to summerize the information of the CRM 2015 SP1 update

  • Give useful links
  • condense information
  • Hosk thoughts on the functionality

Useful links for Administrator’s and Customizers

Below is a collection of links about CRM 2015 SP1 in general and not focusing on the Developer enhancements.

CRM Online 2015 Update 1: official pre-release content is here!

This what’s (new is good for non Developer related highlights)

CRM 2015 Update 1 videos

What’s new for administrators and customizers in Microsoft Dynamics CRM 2015 and CRM Online

What’s new for developers

There is so many juicy news bits of functionality, I recommend you read the what’s new for Developers in it’s full glory.

What’s new for Microsoft Dynamics CRM Online 2015 Update 1

Upgrade considerations

This technet article was interesting

Before you upgrade: issues and consideration

IE 10 and 11 are only supported in CRM 2015

Those upgrading from CRM 2013, in CRM 2013 CRM had base and extensionBase tables but these are merged.  There is a section called

Base and ExtensionBase table merge cannot be deferred (if you didn’t know Microsoft got rid of the ExtensionBase table and now only have one table for each entity)

To find out the amount of table data that will be merged during upgrade, see Determine how much ExtensionBase table data will need to merge.

This is a great example why you should never write customizations (web services, reports or plugins,  I’m sure there will be some developers who have written reports based on the extension and base table which will all break when upgrading to CRM 2015.

The page states in place upgrades are not supported for these products (which is worth keeping in mind)

  • Microsoft Dynamics CRM 2013 Reporting Extensions (on-premises only)
  • Microsoft Dynamics CRM 2013 Email Router
  • Microsoft Dynamics CRM List Component for SharePoint Server

Updating CRM 2015 online

I will admit to not knowing if this was changed or if I have just read about it.

Manage Microsoft Dynamics CRM Online updates

The technet article mentions the functionality of delaying and choosing a time to schedule your CRM update.  The bottom line is you have to approve the update before it is applied.

The article mentions you can choose not to deploy an update.  I have wondering what happened to CRM online instances which automatically updated and the chaos in panic testing this would bring about with a set deadline in place.  Some CRM organisation have lots of customizations need a lot of testing.  so it can be some time before a company wants to apply a CRM update

What’s new for developers

My initial plan was to review a lot of the changes in a series of blogs but I noticed today that CRM MVP Daniel Cai has already done it.  He has done a great job, awesome work.  So I changed my plan to build on his good work rather than duplicate it.

New SDK Capabilities – CRM Online 2015 Update 1 release

Daniel has written 9individual blog posts which go into detail about the new functionality and with the all important screen shots.  Daniel has done such a great job there isn’t any point in me doing it again.  I have linked to his blogs and given a quick summary of the functionality.

I recommend all CRM developers read Daniels blog links to get a good idea of the new functionality and don’t just read my quick summary

Fantastic functionality which allows you to create alternative Keys.  Alternative key doesn’t make it initially obvious what the functionality does (to me anyway).  It allows you to create a new unique identifier for a record made up of a combination of columns (e.g. think composite key maybe foreign key).

The columns used to create the alternate key must be either String/Integer/Decimal.

CRM will index these in the background and you can monitor the creation –  Monitor index creation for alternate keys

The result of this means it’s easier to create/update records in CRM and in applications integrating with CRM.

When I read about Upsert I thought it was a joke, Upsert.  The functionality although sounds funny is a great idea.  The premise is it’s an Update and insert.

When using an upsert the functionality checks to see if the record exists, if it does it updates the record. If the record doesn’t exist it creates the record.

Simple but very effective because for integrating applications it avoids the round trip of checking to see if records exist and then choose to create or update.

Another pragmatic piece of code.  This improved functionality allows standard calls to do things like assign values, setting state, before the update you need to use special messages.

This post will give you more details

Perform specialized operations using Update

CRM Online instances can turn on plugin tracing logging, which creates a trace file you can look at.  Very useful considering you couldn’t view any logs before (because you can’t get onto the server).  This would allow you to view the values used in your plugins.

Entity change tracking allows you to detect if the data you retrieve has changed.  This would enable you to do a retrieve and only return the data which has changed from your initial retrieve.

This page from Microsoft will give you more detail and the title explains it’s intended use

Use change tracking to synchronize data with external systems

More CRM integration changes.  This functionality allows you to batch a bunch of actions into one transaction.  You can roll the transaction back if any of them error.  Microsoft Calls this Multiple message execution

Very useful for code integrating with CRM.  Read more on the CRM SDK – Execute messages in a single database transaction

This is a biggie.  This functionality tells you if the data you are going to update has been changed in between you retrieving and updating the information.

It uses a RowVersion value to see if the value being updated is the latest and if so it will throw an error, which means you can retrieve the latest and then update.

CRM SDK link – Reduce potential data loss using optimistic concurrency.

You can now check what version the CRM server is running on.  This offers you the ability to create branches in the code to use different functionality.  This could be useful in commercial solutions or maybe creating solutions to work across different CRM versions.

Try the Web API preview

This is interesting, they are offering you a preview but you shouldn’t use it in production environment (It’s not finished yet I guess).  You might be thinking, why do I care about a preview.   The new web api uses OData v4, OAuth 2, and JSON

It’s this line which intrigues me

The preview release allows you to give feedback and get an understanding of what is coming in a future release. The Web API preview shouldn’t be used on production environments and no support is provided. For more information on the next endpoint,

Learn more here – Web API Preview.

Improved Sub Grid Javascript

You can change the selected view for the Javascript and get the entity references easier

Non developer improvements

A quick summary of what’s in here

What’s new

Improved CRM navigation

quick video

The change now shows all the potential links when you click on an area e.g. sales.

So where before you had to scroll along to see the options, now they are all displayed

The recently viewed is a global tool (e.g. available in many parts of CRM) allows you to view recently viewed records and pin items

Themes

You can change the colours and add a logo.  This is like changing your theme in windows.  It will change all the colours of the bar

Watch a short video (2:45) about themes

Theming post from powerobjects

Onenote integration

Track emails with folders

This is a good productivity improvement.  It allows you to track emails in CRM by copying them to a folder.

folder level tracking

Export to Excel

It shows how much CRM developers use excel to export and import data by the fact I was quite excited by this enhancement.

This has been completely rewritten and you can now import/export 100000 instead of the previous limit of 10000.  This is excellent but strikes me as one of those changes which should have be done a while ago but lets not complain because its in now

New export to excel functionality

CRM Gui Customization changes (non code)

This page is very useful

What’s new for administrators and customizers in Microsoft Dynamics CRM 2015 and CRM Online

This page above includes all the changes and updates for non code customizations, which I sometimes call GUI customizations.  It does also mention some config settings

Older than X

this was limited to months but now you can specify minutes, hours, day, week, years.

Older than 5 minutes

Older than 1 year

Older than 2 weeks

Daniel Cai has written a blog post on this if you want to learn more New Query Operators

Date only field

CRM 2015 SP1 can have Date only fields and Time zone indepdent.  This is different from creating Date and time fields but only displaying the date.  Why did it take so long to add this?

Clear field with Business rules

Microsoft keep adding improvements to business rules which is good and need.  The next improvement allows you to clear fields.

Auto Creating CRM records not just for cases

Previous versions of CRM you could create Cases using emails and other social posts.  The documentation says other social messages but for most people this is emails and perhaps phone activities.

This functionality has been extended to sales and marketing.  So emails and other activities can create System or custom entities.

Rollup fields can use AVG

Rollup field can use AVG in calcluations

Mobile phone and tablet apps have been improved and I will expect this to be mentioned in every new release for the forthcoming future.

Final thoughts

A theme of this update is synchronizing CRM with external systems (applications, web services, 3rd party software).  Microsoft have made it much easier and quicker to synchronise data between CRM and other systems whilst adding functionality for checking data (e.g. to avoid overwriting already changed data)

Microsoft shows you how the changes work together

Synchronize CRM data with external systems

These two images from the page above succinctly show you the potential improvements

Before CRM 2015 SP1

After

I can understand these improvements are very useful but what puzzles me slightly is the sudden motivation to add this functionality into CRM 2015.   If anyone knows the reason or has an idea please add a comment.

Most of the CRM projects I have worked on have been extremely customized and used Microsoft Dynamics CRM for XRM developer.  CRM was the framework to build a solution for the customer.  The new updates in CRM 2015 SP1 take this a step further by making it easier to not only create the main solution but create applications to integrate with the CRM solution.

This fits in with Microsoft’s recent tactics of acquiring applications/software and integrating them with Microsoft Dynamics CRM (Parature, Unified Service Desk, MDM, Social engagement).

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

Yesterday I did a complete brain fart.  I was an idiot, I made a mistake and I admit it.

“There’s no way that you can live an adequate life without many mistakes.  In fact, one trick in life is to get so you can handle mistakes. Failure to handle psychological denial is a common way for people to go broke.”

Charlie Munger on Mistakes

Here are some more Charlie Munger quotes on mistakes

Good advice is to admit your mistakes fast and move on.

The earlier you admit making a mistake the more time you have to fix it – HoskWidsom 

The Task – My Mission

The functionality I was going to change on the Incident entity.  It was to move a status reason from Resolved to Cancelled.

StateCode and StatusCode

To understand what needed to be done we need to understand Statecode and StatusCode.  It’s one of those areas which new CRM developers struggle to understand because statecode and statuscode sound similar, it gets a bit more confusing when the display names are different.

Lets clear it up

StateCode = State

StatusCode = Status Reason

The state/Statecode is the status of the record and holds its current state, most CRM entities have two status = Active/Inactive

The default/minimum

0:active

1:inactive

Many of the out of box entities have more states.  The states are commonly used to control the state of the record and to filter records in views in CRM e.g. Active contacts, Inactive contacts, Completed Tasks, Pending Emails

How is Statuscode linked

One State can have many statuscodes (status reasons) and they work like a linked/Dependant option set.

This article Status and Status Reason values in Dynamics CRM 2013 lists all the out of the box Status and Status Reason’s, I’m guessing this information is probably buried somewhere in the CRM SDK but I couldn’t find it.  The two tables below come from the article.

State Status Reason
0:Active 1:Active
1:Inactive 2:Inactive

Many entities in CRM have lots of status reasons.  Here are the

Status                                        Status Reason

Case 0:Active 1:In Progress
2:On Hold
3:Waiting for Details
4:Researching
1:Resolved 5:Problem Solved
1000:Information Provided
2:Canceled 6:Canceled

A few things we know

  • Status is the status of a record
  • Status Reason is the detailed reason or stage of the current status.
  • Status reason allows you to create different stages/reasons for a record.  In the case example a case record is created and I view the status reason as to the detailed explanation of status or the stage.

An active case (e.g. case with status of active) can have a status reason of either in progress, On Hold, Waiting for Details, Researching.

Using the status reasons as a filter you can create views/reports and dashboards showing the records in the different stages.

Option sets

An important point in my story is status and status reasons are special/linked option sets. Option sets in CRM hold two values

  • int value
  • Text value

The int value is held in the database and the text/string value is held as metadata.  I recently blogged about Metadata

CRM 2015 – Understanding CRM Metadata

If you look at the table of case again you can see the numbers which specify the int value of the Status and Status Reasons

Status and Status Reason act slightly differently from normal option sets.  When you add an Option set in CRM it lets you specify the text and the number.

optionSets 1

Status Reason doesn’t let you specify the number value

optionSets

Custom entities have two Status – Active/In Active

For Out of the box entities you cannot add additional Status values.

What I did

I was working on Case (incident schema name) and the task was to remove a status from resolved to cancelled.  The status reason (statuscode) was called Case Withdrawn.

I deleted the Status Reason from Resolved

Created a new Status Reason in Canceled to Case Withdrawn

Things quickly got bad, can you guess what mistakes I have made, here are a couple of things to think about

  • Code using the case status reasons
  • Data in CRM which was set to the Resolved – Case Withdrawn
  • How to set it back

Code

The current system I am working on has a lot of customizations.  Users cannot directly change the status\Status reason of a case record.  Code will be activated by completed tasks or other actions in the system and the status will automatically change.

When I tried to withdraw a case I got an error with this message

961080001 is not a valid status code for state code IncidentState.Resolved on incident with Id 00000000-0000-0000-0000-000000000000

The reason for this error is because the code was using closeIncidentRequest instead of the classic SetStateRequest.

The reason a closeIncidentRequest was being used is because this allows you to specify the values in the IncidentResolution.

If you have closed a case using the CRM GUI, it pops up a form which collects a few values.  If my memory serves me correctly, I believe there is an out of the box report which uses the incident resolution values (possibly calculating the time spent via the time on activities)

The change from of the Case Withdrawn status reason meant the code was now trying to set the case to resolved using a status reason from cancelled.  This didn’t work.

The reason why the code compiled was because I regenerated the Optionset enums file, so the name of the status reason was the same but the value was different.  This value difference would cause me more problems.

Data

The really stupid part of this story is I have done this exact thing before, I did find this problem quicker than last time.  The morale of the story is you have to respect the data

CRM 2013 – What happens when you delete a status reason currently in use

When you change a status reason or remove an option set value is you might have a lot of records who have an option set number value which doesn’t exist.  You must create a plan to Migrate the data.

Better choices than deleting

Rename the Status record and reuse.  Migrate the data as a manual step

rename the status record to DO NOT USE. Migrate the data as a manual step.

It’s important to understand you will need to migrate or at least identify the records which will need migrating before you import and publish your changes.

To find the data with Status reasons pointing to the deleted I had a bit of a tricky problem.  I didn’t have access to the SQL Server, so I instead I used

fxb FetchXML Builder created Jonas Rapp

I had been meaning to test it out and it worked very well.  To use it all you need to do is drop the dll in your XRMToolBox folder, awesome.

I used this to find the cases which I needed to do something with.

How to set it back

It turned out the bug was a bit bigger than initially anticipated and it was going to be fixed in this release, which mean I had to change everything back.

This proved to be trickier than I originally thought it would be due to the CRM GUI doesn’t let you specify the number for the Status Reason.

I could add in a Status Reason called Case Withdrawn but it would have a completely different int/value number, arghghg.

We were going to restore the database but then I worked out how to add a status reason using the CRM SDK which allow me to specify the number for the status reason.

Stop, think and then act

I often recommend CRM developers should stop and think before they code, this is useful advice before you make any customizations.

When making changes, explain the changes first to your Cardboard developer buddy (Why all developers should be friends with a cardboard developer) and work out the reasons why it might be a bad idea.

A major part of CRM development is avoiding mistakes, missing the gotchas (which is why experience is usually very useful read Why .NET developers struggle with CRM Development to understand why) and this was a classic case of act in haste and take half a day to tidy up CRM.