Peter Forsbom
Peter Forsbom

Reputation: 95

Updating entity in Dynamics 365 using CorrelationId context

I have set up a ServiceEndpoint in Dynamics 365 to send messages to Azure Service Bus. Whenever there is an update on an Account Entity a message is queued. I have a service that is listening on this queue. My service is updating a property on the Account Entity that was queued.

My code is not executed as a plugin and does not implement IPlugin. This means that I do not have access to the IPluginExecutionContext. To avoid an infinite loop I would like to use the CorrelationId as a context for my update calls.

Is this possible?

This is what happens:

  1. Soemone updates or creates an account in Dynamics 365 Online
  2. my service on my local server receives the message from Azure Service Bus
  3. I parse the incomming JSON
  4. Verify that the Depth is not larger than 1 to avoid infinite loop
  5. I retreive the Account Entity from Dynamics
  6. I make an update on a custom field
  7. I update the Account Entity - Here is where it gets tricky! My update request is generating a new message with a different CorrelationId. I would like the update request to use the CorrelationId of the incomming message so that I do not find my self in an infinite loop.

My calls are using OrganizationServiceProxy

Upvotes: 0

Views: 727

Answers (1)

AnkUser
AnkUser

Reputation: 5531

Why not use RemoteExecutionContext Class

Very quickly you can turn your Json(message) from Azure queue into Dynamics plugin context

 private static RemoteExecutionContext GetContext(string contextJson)
        {
            _log.Verbose($"Inside function {nameof(RemoteExecutionContext)}");
            RemoteExecutionContext rv = null;
            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(contextJson)))
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RemoteExecutionContext));
                rv = (RemoteExecutionContext)ser.ReadObject(ms);
            }
            _log.Verbose($"Exit function {nameof(RemoteExecutionContext)}");
            return rv;
        }

and you can call it something like below

 RemoteExecutionContext pluginContext = GetContext(receivedBody);

Article post for help

Your issue is not with RemoteExecutionContext rather it is with trigger of plugin on Update of Account for Azure service bus. You might know you can restrict update of Account to run only on fields you wish rather than all i.e you can get message in your ASB queue on update of certain fields of Account.

enter image description here

In addition if you want to handle with code, there is something called shared variables in Plugins. You can use them.

Upvotes: 1

Related Questions