Route non-case records using a plug-in

You can trigger routing for non-case records such as email messages programmatically, using the IPlugin interface.

You can use the following sample code in your Console App (.NET framework) of Visual Studio. The code checks for the following two conditions and if they're met, it triggers the msdyn_ApplyRoutingRuleEntityRecord action.

  • whether the Web service message is to create a record
  • whether the record is an email message.
public class SamplePlugin : IPlugin 
{ 
    public void Execute(IServiceProvider serviceProvider) 
    { 
          // Obtain the tracing service 
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 
        // Obtain the execution context from the service provider 
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 
            // Check if create message 
            if (context.MessageName.ToLower().Equals("create")) 
            { 
                // The InputParameters collection contains all the data passed in the message request 
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 
                { 
                    // Obtain the target entity from the input parameters 
                    Entity entity = (Entity)context.InputParameters["Target"]; 
                    // Target is an email 
                    if (entity.LogicalName.ToLower().Equals("email")) 
                    { 
                        try 
                        {  
                            // Obtain the organization service reference that you'll need for web service calls 
                            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
                            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 
                            // Execute msdyn_ApplyRoutingRuleEntityRecord request 
                            OrganizationRequest request = new OrganizationRequest("msdyn_ApplyRoutingRuleEntityRecord"); 
                            request["Target"] = new EntityReference("email", entity.Id); 
                            service.Execute(request); 
                        } 
                        catch (Exception ex) 
                        { 
                            tracingService.Trace("SamplePlugin: {0}", ex.ToString()); 
                            throw; 
                        } 
                    } 
                } 
            } 
        } 
    } 
}

Overview of routing
Set up routing for records
Manually route records
Automatically route records using flow