Skip to content

Commit

Permalink
1.2.54.1:
Browse files Browse the repository at this point in the history
MapMultiSelectOptionSet:
 - Added "Keep Existing Values" parameter
SetMultiSelectOptionSet:
 - Added "Keep Existing Values" parameter
  • Loading branch information
Demian Adolfo Raschkovan committed Jan 13, 2019
1 parent cbd2656 commit 2d2c4c1
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 28 deletions.
Binary file modified .vs/Dynamics-365-Workflow-Tools/v15/.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"\\msdyncrmWorkflowTools\\msdyncrmWorkflowTools_Class",
"\\msdyncrmWorkflowTools\\msdyncrmWorkflowTools_ConsoleTest"
],
"SelectedNode": "\\msdyncrmWorkflowTools\\msdyncrmWorkflowTools_Class\\msdyncrmWorkflowTools_Class.cs",
"SelectedNode": "\\msdyncrmWorkflowTools\\msdyncrmWorkflowTools.sln",
"PreviewInSolutionExplorer": false
}
Binary file modified .vs/slnx.sqlite
Binary file not shown.
Binary file modified msdyncrmWorkflowTools/.vs/msdyncrmWorkflowTools/v15/.suo
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public class MapMultiSelectOptionSet : CodeActivity
[Input("Target Attributes")]
public InArgument<string> TargetAttributes { get; set; }

/// <summary>
/// Indicate if the existing selected values in the target multi-select optionset attributes will be maintained.
/// By default, it will remove the existing values and assign the new ones given by the argument "AttributesValues"
/// </summary>
[Input("Keep Existing Values")]
[Default("false")]
public InArgument<Boolean> KeepExistingValues { get; set; }

