Dynamics 365 – Error when exporting a solution

If debugging is the process of removing software bugs, then programming must be the process of putting them in. Edsger Dijkstra

 

I got an error message when the ALM tool was exporting an unmanaged solution from an environment and packaging it up before deploying it.

The ALM tool sometimes errors but usually around data. This problem needed more thinking to resolve it because I wasn’t sure the cause and I hadn’t seen it before.

When I’m not sure of the cause, I remember this quote

If I had an hour to solve a problem, I’d spend 55 minutes thinking about the problem and 5 minutes thinking about solutions — Albert Einstein

Before I solve a problem, I ask myself some questions.

  • What did I know about the deployment?
  • What are the facts?
  • What assumptions I am making?

Think before action, create a plan and execute

When debugging, eliminate what isn’t causing the problem and whatever code or customization is left must be the culprit. #HoskWisdom

The error message

The error message was my best lead, here it is

Unhandled exception:

Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]

Message: An unexpected error occurred.Detail:

<OrganizationServiceFault xmlns:i=”http://www.w3.org/2001/XMLSchema-instance“ xmlns=”http://schemas.microsoft.com/xrm/2011/Contracts“>

<ActivityId>cbce1d3a-44d6–47d0-b319-f082f16a71f5</ActivityId>

<ErrorCode>-2147220970</ErrorCode>

<ErrorDetails xmlns:d2p1=”http://schemas.datacontract.org/2004/07/System.Collections.Generic“>

<KeyValuePairOfstringanyType>

<d2p1:key>ApiExceptionSourceKey</d2p1:key>

<d2p1:value xmlns:d4p1=”http://www.w3.org/2001/XMLSchema“ i:type=”d4p1:string”>Plugin/Microsoft.Crm.WebServices.ExportXmlService</d2p1:value>

</KeyValuePairOfstringanyType>

<KeyValuePairOfstringanyType>

<d2p1:key>ApiOriginalExceptionKey</d2p1:key>

<d2p1:value xmlns:d4p1=”http://www.w3.org/2001/XMLSchema“ i:type=”d4p1:string”>Microsoft.Crm.CrmException: An unexpected error occurred. — -> System.IndexOutOfRangeException: Index was outside the bounds of the array.

at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)

at Microsoft.Crm.Tools.ImportExportPublish.ThirdPartyComponentPropertiesProvider.GetProperties(String componentKey)

at Microsoft.Crm.Tools.ImportExportPublish.ExportHandler.GetFullUnmanagedPropertyMapForType(String itemType, Boolean managed, IComponentPropertiesProvider componentPropertiesProvider)

at Microsoft.Crm.Tools.ImportExportPublish.ExportHandler.AddXmlProperties(String itemType, XmlDocument importDocument, XmlNode parentNode, IGenericType container, Boolean managed, EntityMetadata entityMetadata, Version targetVersion, IComponentPropertiesProvider componentPropertiesProvider)

at Microsoft.Crm.Tools.Common.ImportExportPublish.EmailTemplateHandler.ExportItem(XmlDocument XDoc, XmlNode Node)

at Microsoft.Crm.Tools.ImportExportPublish.ExportHandler.Export(XmlDocument XDoc)

at Microsoft.Crm.Tools.ImportExportPublish.ExportHandler.Export(XmlDocument XDoc)

at Microsoft.Crm.Tools.ImportExportPublish.RootExportHandler.RunExport()

at Microsoft.Crm.WebServices.ExportXmlService.ExportSolutionWithTargetVersion(String solutionName, Boolean managed, String targetVersion, Boolean exportAutoNumberingSettings, Boolean exportCalendarSettings, Boolean exportCustomizationSettings, Boolean exportEmailTrackingSettings, Boolean exportGeneralSettings, Boolean exportMarketingSettings, Boolean exportOutlookSynchronizationSettings, Boolean exportRelationshipRoles, Boolean exportIsvConfig, Boolean exportSales, Boolean exportExternalApplications, ExecutionContext context)

Here are some facts

The last successful deployment 4 days ago

Did I get the latest from source control? Yes

Is this problem just happening to me? ask someone else to deploy

What was in the patch? one workflow

In the blog post — Dynamics 365 error messages point you in the right direction (yes, I read my blog posts) I got some pointers

Dynamics 365 errors point you in the right direction

To diagnose an error, look at the error with an empty mind #HoskCodeWisdom

Dynamics 365 error messages point you in the right direction but never give you the answer. developers focus on the problem, they forget to think what the error message is telling them.

Don’t assume

Don’t rush to solve a bug, before coming up with a solution, understand the problem, identify your assumptions and find out the facts. Debug the code, identify and path the code is taking and then work out what it’s supposed to do.

An example of bug investigation and how to approach fixing bugs

Sherlock Hosk and the case of the annoying bug

Approach bugs with an empty mind and don’t assume what is happening but prove what is happening.

For example –How to diagnose plugin errors in Dynamics 365 it has a good section on plugin error messages

follow these steps

  • Stop
  • Engage Brain\Think
  • What’s happening?
  • What should happen?
  • Make a list of the possible causes of the problem
  • Investigate your list

The Plan

Get someone else to deploy — they did and got the same error

Understand the key parts from the error message and see where it points us. The key parts are below.

Plugin/Microsoft.Crm.WebServices.ExportXmlService

Microsoft.Crm.CrmException: An unexpected error occurred. — -> System.IndexOutOfRangeException: Index was outside the bounds of the array

at Microsoft.Crm.Tools.Common.ImportExportPublish.EmailTemplateHandler.ExportItem(XmlDocument XDoc, XmlNode Node)

at Microsoft.Crm.Tools.ImportExportPublish.ExportHandler.Export(XmlDocument XDoc)

at Microsoft.Crm.Tools.ImportExportPublish.ExportHandler.Export(XmlDocument XDoc)

at Microsoft.Crm.Tools.ImportExportPublish.RootExportHandler.RunExport()

The errors above are saying it’s an IndexOutOfRangeException, in programming terms this means your counter has incremented (plus one) but there are no more records. e.g. you are trying to get row 10 in your index but there are only 9 rows.

The plugins are from Microsoft, note plugins the team created

EmailtemplateHander — it seems like the problem is occurring when we are exporting email templates.

I tried to export the solution from the environment and I got the same error, so the error was nothing to do with the ALM tool. It was a Microsoft problem.

I searched the internet for similar problems, but the problems were different and the solutions were different. The common theme was there was a defective customisation that needed to be removed or recreated, the solution would export after that.

My feeling at this point was somehow one of the email templates (we didn’t have any other templates) had got corrupt. There were 40 so there wasn’t any easy way to identify which one (or ones) was causing the problem. My plan was to take those email templates from another environment and import them over the top.

I tried this, and it worked, and I could go back on with my deployments. It seems easy now but at the time it didn’t make much sense.

  • Deploying a workflow shouldn’t break email templates
  • I have never seen an error when exporting a solution
  • The error message wasn’t clear and didn’t give much information on the problem.

It was a tricky problem; I wanted to share my experience, just in case you have a similar experience or are contronted with a problem you aren’t sure how to solve.