Skip to content

Commit 876bf82

Browse files
authored
Merge pull request #324 from delegateas/fix/303-delete-target-in-post-operation-plugin
Fix error when deleting target record in post-operation plugin
2 parents dffd39c + d75ad33 commit 876bf82

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/XrmMockup365/Core.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,13 +975,13 @@ internal OrganizationResponse Execute(OrganizationRequest request, EntityReferen
975975
var entityLogicalName = ((Entity)request.Parameters["Target"]).LogicalName;
976976
var reference = primaryRef ?? new EntityReference(entityLogicalName, createResponse.id);
977977

978-
var createdEntity = GetDbRow(reference).ToEntity();
978+
var createdEntity = TryRetrieve(reference);
979979
TriggerExtension(
980980
new MockupService(this, userRef.Id, pluginContext), request,
981981
createdEntity, null, userRef);
982982
break;
983983
case "Update":
984-
var updatedEntity = GetDbRow(primaryRef).ToEntity();
984+
var updatedEntity = TryRetrieve(primaryRef);
985985
TriggerExtension(
986986
new MockupService(this, userRef.Id, pluginContext), request,
987987
updatedEntity, preImage, userRef);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
namespace DG.Some.Namespace {
3+
using System;
4+
using DG.XrmFramework.BusinessDomain.ServiceContext;
5+
using TestPluginAssembly365.Plugins.SyncAsyncTest;
6+
using XrmPluginCore;
7+
using XrmPluginCore.Enums;
8+
9+
/// <summary>
10+
/// Test plugin that deletes the target account in a post-Update operation.
11+
/// Used to verify that XrmMockup handles record deletion inside a post-operation plugin gracefully.
12+
/// </summary>
13+
public class AccountDeleteInPostPlugin : TestPlugin {
14+
public AccountDeleteInPostPlugin() {
15+
#pragma warning disable CS0618 // Type or member is obsolete - disabled for testing purposes
16+
RegisterPluginStep<Account>(
17+
EventOperation.Update,
18+
ExecutionStage.PostOperation,
19+
Execute);
20+
#pragma warning restore CS0618 // Type or member is obsolete
21+
}
22+
23+
protected void Execute(LocalPluginContext localContext) {
24+
if (localContext == null) {
25+
throw new ArgumentNullException("localContext");
26+
}
27+
28+
var service = localContext.OrganizationService;
29+
service.Delete(Account.EntityLogicalName, localContext.PluginExecutionContext.PrimaryEntityId);
30+
}
31+
}
32+
}

tests/XrmMockup365Test/TestPlugins.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,33 @@ public void TestExecutesLegacyDaxifPlugins_Update()
220220
);
221221
}
222222
}
223+
224+
[Fact]
225+
public void TestDeleteTargetInPostUpdatePlugin()
226+
{
227+
crm.DisableRegisteredPlugins(true);
228+
crm.RegisterAdditionalPlugins(DG.Tools.XrmMockup.PluginRegistrationScope.Temporary, typeof(DG.Some.Namespace.AccountDeleteInPostPlugin));
229+
230+
var account = new Account { Name = "DeleteMe" };
231+
account.Id = orgAdminService.Create(account);
232+
233+
// Update triggers AccountDeleteInPostPlugin which deletes the account — should not throw
234+
var update = new Account { Id = account.Id, Name = "Updated" };
235+
orgAdminService.Update(update);
236+
237+
// Verify the account was actually deleted by the plugin
238+
var retrieved = orgAdminService.RetrieveMultiple(
239+
new QueryExpression(Account.EntityLogicalName)
240+
{
241+
Criteria = new FilterExpression
242+
{
243+
Conditions =
244+
{
245+
new ConditionExpression("accountid", ConditionOperator.Equal, account.Id)
246+
}
247+
}
248+
});
249+
Assert.Empty(retrieved.Entities);
250+
}
223251
}
224252
}

0 commit comments

Comments
 (0)