Tags

, , ,

Hello Everyone,

Today I would like to share about the small Integration  between Dynamics CRM and Dynamics NAV through web-services.

I had a requirement from one of my client where i need to create the customer in NAV as soon as a new Account is created in CRM.

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("Administrator", "password");
                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)
            {

            }
        }

    }
}

Summing up; when a new Account is created in CRM, this event fires the plugin which creates a Customer in NAV using NAV webservice.

Thanks!!!

🙂

 

Advertisements