Trigger a Journey when a record is updated using Plugin C# Code

Create a Trigger and Journey

In your marketing automation platform, navigate to the section where you can create triggers and journeys. This process may vary depending on the platform you're using.

Create a new trigger that defines the conditions under which a contact should enter the journey

Create a journey that outlines the steps you want contacts to go through once they enter the journey. This could include sending emails, updating account records, or triggering other actions.

Trigger the Journey with Plugin Code

Depending on the marketing automation platform you're using, you may have access to APIs or SDKs that allow you to trigger journeys programmatically.

Use the appropriate API or SDK to trigger the journey when certain conditions are met in your application. This could be in response to user actions, system events, or any other triggers relevant to your use case.

Typically, you would need to authenticate with the marketing automation platform's API, specify the journey to trigger, and provide any additional data required for personalization or tracking purposes.

Step 1. Create a trigger when an account is created or updated Name column

Create a trigger when an account is created or updated Name column

Step 2. Create a Journey

Create a Journey

Create a Journey

Step 3. Use the trigger which we have created and Save it

Use the trigger

Step 4. Publish the Journey

Publish the Journey

Step 5. Journey will trigger when an account is updated with Name column

Journey will trigger when an account is updated with Name column

  1. Create a Plugin
    • Create a C# plugin using the appropriate framework for your CRM system (e.g., Dynamics 365).
    • Register the plugin to execute on the update message of the entity you're monitoring.
  2. Retrieve Updated Record Information
    • In the plugin code, retrieve the updated record's information, such as the entity type and the updated fields.
  3. Connect to Marketing Automation Platform API
    • Use the appropriate API or SDK provided by your marketing automation platform to trigger the journey.
    • You may need to authenticate with the API using credentials or tokens provided by your marketing automation platform.
  4. Trigger the Journey
    • Once connected to the marketing automation platform's API, trigger the journey by sending the necessary data.
    • The data you send may include the contact's information or any other relevant details required by the journey.
  5. Handle Errors and Logging
    • Implement error handling in your plugin code to handle any exceptions that may occur during the process.

Step 6. Below is code for trigger a journey using plugin C# code

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xrm.Sdk;
namespace Plugins
    public class ManageAccount : IPlugin
        public void Execute(IServiceProvider serviceProvider)
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(null);
            Entity entity = (Entity)context.InputParameters["Target"];
            if (entity.LogicalName != "account")
            if (context.MessageName == "Create" || context.MessageName == "Update")
                if (account.Attributes.Contains("name") && account.GetAttributeValue<string>("name") != !string.IsNullOrEmpty)
                    EntityReference primaryContact = Record.Attributes.Contains("apy_primarycontact") ? Record.GetAttributeValue<EntityReference>("apy_primarycontact") : account.GetAttributeValue<EntityReference>("apy_primarycontact");
                    if (primaryContact == null)
                    var contact = new Entity("contact", primaryContact.Id);
                    Trigger trigger = new Trigger(Service);
                    trigger.ExecuteContactExitTrigger(contact, Trigger.TriggerNames.Trigger_ExitOgnomyConfirmation);
                    DateTime currentDateTime = DateTime.Now;
                    string journeyName = new apyKeyValuePair(Trigger.TriggerNames.Trigger_ExitOgnomyConfirmation.ToString(), Service).Value;
                    OrganizationRequest customTrigger = new OrganizationRequest() { RequestName = journeyName };
                    customTrigger.Parameters.Add("msdynmkt_bindingid", contact.Id.ToString());
                    customTrigger.Parameters.Add("msdynmkt_signaluserauthid", contact.Id.ToString());
                    customTrigger.Parameters.Add("msdynmkt_signalingestiontimestamp", currentDateTime);
                    customTrigger.Parameters.Add("msdynmkt_signaltimestamp", currentDateTime);
                    customTrigger.Parameters.Add("msdynmkt_profileid", contact.Id.ToString());

Unit Test code for Journey

public void UpdateContact_HasJourneyInstance_ExitContactFromContatJourney()
    Guid paitentAppoinmentId = Guid.NewGuid();
    bool didPublish = false;
    Entity contact = new Entity("contact", Guid.NewGuid());
    contact.Attributes.Add("donotbulkemail", false);
    contact.Attributes.Add("statecode", new OptionSetValue(0));
    contact.Attributes.Add("apy_parentaccount", account.ToEntityReference());
    contact.Attributes.Add("apy_websiteid", "websiteid_value");
    Entity target = new Entity("account", Guid.NewGuid());
    target.Attributes.Add("name", "allam");
    target.Attributes.Add("apy_primarycontact", contact.ToEntityReference());
    Entity journey = new Entity("msdynmkt_journey", Guid.NewGuid());
    var keyValueJourneyAppointmentUpdateConfirmation = new Entity("apy_apykeyvaluepair", Guid.NewGuid());
    keyValueJourneyAppointmentUpdateConfirmation.Attributes.Add("apy_name", "Journey_Name");
    keyValueJourneyAppointmentUpdateConfirmation.Attributes.Add("apy_value", "c2e6eapy-6432-3d46-bef1-974cb2614cb6");
    Guid journeyInstanceId = Guid.NewGuid();
    Entity journeyInstance = new Entity("msdynmkt_journeyinstance", journeyInstanceId);
    journeyInstance.Attributes.Add("msdynmkt_journeyinstanceid", journeyInstanceId);
    journeyInstance.Attributes.Add("msdynmkt_journeydefinitionid", new Guid(keyValueJourneyAppointmentUpdateConfirmation.GetAttributeValue<string>("apy_value")));
    journeyInstance.Attributes.Add("msdynmkt_targetid", contact.Id);
    journeyInstance.Attributes.Add("msdynmkt_targetentity", contact.LogicalName);
    journeyInstance.Attributes.Add("msdynmkt_journeyinstancestate", new OptionSetValue(534121000));
    List<Entity> data = new List<Entity>
         contact, keyValueJourneyAppointmentUpdateConfirmation,
         journeyInstance, journey
    XrmFakedContext context = new XrmFakedContext();
    XrmFakedPluginExecutionContext ctx = context.GetDefaultPluginContext();
    ctx.MessageName = "Update";
    ctx.InputParameters["Target"] = target;
    OrganizationResponse OrganizationRequestMock(OrganizationRequest req)
        didPublish = true;
        Assert.AreEqual(contact.Id.ToString(), req.Parameters["msdynmkt_bindingid"]);
        Assert.AreEqual(contact.Id.ToString(), req.Parameters["msdynmkt_signaluserauthid"]);
        Assert.AreEqual(DateTime.Now.ToShortDateString(), ((System.DateTime)req.Parameters["msdynmkt_signalingestiontimestamp"]).Date.ToShortDateString());
        Assert.AreEqual(DateTime.Now.ToShortDateString(), ((System.DateTime)req.Parameters["msdynmkt_signaltimestamp"]).Date.ToShortDateString());
        Assert.AreEqual(contact.Id.ToString(), req.Parameters["msdynmkt_profileid"]);
        return new OrganizationResponse { ResponseName = "Success" };
    Assert.IsTrue(didPublish, "Publish bool was false");
    Entity accountEnt = context.CreateQuery("account").Where(x => x.Id == contact.Id).First();
    Assert.AreEqual(accountEnt.GetAttributeValue<string>("name"), allam);

Similar Articles