protected override void Execute(CodeActivityContext executionContext)
{
Common objCommon = new Common(executionContext);
Expand All @@ -35,7 +43,7 @@ protected override void Execute(CodeActivityContext executionContext)
string[] targetAttributes = GetTargetAttributes(executionContext, objCommon.tracingService);
EntityReference sourceEntityReference = GetSourceEntityReference(executionContext, objCommon.service);
EntityReference targetEntityReference = GetTargetEntityReference(executionContext, objCommon.service);
Entity targetEntity = BuildTargetEntity(sourceEntityReference, targetEntityReference, sourceAttributes, targetAttributes,objCommon.tracingService,objCommon.service);
Entity targetEntity = BuildTargetEntity(sourceEntityReference, targetEntityReference, sourceAttributes, targetAttributes,objCommon.tracingService,objCommon.service, executionContext);

if (targetEntity != null)
{
Expand Down Expand Up @@ -88,7 +96,7 @@ private string[] GetTargetAttributes(CodeActivityContext executionContext, ITrac
return targetAttributesArray;
}

private Entity BuildTargetEntity(EntityReference sourceEntityReference, EntityReference targetEntityReference, string[] sourceAttributes, string[] targetAttributes, ITracingService tracingService, IOrganizationService organizationService)
private Entity BuildTargetEntity(EntityReference sourceEntityReference, EntityReference targetEntityReference, string[] sourceAttributes, string[] targetAttributes, ITracingService tracingService, IOrganizationService organizationService, CodeActivityContext executionContext)
{
if (sourceEntityReference == null || targetEntityReference == null || sourceAttributes == null || targetAttributes == null)
return null;
Expand All @@ -107,17 +115,22 @@ private Entity BuildTargetEntity(EntityReference sourceEntityReference, EntityRe
Entity targetEntity = new Entity(targetEntityReference.LogicalName, targetEntityReference.Id);
string targetAttribute = null;
int attributeMappedCounter = 0;

OptionSetValueCollection sourceNewValues = null;
OptionSetValueCollection targetExistingValues = null;


for (int i = 0; i < numberSourceAttribute; i++)
{
string sourceAttribute = sourceAttributes[i];
if (sourceEntity.Contains(sourceAttribute))
{
var optionSetValueCollection = sourceEntity[sourceAttribute] as OptionSetValueCollection;
if (typeof(OptionSetValueCollection).Equals(optionSetValueCollection.GetType()))
sourceNewValues = sourceEntity[sourceAttribute] as OptionSetValueCollection;
if (typeof(OptionSetValueCollection).Equals(sourceNewValues.GetType()))
{
targetAttribute = targetAttributes[i];
targetEntity.Attributes.Add(targetAttribute, optionSetValueCollection);
tracingService.Trace("Source attribute '{0}' has been mapped to target attribute {1}. ", sourceAttribute, targetAttribute);
targetExistingValues = GetExistingAttributeValues(targetEntity.ToEntityReference(), targetAttribute, tracingService, organizationService, executionContext);
targetEntity.Attributes.Add(targetAttribute, MergeOptionSetCollections(sourceNewValues, targetExistingValues,tracingService));
attributeMappedCounter++;
}
else
Expand All @@ -132,5 +145,47 @@ private Entity BuildTargetEntity(EntityReference sourceEntityReference, EntityRe

return targetEntity;
}
private OptionSetValueCollection GetExistingAttributeValues(EntityReference targetEntityReference, string attributeName, ITracingService tracingService, IOrganizationService organizationService, CodeActivityContext executionContext)
{
tracingService.Trace("Retrieving existing values");

Boolean attributeValues = KeepExistingValues.Get<Boolean>(executionContext);

if (attributeValues == false)
return null;

Entity record = organizationService.Retrieve(targetEntityReference.LogicalName, targetEntityReference.Id, new ColumnSet(new string[] { attributeName }));

tracingService.Trace("Existing values have been retrieved correctly");

if (record.Contains(attributeName))
return record[attributeName] as OptionSetValueCollection;
else
return null;
}

private OptionSetValueCollection MergeOptionSetCollections(OptionSetValueCollection newValues, OptionSetValueCollection existingValues, ITracingService tracingService)
{
tracingService.Trace("Merging new and exiting multi-select optionset values");

if (existingValues == null && newValues == null)
return new OptionSetValueCollection();

if (existingValues == null)
return newValues;

if (newValues == null)
return existingValues;

foreach (OptionSetValue newValue in newValues)
{
if (!existingValues.Contains(newValue))
existingValues.Add(newValue);
}

tracingService.Trace("New and exiting multi-select optionset values have been merged correctly. Total options: {0} ", existingValues.Count);
return existingValues;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class SetMultiSelectOptionSet : CodeActivity
[RequiredArgument]
[Input("Attribute Values")]
public InArgument<string> AttributeValues { get; set; }
/// <summary>
/// Indicate if the existing selected values in the target multi-select optionset attribute will be maintained.
/// By default, it will remove the existing values and assign the new ones given by the argument "AttributesValues"
/// </summary>
[Input("Keep Existing Values")]
[Default("false")]
public InArgument<Boolean> KeepExistingValues { get; set; }

protected override void Execute(CodeActivityContext executionContext)
{
Expand All @@ -33,9 +40,13 @@ protected override void Execute(CodeActivityContext executionContext)

EntityReference sourceEntityReference = GetTargeteEntityReference(executionContext,objCommon.tracingService, objCommon.service);
string attributeName = GetAttributeName(executionContext,objCommon.tracingService);
OptionSetValueCollection values = GetAttributeValues(executionContext,objCommon.tracingService);
OptionSetValueCollection newValues = GetNewAttributeValues(executionContext, objCommon.tracingService);
OptionSetValueCollection existingValues = GetExistingAttributeValues(sourceEntityReference, attributeName,executionContext, objCommon.tracingService, objCommon.service);


//UpdateRecord(sourceEntityReference, attributeName, values,objCommon.service,objCommon.tracingService);
UpdateRecord(sourceEntityReference, attributeName, newValues, existingValues, objCommon.service, objCommon.tracingService);

UpdateRecord(sourceEntityReference, attributeName, values,objCommon.service,objCommon.tracingService);
}

private EntityReference GetTargeteEntityReference(CodeActivityContext executionContext, ITracingService tracingService, IOrganizationService organizationService)
Expand All @@ -53,7 +64,9 @@ private string GetAttributeName(CodeActivityContext executionContext, ITracingSe
return attributeName;
}

private OptionSetValueCollection GetAttributeValues(CodeActivityContext executionContext, ITracingService tracingService)
//private OptionSetValueCollection GetAttributeValues(CodeActivityContext executionContext, ITracingService tracingService)
private OptionSetValueCollection GetNewAttributeValues(CodeActivityContext executionContext, ITracingService tracingService)

{
string attributeValues = AttributeValues.Get<string>(executionContext) ?? throw new ArgumentNullException("Attribute Values is empty");
tracingService.Trace("Attribute Values:'{0}'", attributeValues);
Expand Down Expand Up @@ -90,18 +103,63 @@ private OptionSetValueCollection GetAttributeValues(CodeActivityContext executio

return optionSetValueCollection;
}
private OptionSetValueCollection GetExistingAttributeValues(EntityReference targetEntityReference, string attributeName, CodeActivityContext executionContext, ITracingService tracingService, IOrganizationService organizationService)
{
tracingService.Trace("Retrieving existing values");

Boolean attributeValues = KeepExistingValues.Get<Boolean>(executionContext);

if (attributeValues == false)
return null;

Entity record = organizationService.Retrieve(targetEntityReference.LogicalName, targetEntityReference.Id, new ColumnSet(new string[] { attributeName }));

tracingService.Trace("Existing values have been retrieved correctly");

if (record.Contains(attributeName))
return record[attributeName] as OptionSetValueCollection;
else
return null;
}


//private void UpdateRecord(EntityReference targetEntityReference, string attributeName, OptionSetValueCollection values, IOrganizationService organizationService, ITracingService tracingService)
private void UpdateRecord(EntityReference targetEntityReference, string attributeName, OptionSetValueCollection newValues, OptionSetValueCollection existingValues, IOrganizationService organizationService, ITracingService tracingService)

private void UpdateRecord(EntityReference targetEntityReference, string attributeName, OptionSetValueCollection values, IOrganizationService organizationService, ITracingService tracingService)
{
if (targetEntityReference == null || attributeName == null || values == null)
throw new ArgumentNullException(string.Format("Unexpected null parameters when trying to update record. Record reference '{0}' - attibute name '{1}' - values '{2}'", targetEntityReference, attributeName, values));
if (targetEntityReference == null || attributeName == null || newValues == null)
throw new ArgumentNullException(string.Format("Unexpected null parameters when trying to update record. Record reference '{0}' - attibute name '{1}' - values '{2}'", targetEntityReference, attributeName, newValues));


Entity targetEntity = new Entity(targetEntityReference.LogicalName, targetEntityReference.Id);
targetEntity[attributeName] = values;
targetEntity[attributeName] = MergeOptionSetCollections(newValues, existingValues, tracingService);

organizationService.Update(targetEntity);

tracingService.Trace("Multi-select option set attribute '{0}' has been updated correctly for the record type '{1}' with id '{2}'", attributeName, targetEntityReference.LogicalName, targetEntityReference.Id);
}
private OptionSetValueCollection MergeOptionSetCollections(OptionSetValueCollection newValues, OptionSetValueCollection existingValues, ITracingService tracingService)
{
tracingService.Trace("Merging new and exiting multi-select optionset values");

if (existingValues == null && newValues == null)
return new OptionSetValueCollection();

if (existingValues == null)
return newValues;

if (newValues == null)
return existingValues;

foreach (OptionSetValue newValue in newValues)
{
if (!existingValues.Contains(newValue))
existingValues.Add(newValue);
}

tracingService.Trace("New and exiting multi-select optionset values have been merged correctly. Total options: {0} ", existingValues.Count);
return existingValues;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// Puede especificar todos los valores o usar los valores predeterminados (número de compilación y de revisión)
// usando el símbolo '*' como se muestra a continuación:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.54.0")]
[assembly: AssemblyFileVersion("1.0.54.0")]
[assembly: AssemblyVersion("1.0.54.1")]
[assembly: AssemblyFileVersion("1.0.54.1")]
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<ItemGroup>
<Reference Include="Microsoft.Crm.Sdk.Proxy, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\Tools\PluginRegistration\Microsoft.Crm.Sdk.Proxy.dll</HintPath>
<HintPath>..\..\..\..\..\..\..\Devtools\Tools\PluginRegistration\Microsoft.Crm.Sdk.Proxy.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
<ItemGroup>
<Reference Include="Microsoft.Crm.Sdk.Proxy, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\Tools\PluginRegistration\Microsoft.Crm.Sdk.Proxy.dll</HintPath>
<HintPath>..\..\..\..\..\..\..\Devtools\Tools\PluginRegistration\Microsoft.Crm.Sdk.Proxy.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.6.1.7600.16394\lib\net35\Microsoft.IdentityModel.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.2.5\lib\net452\Microsoft.Xrm.Sdk.dll</HintPath>
<HintPath>..\..\..\..\..\..\..\Devtools\Tools\PluginRegistration\Microsoft.Xrm.Sdk.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xrm.Sdk.Workflow, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.Workflow.9.0.2.5\lib\net452\Microsoft.Xrm.Sdk.Workflow.dll</HintPath>
<HintPath>..\..\..\..\..\..\..\Devtools\Tools\PluginRegistration\Microsoft.Xrm.Sdk.Workflow.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<assemblyIdentity name="Microsoft.Xrm.Sdk.Deployment" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Rest.ClientRuntime" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Loading

0 comments on commit 2d2c4c1

Please sign in to comment.