10 October, 2012

Create a Customer in Dynamics GP 2010 using eConnect

A few days ago I created a simple example to create a vendor in Dynamics GP using eConnect. I received a few requests to show a sample integration to create new customers. Creating new customers in Dynamics GP via eConnect is a common request and usually proceeds the creation of other receivables documents.

The sample below creates a new customer using the eConnect .NET assembly and in-memory serialization.

To run the following code on your machine:
  1. Install the latest version of the eConnect 11 SDK.
  2. Create a new Console Application in Microsoft Visual Studio.
Add references to these dynamic link libraries which are located by default in C:\Program Files (x86)\Microsoft Dynamics\eConnect 11.0\API\. (Ignore the x86 if you are using a 32-bit system.)
  1. Microsoft.Dynamics.GP.eConnect.dll
  2. Microsoft.Dynamics.GP.eConnect.Serialization.dll
Replace the Program.cs class in the project for the new class below.


using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Dynamics.GP.eConnect;
using Microsoft.Dynamics.GP.eConnect.Serialization;
 
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Beginning integration test.");
 
        using (eConnectMethods eConnectMethods = new eConnectMethods())
        {
            try
            {
                Console.WriteLine("Creating a new customer.");
 
                // Modify the connection string for your environment.
                string connectionString = @"data source=localhost; initial catalog=TWO; integrated security=SSPI";
 
                // Create the customer.
                taUpdateCreateCustomerRcd customer = new taUpdateCreateCustomerRcd();
                customer.CUSTNMBR = "TEST001";
                customer.CUSTNAME = "Test Customer";
 
                // Assign the customer to a new master customer type.
                RMCustomerMasterType customerMasterType = new RMCustomerMasterType();
                customerMasterType.taUpdateCreateCustomerRcd = customer;
 
                // Assign the master customer type to a new collection of master customer types.
                RMCustomerMasterType[] customerMasterTypes = { customerMasterType };
 
                // Serialize the master vendor type in memory.
                eConnectType eConnectType = new eConnectType();
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xmlSerializer = new XmlSerializer(eConnectType.GetType());
 
                // Assign the master customer types to the eConnectType.
                eConnectType.RMCustomerMasterType = customerMasterTypes;
 
                // Serialize the eConnectType.
                xmlSerializer.Serialize(memoryStream, eConnectType);
 
                // Reset the position of the memory stream to the start.              
                memoryStream.Position = 0;
 
                // Create an XmlDocument from the serialized eConnectType in memory.
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(memoryStream);
                memoryStream.Close();
 
                // Call eConnect to process the XmlDocument.
                eConnectMethods.CreateEntity(connectionString, xmlDocument.OuterXml);
 
                Console.WriteLine("Successfully created customer {0}.", customer.CUSTNMBR);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured: " + ex.Message);
            }
            finally
            {
                eConnectMethods.Dispose();
            }
        }
 
        Console.WriteLine("Integration test complete." +
                           Environment.NewLine +
                           Environment.NewLine);
        Console.WriteLine("Press <Enter> to continue...");
        Console.ReadLine();
    }
}

Execute the project and you should see the following output:


And you should see the vendor created in Dynamics GP:



1 comment:

Elie Hirschman said...

Thanks for this informative post. Is there eConnect code to link a customer to a vendor as well?