Add new SalesLine to existing SalesOrder - MS Dynamics AX 2012 - AIF (C#)

1.1k views Asked by At

I need to add SalesLine to an existing SalesOrder. SalesOrder do not have any SalesLine yet. I am using AIF in CSharp (C#). I am using following code but I am getting following exceptions (checked through Dynamics AX Exceptions window) .

  1. Error found when validating record.
  2. Update has been canceled.

Here is my code.

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        SalesOrderCreateReadFindUpdateDelete.KeyField keyField = new SalesOrderCreateReadFindUpdateDelete.KeyField() { Field = "SalesId", Value = "SO-015749" };
        SalesOrderCreateReadFindUpdateDelete.EntityKey entityKey = new SalesOrderCreateReadFindUpdateDelete.EntityKey();
        entityKey.KeyData = new SalesOrderCreateReadFindUpdateDelete.KeyField[1] { keyField };
        SalesOrderCreateReadFindUpdateDelete.EntityKey[] entityKeys = new SalesOrderCreateReadFindUpdateDelete.EntityKey[1] { entityKey };
        SalesOrderCreateReadFindUpdateDelete.SalesOrderServiceClient _Client;
        using (_Client = new SalesOrderCreateReadFindUpdateDelete.SalesOrderServiceClient())
        {
            SalesOrderCreateReadFindUpdateDelete.CallContext _callContext = new SalesOrderCreateReadFindUpdateDelete.CallContext();
            _callContext.Company = "ART";
            SalesOrderCreateReadFindUpdateDelete.AxdSalesOrder _SalesOrderList = _Client.read(_callContext, entityKeys);
            SalesOrderCreateReadFindUpdateDelete.AxdEntity_SalesTable _SalesOrderTable = _SalesOrderList.SalesTable.First();
            SalesOrderCreateReadFindUpdateDelete.AxdEntity_SalesLine salesLine = new SalesOrderCreateReadFindUpdateDelete.AxdEntity_SalesLine();

            salesLine.ItemId = "PF507028";
            salesLine.SalesQty = 1;
            salesLine.SalesUnit = "ea";
            salesLine.SalesId = "SO-015749";
            salesLine.RecId = _SalesOrderTable.RecId;
            salesLine.RecVersion = _SalesOrderTable.RecVersion;

            SalesOrderCreateReadFindUpdateDelete.AxdEntity_InventDim inventDim = new SalesOrderCreateReadFindUpdateDelete.AxdEntity_InventDim();
            inventDim.InventSiteId = "1";
            inventDim.InventLocationId = "13";

            salesLine.InventDim = new SalesOrderCreateReadFindUpdateDelete.AxdEntity_InventDim[1] { inventDim };
            _SalesOrderTable.SalesLine = new SalesOrderCreateReadFindUpdateDelete.AxdEntity_SalesLine[1] { salesLine };

            _Client.update(_callContext, entityKeys, _SalesOrderList);
            lblOutput.Text += "<br />Success";
        }
    }
    catch (Exception ex)
    {
        lblOutput.Text += "<br />Exception: " + ex.Message;
    }
}

Am I missing something or doing something wrong? If I have to provide more fields then where can check these required fields in MS Dynamics. I am new to MS Dynamics.

Please Help! Thank you.

1

There are 1 answers

0
Pradeep Muttikulangara Vasu On BEST ANSWER

In your case you are updating header and inserting lines i.e partial update. Refer below links for code samples. Updating a salesorder using AIF https://community.dynamics.com/ax/b/goshoom/archive/2014/01/21/creating-sales-orders-via-aif-in-ax2012

In your code I suggets to remove below two lines as I think this is not required.

salesLine.RecId = _SalesOrderTable.RecId; 
salesLine.RecVersion = _SalesOrderTable.RecVersion; 

Then modify below line to make sure same sales order is getting updated.

salesLine.SalesId = _SalesOrderTable.SalesId;

Add below four lines for partial update (Header update and line creation).

_SalesOrderTable.action = AxdEnum_AxdEntityAction.update;
_SalesOrderTable.actionSpecified = true; 
salesLine.action = AxdEnum_AxdEntityAction.create; 
salesLine.actionSpecified = true;

Configure troubleshooting options for integration ports so that you can verify the logs. https://technet.microsoft.com/en-us/library/hh202045.aspx

Please verify document log and exception log to see the values passed to Ax. https://technet.microsoft.com/en-us/library/aa834439.aspx https://technet.microsoft.com/en-us/library/aa834351.aspx

To debug AIF refer msdn blog. https://blogs.msdn.microsoft.com/axsupport/2012/06/04/debugging-services-in-ax-2012/