Compartilhar via


Update methods

The "Update" methods are used to change the values of an existing object's properties. Most updates can be performed using the following steps:

  • Instantiate an object of the type to be updated.
  • Create and set the key value for the object being updated.
  • Populate only the properties that are to be updated.
  • Create a policy object for the update operation. A policy object provides additional control for the update operation. You will learn more about the policies in Update policy.
  • Use the object as a parameter for the Update method.

The following example demonstrates updating a vendor object. Notice how the vendor key object's Id property is populated with the ID of an existing vendor. A new vendor object will use this vendor key to specify which vendor to update. A value is supplied for the vendor object's Name property.

The vendor object is then passed as the first parameter of the UpdateVendor method. The UpdateVendor 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. The "UpdateVendor" parameter specifies the default update policy for a vendor object. GetPolicyByOperation also requires a context object for its second parameter.

Cc508736.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;
            VendorKey vendorKey;
            Vendor vendor;
            Policy vendorPolicy;

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

            // Make sure the default credentials are being 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;

            // Specify the vendor to be updated
            vendorKey = new VendorKey();
            vendorKey.Id = "ACETRAVE0001";

            // Create the vendor object and set the value of
            // the field to be updated
            vendor = new Vendor();
            vendor.Name = "A Travel Company, Inc.";
            vendor.Key = vendorKey;

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

            // Update the vendor information
            wsDynamicsGP.UpdateVendor(vendor, context, vendorPolicy);
        }
    }
}

Cc508736.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;
            VendorKey vendorKey;
            Vendor vendor;
            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;

            // Specify the vendor to be updated
            vendorKey = new VendorKey();
            vendorKey.Id = "ACETRAVE0001";

            // Create the vendor object and set the value of
            // the field to be updated
            vendor = new Vendor();
            vendor.Name = "A Travel Company, Inc.";
            vendor.Key = vendorKey;

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

            // Update the vendor information
            wsDynamicsGP.UpdateVendor(vendor, context, vendorPolicy);

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

When performing an update on a sales document (backorder, fulfillment order, invoice, order, quote, or return), the object must first be populated by a retrieve method. Retrieving the object ensures no data fields will be lost when the update is completed.

The following example demonstrates how to update a sales return object. Notice how the sales document key object specifies the sales document to be updated. The GetSalesReturnByKey method uses the key object along with a context object to return the specified sales return object. The sales return object's Comment property is populated with a new value.

The update occurs when the sales return object is passed as the first parameter to the UpdateSalesReturn method. The UpdateSalesReturn 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 salesReturnPolicy object with the default policy for updating sales returns.

Cc508736.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;

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

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

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

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

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

            // Set up a policy object
            Policy salesReturnPolicy = wsDynamicsGP.GetPolicyByOperation
            ("UpdateSalesReturn",context);

            // Create a Sales Return key
            SalesDocumentKey salesReturnKey = new SalesDocumentKey();
            salesReturnKey.Id = "RTN10010";

            // Retrieve the sales return object
            SalesReturn salesReturn = wsDynamicsGP.GetSalesReturnByKey
            (salesReturnKey, context);

            // Update the comment property
            salesReturn.Comment = "Update test comment";

            // Update the sales return object
            wsDynamicsGP.UpdateSalesReturn(salesReturn,
            context, salesReturnPolicy);
        }
    }
}