DEV Community

Prakash Kalimuthu
Prakash Kalimuthu

Posted on Edited on

Dissecting Dataverse Plug-ins, Part 2: ParentContext Is Not a "Triggered by Another Plug-in" Flag

Spent an afternoon debugging a Dataverse plug-in that looked like someone else was triggering it.

A PreOperation step on Create reported context.ParentContext != null. Easy conclusion: another plug-in must have kicked off this pipeline.

Wrong. And the deeper I went, the more the obvious check fell apart.

I checked three paths in the trace logs on a current 9.2 org:

  • a plain Create from the Web API,
  • a model-driven app save,
  • and a Create triggered by another plug-in through the org service.

All three had a non-null ParentContext at stage 20. The plugin-triggered one also had Depth=2.

The cleanest case was a table with zero stage-10 steps and a single stage-20 step. A direct Web API contact create still came back with a parent context. It held only platform keys: IsAutoTransact, AcceptLang, DefaultsAddedFlag. No PreValidation step ran, and no other plug-in was in the pipeline.

So ParentContext is not a "was this triggered by another plug-in" flag. The platform supplies one for Create, Update and Delete at stage 20 whether or not another plug-in is involved.

There is a second trap. In the nested test, the immediate ParentContext did not point at the plug-in that called Create. It pointed at the nested operation's own MainOperation context: same message, same entity, stage 30. The triggering operation was one level higher, as the grandparent.

If you need "another plug-in started this", use Depth > 1. If you need the origin, walk up the parent chain and inspect MessageName / PrimaryEntityName there. The immediate parent can look identical to the current operation.

Where ParentContext legitimately matters is shared variables. If a PreValidation step writes a value on Create, Update or Delete, PreOperation and PostOperation read it from ParentContext.SharedVariables, not from context.SharedVariables.

And check the key, not just the context:

if (context.ParentContext != null &&
    context.ParentContext.SharedVariables.TryGetValue("my.key", out var value))
{
    // only true when stage 10 actually wrote it
}
Enter fullscreen mode Exit fullscreen mode

Two different questions. Two different checks. The parent context is not a trigger log, and a trigger log is not a shared-variable bag.

Top comments (0)