Compartilhar via


Creating or updating extension data

When an application uses the Dynamics GP service to create a new object or update an existing object that has additional data included in the ExtensionList collection, it is the responsibility of the application to maintain this additional data. When creating new objects, the application must add the necessary Extension objects to the ExtensionList collection. When updating objects, the application must be sure the Extension objects in the collection contain the necessary information.

For instance, in the example discussed in Retrieving extension data, the Customer object has been extended to include contact history information in an extension with the ExtensionId value "ContactHistory". The XML element containing this information has the following format:

<ContactHistory>
    <FirstContactDate>6/6/1999 12:00:00 AM</FirstContactDate>
    <FirstContactSalesperson>NANCY B.</FirstContactSalesperson>
    <LastContactDate>3/1/2006 12:00:00 AM</LastContactDate>
    <LastContactSalesperson>ERIN J.</LastContactSalesperson>
</ContactHistory>

The following C# example demonstrates creating the contact history information that is being included with the Customer object in the ExtensionList collection. An XML element is created that contains the contact history information. A new Extension object is created with the ExtensionId value "ContactHistory". The XML element containing the contact history data is added as the DocExtension for the object. Finally, the new customer object is saved.

Cc508783.LegacyEndpoint(en-us,MSDN.10).gif** Legacy endpoint**

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DynamicsGPWebServiceSample.DynamicsGPService;
using System.Xml;
using System.Globalization;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
         static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Customer customer;
            CustomerKey customerKey;
            Policy customerPolicy;
            DateTime today;

            XmlDocument doc;
            XmlElement contactHistoryXML;
            XmlElement firstContactDateXML;
            XmlElement firstContactSalespersonXML;
            XmlElement lastContactDateXML;
            XmlElement lastContactSalespersonXML;
            XmlText text;

            // Create an instance of the service
            DynamicsGP wsDynamicsGP = new DynamicsGP();

            // Make sure that default credentials are being used
            wsDynamicsGP.UseDefaultCredentials = true;

            // Create a context with which to call the service
            context = new Context();

            // Specify which company to use (lesson company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create a new customer object
            customer = new Customer();

            // Create a customer key
            customerKey = new CustomerKey();
            customerKey.Id = "CONTOSO";
            customer.Key = customerKey;

            // Set properties for the new customer
            customer.Name = "Contoso, Ltd";

            // Customer Address Key
            CustomerAddressKey customerAddressKey = new CustomerAddressKey();
            customerAddressKey.CustomerKey = customerKey;
            customerAddressKey.Id = "PRIMARY";

            // Customer Address List with Contact Person
            CustomerAddress[] customerAddresses = new CustomerAddress[1];
            customerAddresses[0] = new CustomerAddress();
            customerAddresses[0].Key = customerAddressKey;
            customerAddresses[0].ContactPerson = "Steve";
            customer.DefaultAddressKey = customerAddressKey;
            customer.Addresses = customerAddresses;

            // Set today's date
            today = DateTime.Today;

            // Get the create policy for the customer
            customerPolicy = wsDynamicsGP.GetPolicyByOperation
            ("CreateCustomer", context);

            // Add the Contact History (extended data) for the new customer

            // Make a new Extension
            Extension ext = new Extension();
            ext.ExtensionId = "ContactHistory";

            // Make the XML extension document
            doc = new XmlDocument();

            contactHistoryXML = doc.CreateElement("ContactHistory");

            // First Contact Date
            firstContactDateXML = doc.CreateElement("FirstContactDate");
            text = doc.CreateTextNode(today.ToString("G",
            DateTimeFormatInfo.InvariantInfo));
            firstContactDateXML.AppendChild(text);
            contactHistoryXML.AppendChild(firstContactDateXML);

            // First Contact Salesperson
            firstContactSalespersonXML =
            doc.CreateElement("FirstContactSalesperson");
            text = doc.CreateTextNode("PAUL W.");
            firstContactSalespersonXML.AppendChild(text);
            contactHistoryXML.AppendChild(firstContactSalespersonXML);

            // Last Contact Date
            lastContactDateXML = doc.CreateElement("LastContactDate");
            text = doc.CreateTextNode(today.ToString("G",
            DateTimeFormatInfo.InvariantInfo));
            lastContactDateXML.AppendChild(text);
            contactHistoryXML.AppendChild(lastContactDateXML);

            // Last Contact Salesperson
            lastContactSalespersonXML =
            doc.CreateElement("LastContactSalesperson");
            text = doc.CreateTextNode("PAUL W.");
            lastContactSalespersonXML.AppendChild(text);
            contactHistoryXML.AppendChild(lastContactSalespersonXML);

            // Add the extension to the Customer object
            ext.DocExtension = contactHistoryXML;

            Extension[] extensionList = new Extension[1];
            extensionList[0] = ext;

            customer.Extensions = extensionList;

            // Create the customer
            wsDynamicsGP.CreateCustomer(customer, context, customerPolicy);
        }
    }
}

Cc508783.NativeEndpoint(en-us,MSDN.10).gif** Native endpoint **

using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using WebServiceSample.DynamicsGPService;
using System.Globalization;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Customer customer;
            CustomerKey customerKey;
            Policy customerPolicy;
            DateTime today;

            XElement contactHistoryXML;

            // Create an instance of the service
            DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();

            // Create a context with which to call the web service
            context = new Context();

            // Specify which company to use (lesson company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create a new customer object
            customer = new Customer();

            // Create a customer key
            customerKey = new CustomerKey();
            customerKey.Id = "CONTOSO";
            customer.Key = customerKey;

            // Set properties for the new customer
            customer.Name = "Contoso, Ltd";

            // Customer Address Key
            CustomerAddressKey customerAddressKey = new CustomerAddressKey();
            customerAddressKey.CustomerKey = customerKey;
            customerAddressKey.Id = "PRIMARY";

            // Customer Address List with Contact Person
            CustomerAddress[] customerAddresses = new CustomerAddress[1];
            customerAddresses[0] = new CustomerAddress();
            customerAddresses[0].Key = customerAddressKey;
            customerAddresses[0].ContactPerson = "Steve";
            customer.DefaultAddressKey = customerAddressKey;
            customer.Addresses = customerAddresses;

            // Set today's date
            today = DateTime.Today;

            // Get the create policy for the customer
            customerPolicy = wsDynamicsGP.GetPolicyByOperation
            ("CreateCustomer", context);

            // Add the Contact History (extended data) for the new customer

            // Make a new Extension
            Extension ext = new Extension();
            ext.ExtensionId = "ContactHistory";

            // Make the XML extension document
            contactHistoryXML = new XElement("ContactHistory",
                new XElement("FirstContactDate", today.ToString("G",
                    DateTimeFormatInfo.InvariantInfo)),
                new XElement("FirstContactSalesperson", "PAUL W."),
                new XElement("LastContactDate", today.ToString("G",
                    DateTimeFormatInfo.InvariantInfo)),
                new XElement("LastContactSalesperson", "PAUL W."));

            // Add the extension to the Customer object
            ext.DocExtension = contactHistoryXML;

            ExtensionList extensionList = new ExtensionList();
            extensionList.Add(ext);

            customer.Extensions = extensionList;

            // Create the customer
            wsDynamicsGP.CreateCustomer(customer, context, customerPolicy);
        }
    }
}