Compartilhar via


Create methods

The "Create" methods add new data to Dynamics GP. Create methods use objects to specify the type of data being created and the values to be saved. To create an object, use the following basic steps:

  • Instantiate an object of the type to be created.

  • Populate the object's properties with values. All required properties must be assigned a value. All non-required properties that are not populated will receive a default value when the Create method is run.

    Hint: The required properties for each object are indicated in bold in the Microsoft Dynamics GP Web Service Reference online help file.

  • Create a policy object for the create operation. A policy object provides additional control for the create operation. You will learn more about the policies in Create policy.

  • Use the object as a parameter for the Create method.

The following example demonstrates creating a new vendor. The vendor object requires a vendor key to uniquely identify this vendor. Notice how a vendor key object is created and used to populate the required property. The Name property is also populated. The vendor object's remaining properties will use default values.

The vendor object is then passed as the first parameter to the CreateVendor method. The CreateVendor method also requires context and policy objects to be created and passed as the second and third parameters.

Notice how the GetPolicyByOperation method **** instantiates the vendorPolicy object and retrieves the policy for the operation. The "CreateVendor" parameter specifies the default create policy for a vendor object. GetPolicyByOperation also requires a context object for its second parameter.

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

using System;
using System.Collections.Generic;
using System.Text;
using DynamicsGPWebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Vendor vendor;
            VendorKey vendorKey;
            Policy vendorPolicy;

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

            // Be sure the default credential are used
            wsDynamicsGP.UseDefaultCredentials = true;

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

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

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

            // Create a new vendor key
            vendorKey = new VendorKey();
            vendorKey.Id = "TstVndr0001";

            // Populate the vendor object
            vendor = new Vendor();
            vendor.Key = vendorKey;
            vendor.Name = "TestVendor0001";

            // Get the create policy for the vendor
            vendorPolicy = wsDynamicsGP.GetPolicyByOperation("CreateVendor",
            context);

            // Create the vendor
            wsDynamicsGP.CreateVendor(vendor, context, vendorPolicy);
        }
    }
}

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynamicsGPWebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Vendor vendor;
            VendorKey vendorKey;
            Policy vendorPolicy;

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

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

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

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

            // Create a new vendor key
            vendorKey = new VendorKey();
            vendorKey.Id = "TstVndr0001";

            // Populate the vendor object
            vendor = new Vendor();
            vendor.Key = vendorKey;
            vendor.Name = "TestVendor0001";

            // Get the create policy for the vendor
            vendorPolicy = wsDynamicsGP.GetPolicyByOperation("CreateVendor",
            context);

            // Create the vendor
            wsDynamicsGP.CreateVendor(vendor, context, vendorPolicy);

            // Close the service
            if (wsDynamicsGP.State != CommunicationState.Faulted)
            {
                wsDynamicsGP.Close();
            }
        }
    }
}