Microsoft Dynamics AX Support

This blog contains posts by the Microsoft Dynamics AX Support Teams Worldwide

Calling the Update Operation on the SalesOrderService

The code is an example of how to call the update method on  sales order in AX 2012 using the SalesOrderService.

 

static void Main(string[] args)

        {

            //First find the order

            AxdSalesOrder salesOrder = Program.FindOrder();

            //Now update it

            Program.UpdateOrder(salesOrder);

        }

 

        static AxdSalesOrder FindOrder()

        {

            CallContext context = new CallContext();
            context.Company = “ceu”;

 

            string salesId = “SO-101244″;

            AxdSalesOrder foundSalesOrder = null;

 

            //Instantiate an instance of the service client class

            SalesOrderServiceClient proxy = new SalesOrderServiceClient();

            try

            {

               
                 foundSalesOrder = proxy.find(context, Program.createQueryCriteria(salesId));

               
                 Console.WriteLine(“The sales order was found.”);

            }

            catch (Exception e)

            {

               
                  Console.WriteLine(“The sales order was not found.”);

            }

 

            return foundSalesOrder;

        }

 

        private static QueryCriteria createQueryCriteria(string salesIdValue)

        {

            CriteriaElement[] criteriaElements = new CriteriaElement[1];

           
criteriaElements[0] = new CriteriaElement();

           
criteriaElements[0].DataSourceName = “SalesTable”;

           
criteriaElements[0].FieldName = “SalesId”;

           
criteriaElements[0].Value1 = salesIdValue;

            QueryCriteria queryCriteria = new QueryCriteria();

           
queryCriteria.CriteriaElement = criteriaElements;

            return queryCriteria;

        }

 

        static void UpdateOrder(AxdSalesOrder salesOrder)

        {

            CallContext context = new CallContext();

           
context.Company = “ceu”;

 

            SalesOrderServiceClient proxy = new SalesOrderServiceClient();

            foreach (AxdEntity_SalesTable salesTable in salesOrder.SalesTable)

            {

               
foreach (AxdEntity_SalesLine salesLine in salesTable.SalesLine)

               
{

                        salesLine.SalesQty = salesLine.SalesQty + 1;

                        salesLine.SalesPrice = salesLine.SalesPrice + 2;

                       
salesLine.SalesPriceSpecified = true;

               
}

            }

 

            try

            {

               
proxy.update(context, Program.createEntityKeyList(), salesOrder);

               
Console.WriteLine(“The sales order was successfully updated”);

            }

            catch (Exception e)

            {

               
Console.WriteLine(“The sales order was not updated.”);

            }

        }

 

        private static EntityKey[] createEntityKeyList()

        {

            EntityKey[] entityKeyList = new EntityKey[1];

            EntityKey entityKey = new EntityKey();

            KeyField[] keyDataList = new KeyField[1];

            KeyField keyField = new KeyField();

           
keyField.Field = “SalesId”;

            keyField.Value = “SO-101244″;

           
keyDataList[0] = keyField;

           
entityKey.KeyData = keyDataList;

           
entityKeyList[0] = entityKey;

            return entityKeyList;

        }