To begin with I started writing a simple plugin where m fetching the values from the account being created using the Target Entity.
Just for testing purpose m currently fetching only Account Name,No and Type.

  • Now I’ll add the NAV webservice to my plugin through AddServiceReference->Advanced->AddWebService

  • Add the URl for the NAV webservice created for Customer, give it a proper name (I gave name as CustomerService).

I then wrote a Method including parameters through which i would like to create the Customers in NAV, using NAV Customer webservice. With this Webservice I’ll initialise the Customer object and the Customer_Service service with the given URL and Network-Credentials.
In my environment the NAV is on another server. So, I’ll use the NAV Customer webservice with the proper Network-Credentials of that server.
I’ll then assign all the values to the respective fields of the Customer object. M using the CustNo in NAV as a manual entry on which the Customer will be created. Finally using the create method of the webservice referencing the Customer object, the customer will be created in NAV.

  • I’ll call the above method inside my Execute Method with values as parameters.

  • Register this plugin on Create message of Account as primaryEntity on Post-Operation event-pipeline.
The Final Code will go like this:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel;

namespace AccountCreation
{
    public class Acc:IPlugin
    {
        IOrganizationService service;
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];

                if (entity.LogicalName == "account")
                {
                    try
                    {
                        Guid accId = entity.Id;
                        string name = (string)entity.Attributes["name"];
                        string accNo = (string)entity.Attributes["accountnumber"];
                        var accCategory = ((OptionSetValue)entity.Attributes["accountcategorycode"]).Value;
                        string custType = "";
                        switch(accCategory)
                        {
                            case 1:
                                custType = "INTERNATIONAL";
                                break;
                            default :
                                custType = "DOMESTIC";
                                break;

                        }

                        CustomerCreate(accNo, name, custType);

                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException("An error occured in plugin. "+ ex, ex);
                    }
                }

             }

        }

        public void CustomerCreate(string CustomerCode, string CustomerName, string CustomerType)
        {
            try
            {
                CustomerService.Customer obj = new CustomerService.Customer();
                CustomerService.Customer_Service objSer = new CustomerService.Customer_Service();
                objSer.Credentials = new System.Net.NetworkCredential("Administratorassword");
                objSer.Url = "http://<server-name>/DynamicsNAV/WS/<Organization>/Page/Customer";

                obj.No = CustomerCode;
                obj.Name = CustomerName;
                obj.Customer_Posting_Group = CustomerType;               

                objSer.Create(ref obj);
            }
            catch (Exception ex)
            {

            }
        }

    }
}