Share via


How to Delete Purchase Orders

You can delete purchase orders from the orders database in the following ways:

This topic explains how to delete purchase orders by specifying conditions that the purchase orders must match.

To delete purchase orders

  1. Create an OrderManagementContext object.

    For more information about creating an OrderManagementContext object, see How to Create an OrderManagementContext Object.

  2. Get the PurchaseOrderManager object from the OrderManagementContext object.

  3. Create a SearchClause object that will match all of the purchase orders that you want to delete.

    For more information about creating a SearchClause object, see How to Create a Search Clause.

  4. Call the DeletePurchaseOrders method on the PurchaseOrderManager object and pass the SearchClause object as a parameter.

Example

The following code example deletes purchase orders that have a status of "Completed".

using System;
using System.Data;
using System.Globalization;
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Orders;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Create the OrderManagementContext object. This
                // example accesses the Orders System in local mode by
                // creating an OrderSiteAgent object. You could also
                // access the Orders System in agent mode by creating
                // an OrderServiceAgent object.

                // In the following, replace "StarterSite" with the
                // name of your site.
                OrderSiteAgent ordersAgent = new OrderSiteAgent("StarterSite");
                OrderManagementContext context = OrderManagementContext.Create(ordersAgent);
                PurchaseOrderManager manager = context.PurchaseOrderManager;

                // Create a search clause.

                DataSet searchableProperties = manager.GetSearchableProperties(CultureInfo.CurrentUICulture.ToString());
                SearchClauseFactory searchClauseFactory = manager.GetSearchClauseFactory(searchableProperties, "PurchaseOrder");
                SearchClause clause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Status", "Completed");

                // Delete purchase orders that match the conditions.

                int recordsDeleted;
                manager.DeletePurchaseOrders(clause, out recordsDeleted);

                Console.WriteLine("Deleted " + recordsDeleted.ToString() + " records.");
                Console.ReadLine();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
                }
                Console.ReadLine();
            }
        }
    }
}

Your database must contain purchase orders whose Status field contains the string "Completed" for this example to delete any records.

Compiling the Code

To run this code example, create a console application and add references to the following assemblies:

  • Microsoft.CommerceServer.CrossTierTypes.dll

  • Microsoft.CommerceServer.Orders.CrossTierTypes.dll

  • Microsoft.CommerceServer.Orders.DataManagement.dll

See Also

Other Resources

Working with Orders Data Management Objects