Query Builder Error when exporting solution in Dynamics 365

Solutions in Dynamics 365 are very useful and there is no doubt about that. One of the latest features is possibility to add single fields, views or forms to the solution, not the whole entity with all its dependencies. That allows us to better control what is transferred and deployed on out staging and production environments. Few times I came across the problem, which is caused clearly by this granularity of solutions:

error

Query Builder Error

The specified field does not exist in Microsoft Dynamics 365

Fortunately this error was in on-premise environment, so I was able to check the tracing logs and the problem has become clear:

“attribute with id=’86e616c0-64fc-413c-9957-470272c3f198′ does not exist”

So, apparently, someone has removed some attribute from the system, but somehow it was orphaned in our solution and is causing the export to fail (no doubt – we are trying to export the solution, so system is getting all the metadata, for all the Solution Components and cannot find one of the component). This is very unfortunate, because we cannot see this missing object in the solution view, it’s only in database in SolutionComponentBase table.  In on-premise environment, it’s not a problem – we simply check the id in traces and then go to our database and do:

SELECT FROM SolutionComponentBase  where ObjectId = ‘guid of missing component’

This will get us the failing components from all our solutions. We can remove them on DB level (sic!) but if you are afraid that this is not supported (which it is, but really cannot break anything) you can do it using SDK.

But what if we have online instance without access to the database? We can request Microsoft Support for the traces and then we will know which attribute is failing. But if we are in a hurry, we would have to loop through all Solution Components is our solution, check if it exists in the system and remove it from the solution. I believe that in 99% of cases, the missing component would be an attribute, so here is the code that would help you to get rid of this error:


static void Main(string[] args)
{
var solutionUniqueName = "SOLUTIONNAME";
var orgService = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);
var solutionComponentQuery = new QueryExpression("solutioncomponent");
solutionComponentQuery.Distinct = true;
solutionComponentQuery.ColumnSet.AddColumns("objectid", "componenttype", "solutioncomponentid");
var solutioncomponentSolutionLink = solutionComponentQuery.AddLink("solution", "solutionid", "solutionid");
solutioncomponentSolutionLink.EntityAlias = "aa";
solutioncomponentSolutionLink.LinkCriteria.AddCondition("uniquename", ConditionOperator.Equal, solutionUniqueName);
var results = orgService.RetrieveMultiple(solutionComponentQuery);
foreach (var item in results.Entities)
{
if(item.GetAttributeValue<OptionSetValue>("componenttype").Value == 2)
{
var attributeRequest = new RetrieveAttributeRequest
{
MetadataId = item.GetAttributeValue<Guid>("objectid")
};
try
{
var result = (RetrieveAttributeResponse)orgService.Execute(attributeRequest);
Console.WriteLine(result.AttributeMetadata.LogicalName);
}
catch
{
//attribute is missing, remove it from solution
Console.WriteLine($"Attribute {attributeRequest.MetadataId} is missing. Removing from the solution.");
var removeSolutionComponent = new RemoveSolutionComponentRequest()
{
ComponentId = item.GetAttributeValue<Guid>("objectid"),
ComponentType = item.GetAttributeValue<OptionSetValue>("componenttype").Value,
SolutionUniqueName = solutionUniqueName
};
orgService.Execute(removeSolutionComponent);
}
}
}
}

It simply iterates through all attributes in the solution and tries to retrieve metadata for each one of them. If it fails, it simply removes this component from the solution. That should be enough for most of the cases, but if there will be any other component types missing, you will have to modify the code to check for those missing components (which I described in one of my previous posts). I have prepared a very simple program, that iterates through all of the solution components and tries to retrieve every single type – and if it fails it just removes it from the solution. Feel free to use it for any means necessary:

https://github.com/pawelgradecki/SolutionExportFixer

7 thoughts on “Query Builder Error when exporting solution in Dynamics 365

  1. Thanks, This seems to be the solution to my issue. However when I run the code, it all works up to the point of “OrgService.Delete”. I then get an error “The ‘Delete’ method does not support entities of type ‘solutioncomponent'”. Any ideas? Has the latest Cloud CRM stopped allowing this delete action?

    Like

    • Yes, it looks like this message is no longer supported for solutioncomponents. I believe that using RemoveSolutionComponentRequest should do the trick. I updated the code on github, give it a try. Basically instead of calling Delete you should execute RemoveSolutionComponentRequest with proper parameters

      Like

  2. I’m not sure why but this didn’t work for me. In case anyone else comes here and it doesn’t work, I cloned a new solution from my solution that wouldn’t export and that seemed to work.

    Like

Leave a comment