Search code examples
c#dynamics-crmmicrosoft-dynamicsdynamics-crm-365

Properly creating a SalesOrder in Dynamics 365


I am trying to write a Dynamics 365 CRM plugin in which I want to create a new "salesorder".

I have the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Microsoft.Crm.Sdk.Samples
{
    public class OrderTest : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            // 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"];

                if (entity.LogicalName != "salesorder")
                    return;

                try
                {
                    Entity salesorder = new Entity("salesorder");

                    salesorder["name"] = "order test";

                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    tracingService.Trace("OrderTestPlugin: Creating the order test.");
                    Guid orderId = service.Create(salesorder);

                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the OrderTest plug-in.", ex);
                }
                //</snippetFollowupPlugin3>

                catch (Exception ex)
                {
                    tracingService.Trace("OrderTestPlugin: {0}", ex.ToString());
                    throw;
                }

            }
        }
    }
}

My problem is that it fails creating the salesorder. The error message I get is useless. It says: Download the details and load with Plug-in Profiler. followed by a long token. I don't understand how to create the "salesorder" and how to get a more understandable error message.


Solution

  • The code you have written is for creating salesorder in another execution pipeline using plugin. And you registered this plugin step to run on creation of salesorder entity record itself.

    Hence this code will go on loops, to avoid that you can use depth property to stop the deadlock.

    For your learning purpose, register this plugin step on some other entity & test it.