Delete or void methods
The "Delete" or "Void" methods remove an object from or void an object in the database. To delete or void an object, use the following basic steps:
- Create a key object for the type of document you want to delete or void.
- Populate the key object with the unique ID of the document.
- Create a policy object for the delete or void operation. A policy object provides additional control for the delete or void operation. You will learn more about the policies in Delete or void policy.
- Pass the key object to the Delete or Void method.
The following C# example demonstrates deleting a customer. Notice how a customer key object is created and populated with the unique ID value "CONTOSO" for the customer. The DeleteCustomer policy is retrieved to populate the policy object. The customer key object is passed as the first parameter to the DeleteCustomer method. The DeleteCustomer method also requires the context and policy objects to be passed as the second and third parameters.
** Legacy endpoint**
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using DynamicsGPWebServiceSample.DynamicsGPService; namespace DynamicsGPWebServiceSample { class Program { static void Main(string[] args) { CompanyKey companyKey; Context context; CustomerKey customerKey; Policy customerPolicy; // Create an instance of the service DynamicsGP wsDynamicsGP = new DynamicsGP(); // Be 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 customer key customerKey = new CustomerKey(); customerKey.Id = "CONTOSO"; // Get the delete policy for the customer customerPolicy = wsDynamicsGP.GetPolicyByOperation ("DeleteCustomer", context); // Delete the customer wsDynamicsGP.DeleteCustomer(customerKey,context,customerPolicy); } } }
** Native endpoint **
using System; using System.Linq; using System.Text; using System.Windows.Forms; using WebServiceSample.DynamicsGPService; namespace DynamicsGPWebServiceSample { class Program { static void Main(string[] args) { CompanyKey companyKey; Context context; CustomerKey customerKey; Policy customerPolicy; // Create an instance of the service DynamicsGPClient wsDynamicsGP = new DynamicsGPClient(); // 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 customer key customerKey = new CustomerKey(); customerKey.Id = "CONTOSO"; // Get the delete policy for the customer customerPolicy = wsDynamicsGP.GetPolicyByOperation ("DeleteCustomer", context); // Delete the customer wsDynamicsGP.DeleteCustomer(customerKey,context,customerPolicy); // Close the service if (wsDynamicsGP.State != CommunicationState.Faulted) { wsDynamicsGP.Close(); } } } }