Skip to content

Commit

Permalink
#28: Bug fixed in handling intersect entity fetch xml
Browse files Browse the repository at this point in the history
Removed appending of placeholders in fetchxml
  • Loading branch information
imranakram committed Mar 7, 2021
1 parent 9dba170 commit 7d038b6
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions shared/Innofactor.Crm.Shuffle/ShuffleDataExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private EntityCollection ExportDataBlock(IExecutionContainer container, ShuffleB
foreach (var attribute in attributes.Attribute)
{
var attr = attribute.Name;
container.Log("Adding column: {0}", attr);
container.Log($"Adding column: {attr}");
lAttributes.Add(attr.Replace("*", "%"));
if (attr.Contains("*"))
{
Expand Down Expand Up @@ -317,7 +317,7 @@ private EntityCollection ExportDataBlock(IExecutionContainer container, ShuffleB
{
#region QueryExpression Entity

container.StartSection("Export entity " + block.Entity);
container.StartSection($"Export entity {block.Entity}");
var qExport = new QueryExpression(block.Entity);
if (block.Export.ActiveOnly)
{
Expand Down Expand Up @@ -356,7 +356,7 @@ private EntityCollection ExportDataBlock(IExecutionContainer container, ShuffleB
try
{
var fetch = container.ConvertToFetchXml(qExport);
container.Log("Exporting {0}:\n{1}", block.Entity, fetch);
container.Log($"Exporting {block.Entity}:\n{fetch}");
}
catch (Exception ex)
{
Expand All @@ -369,7 +369,7 @@ private EntityCollection ExportDataBlock(IExecutionContainer container, ShuffleB
{
SelectAttributes(container, cExportEntities, lAttributes, lNullAttributes);
}
SendLine(container, "Block {0} - {1} records", block.Name, cExportEntities.Count());
SendLine(container, $"Block {block.Name} - {cExportEntities.Count()} records");
container.EndSection();

#endregion QueryExpression Entity
Expand All @@ -392,9 +392,10 @@ private EntityCollection ExportDataBlock(IExecutionContainer container, ShuffleB
}

var fetch = xDoc.OuterXml;
fetch = fetch.Replace("<fetch ", "<fetch {0} {1} "); // Detta för att se till att CrmServiceProxy.RetrieveMultiple kan hantera paging
//Imran: Removed because this is causing invalid Xml errors. Could not see the point of having these placeholders.
//fetch = fetch.Replace("<fetch ", "<fetch {0} {1} "); // Detta för att se till att CrmServiceProxy.RetrieveMultiple kan hantera paging
#if DEBUG
container.Log("Exporting intersect entity {0}\n{1}", block.Entity, fetch);
container.Log($"Exporting intersect entity {block.Entity}\n{fetch}");
#endif
var qExport = new FetchExpression(fetch);
cExportEntities = container.RetrieveMultiple(qExport);
Expand All @@ -403,24 +404,43 @@ private EntityCollection ExportDataBlock(IExecutionContainer container, ShuffleB
var newattributes = new List<KeyValuePair<string, object>>();
foreach (var attr in entity.Attributes)
{
if (attr.Value is Guid)
if (attr.Value is Guid guid)
{
var attrname = attr.Key;
var relatedentity = attrname.Substring(0, attrname.Length - (attrname.EndsWith("idone") || attrname.EndsWith("idtwo") ? 5 : 2));
newattributes.Add(new KeyValuePair<string, object>(attrname, new EntityReference(relatedentity, (Guid)attr.Value)));
if (!newattributes.Contains(new KeyValuePair<string, object>(attrname, new EntityReference(relatedentity, guid))))
{
container.Log($"Adding Attribute {attrname} - Related entity {relatedentity}");
newattributes.Add(new KeyValuePair<string, object>(attrname, new EntityReference(relatedentity, guid)));
#if DEBUG
container.Log($"{attrname} added");
#endif

}
else
{
#if DEBUG
container.Log($"{attrname} already exists.");
#endif

}
}
}
foreach (var newattr in newattributes)
{
if (!newattr.Key.Equals(container.Entity(entity.LogicalName).PrimaryIdAttribute))

#if DEBUG
container.Log($"Entity {entity.LogicalName} contains attribute {newattr.Key}: {entity.Attributes.Contains(newattr.Key)}");
#endif
if(!entity.Attributes.Contains(newattr.Key))
{
entity.Attributes.Add(newattr.Key, newattr.Value);
}
}
}
container.EndSection();

#endregion FetchXML Intersect
#endregion FetchXML Intersect
}

container.Log($"Returning {cExportEntities.Count()} records");
Expand Down Expand Up @@ -466,6 +486,6 @@ private EntityCollection ExportDataBlock(IExecutionContainer container, ShuffleB
return type;
}

#endregion Private Methods
#endregion Private Methods
}
}

0 comments on commit 7d038b6

Please sign in to comment.