Share via


How to Update a Single Purchase Order

You can update a Purchase Order in the database by using the UpdatePurchaseOrder method of the PurchaseOrderManager object. You create a dataset that represents the modified purchase order, and then call the UpdatePurchaseOrder method and provide the dataset as an argument. The UpdatePurchaseOrder method retrieves a purchase order with the same OrderGroupId as the purchase order in the dataset, and then replaces the purchase order in the database with the purchase order specified by the dataset.

An application that calls the UpdatePurchaseOrder method must run under an account that has OrdersViewer role. If the application updates payments, the account the application runs under must also have permission to perform the ViewPurchaseOrderPayments task.

To update a purchase order in the database

  1. Create an OrderManagementContext object.

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

  2. Get the PurchaseOrderManager object from the OrderManagementContext object.

  3. Get the OrderGroupId of the purchase order you want to update.

  4. Retrieve a dataset representation of the purchase order by calling the GetPurchaseOrderAsDataSet method of the PurchaseOrderManager object and passing the OrderGroupId as a parameter.

  5. In the dataset, change the values that you want to update.

  6. Call the UpdatePurchaseOrder method of the PurchaseOrderManager object and pass the updated dataset as the first parameter. Pass the ID that you use for your application as the second parameter.

    For more information about the application ID and its use, see How to Coordinate Updates to Orders.

Example

The following code example changes the status of an order to "Cancelled".

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;

                // Get a DataSet that represents the purchase order.

                // In the following, replace the value of the Guid 
                // with the value in the OrderGroupId column of a 
                // purchase order in your database.
                Guid id = new Guid("c3441414-4445-4daf-a4b0-13c369432d22");
                DataSet po = manager.GetPurchaseOrderAsDataSet(id);

                // Cancel this purchase order by changing the
                // value of the Status field to "Cancelled".
                po.Tables["PurchaseOrder"].Rows[0]["Status"] = "Cancelled";

                // Save the purchase order. The second argument 
                // is the application ID that is used to 
                // coordinate updates to the database.
                manager.UpdatePurchaseOrder(po, "LOB Adapter");

                // Get the DataSet that represents the purchase order
                // again, and display the value of its Status column.
                DataSet updatedPo = manager.GetPurchaseOrderAsDataSet(id);
                Console.WriteLine("Status = {0}.", updatedPo.Tables["PurchaseOrder"].Rows[0]["Status"]);
                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();
            }
        }
    }
}

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

How to Bulk Update Purchase Orders

How to Coordinate Updates to Orders

Updating Purchase Orders