Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#101 add createdon etc attributes to context entity so they are avail… #104

Merged
merged 4 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/XrmMockupShared/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ internal OrganizationResponse Execute(OrganizationRequest request, EntityReferen

if (eventOp.HasValue)
{
//copy the createon etc system attributes onto the target so they are available for postoperation processing
CopySystemAttributes(postImage, entityInfo.Item1 as Entity);

pluginManager.TriggerSystem(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this);
pluginManager.TriggerSync(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this);
pluginManager.StageAsync(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this);
Expand All @@ -604,6 +607,25 @@ internal OrganizationResponse Execute(OrganizationRequest request, EntityReferen
return response;
}

private void CopySystemAttributes(Entity postImage, Entity item1)
{
if (item1 == null)
{
return;
}

item1["createdon"] = postImage.GetAttributeValue<DateTime>("createdon");
Copy link
Member

@TomMalow TomMalow Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code handles these 4 attributes in the same way. Can we maybe generalize? Maybe move the attributes to a list that the code iterates over and adds them to the item1. This will also make it easier to extend with additional attributes. Both me and @magesoe has a suspicion that there are other system attributes we need to do this with as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the solution more generic, with a list of system attribute names which is iterated over.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could #92 be resolved via this as well ?

item1["modifiedon"] = postImage.GetAttributeValue<DateTime>("modifiedon");
if (postImage.Contains("createdby"))
{
item1["createdby"] = new EntityReference(postImage.GetAttributeValue<EntityReference>("createdby").LogicalName, postImage.GetAttributeValue<EntityReference>("createdby").Id);
}
if (postImage.Contains("modifiedby"))
{
item1["modifiedby"] = new EntityReference(postImage.GetAttributeValue<EntityReference>("modifiedby").LogicalName, postImage.GetAttributeValue<EntityReference>("modifiedby").Id);
}
}

internal void HandleInternalPreOperations(OrganizationRequest request, EntityReference userRef)
{
if (request.RequestName == "Create")
Expand Down
35 changes: 35 additions & 0 deletions tests/SharedPluginsAndCodeactivites/ContactPostPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace DG.Some.Namespace {
using System;
using Microsoft.Xrm.Sdk;
using DG.XrmFramework.BusinessDomain.ServiceContext;

public class ContactPostPlugin : Plugin
{
public ContactPostPlugin()
: base(typeof(ContactPostPlugin))
{
RegisterPluginStep<Contact>(
EventOperation.Create,
ExecutionStage.PostOperation,
Execute);
}

protected void Execute(LocalPluginContext localContext)
{
if (localContext == null) {
throw new ArgumentNullException("localContext");
}

var service = localContext.OrganizationService;

var con = localContext.PluginExecutionContext.InputParameters["Target"] as Contact;

if (con.FirstName == "CheckSystemAttributes")
{
con.LastName = con.CreatedOn.ToString();
con.FirstName = "updated";
service.Update(con);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)AccountCustomActivity.cs" />
<Compile Include="$(MSBuildThisFileDirectory)AccountBothImagePlugin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ContactPostPlugin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SyncAsyncTest\Test5\Sync2WithExecutionOrder - Copy.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SyncAsyncTest\Test5\AsyncWithExecutionOrder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SyncAsyncTest\Test5\Sync1WithExecutionOrder.cs" />
Expand Down
16 changes: 16 additions & 0 deletions tests/SharedTests/TestPlugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public void TestImages()
}
#endif

[TestMethod]
public void TestSystemAttributesAddedToTargetForPostOperationStepPlugins()
{
using (var context = new Xrm(orgAdminUIService))
{
var con = new Contact();
con.FirstName = "CheckSystemAttributes";

con.Id = orgAdminUIService.Create(con);

con = Contact.Retrieve(orgAdminService, con.Id, x => x.LastName,x=>x.CreatedOn);
Assert.IsTrue(!string.IsNullOrEmpty(con.LastName));
Assert.AreEqual(con.CreatedOn.ToString(), con.LastName);
}
}

[TestMethod]
public void TestPluginTrigger()
{
Expand Down