web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Add new SalesLine to existing SalesOrder

(0) ShareShare
ReportReport
Posted on by

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.

Please Help! Thank you.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Ajit Profile Picture
    8,788 on at

    I do not understand why are you assigning recid and recversion? I am talking about below two lines.

    salesLine.RecId = _SalesOrderTable.RecId;

           salesLine.RecVersion = _SalesOrderTable.RecVersion;

    I think that could be the issue.

  • Community Member Profile Picture
    on at

    Removing RecID and RecVersion did not helped and I got same exception.

    And when I added following lines in my code (suggested by a developer from another forum) I got Invalid entity action. exception.

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


  • Suggested answer
    Ajit Profile Picture
    8,788 on at

    Did you go through with this TechNet blog - technet.microsoft.com/.../cc598792.aspx

  • Community Member Profile Picture
    on at

    Not sure. I am new to Dynamics AX development. If you think its not necessary then please guide how to add lines?

  • Ajit Profile Picture
    8,788 on at

    And as per the error 'Error found when validating record.' you can try to debug your original code and find where it is failing.

  • Community Member Profile Picture
    on at

    Code is not failing in Visual studio. Its the exception returned by MS Dynamics AX. Is there a way to debug Dynamics functions which are being called from Visual Studio?

  • Martin Dráb Profile Picture
    239,399 Most Valuable Professional on at

    Yes, of course. You can find such information in documentation, in this case under Debugging in Microsoft Dynamics AX 2012 > Services.

  • Community Member Profile Picture
    on at

    goshoom 

    If you can please check my code (starting post) and next couple of replies, and suggest that what could be wrong or missing?

    Meanwhile I am reading the link.

  • Martin Dráb Profile Picture
    239,399 Most Valuable Professional on at

    The first thing you should do is simplifying your code to make it more readable. I did a bit of it for you, including splitting the logic for dealing with AIF from the handling of GUI (remember the single responsibility principle and levels of abstraction). I also added some comments you should look at.

    Nevertheless I don't see any obvious bug except of the problem with RecId/RecVersion.

    using SalesOrderCreateReadFindUpdateDelete;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            AddLine();
            lblOutput.Text += "<br />Success";
        }
        catch (Exception ex)
        {
            lblOutput.Text += "<br />Exception: " + ex.Message;
        }
    }
    
    private void AddLine()
    {
        EntityKey[] entityKeys = new EntityKey[1] { CreateEntityKey("SO-015749") };
        
        using (SalesOrderServiceClient client = new SalesOrderServiceClient())
        {
            CallContext callContext = new CallContext()
            {
                Company = "ART"
            };
            
            AxdSalesOrder salesOrderList = client.read(callContext, entityKeys);
            AxdEntity_SalesTable salesOrderTable = salesOrderList.SalesTable.First(); // Check if found
            AxdEntity_SalesLine salesLine = new AxdEntity_SalesLine();
    
            salesLine.ItemId = "PF507028";
            salesLine.SalesQty = 1;
            salesLine.SalesUnit = "ea";
            salesLine.SalesId = "SO-015749"; // Remove duplicity, the same value is hard-coded in KeyField
            salesLine.RecId = salesOrderTable.RecId; // This is completely wrong; you mustn't set the value and you don't want to be the IDs the same anyway
            salesLine.RecVersion = salesOrderTable.RecVersion; // Never set the value
    
            AxdEntity_InventDim inventDim = new AxdEntity_InventDim();
            inventDim.InventSiteId = "1";
            inventDim.InventLocationId = "13";
    
            salesLine.InventDim = new AxdEntity_InventDim[1] { inventDim };
            salesOrderTable.SalesLine = new AxdEntity_SalesLine[1] { salesLine };
    
            client.update(callContext, entityKeys, salesOrderList);
        }
    }
    
    private EntityKey CreateEntityKey(string salesId)
    {
        KeyField keyField = new KeyField() { Field = "SalesId", Value = salesId };
        EntityKey entityKey = new EntityKey();
        entityKey.KeyData = new KeyField[1] { keyField };
        
        return entityKey;
    }
  • Community Member Profile Picture
    on at

    goshoom 

    Thank you so much for your help.  Your edited code is very easy to read and understand, but unfortunately I am getting same exception with your code too.

    Now I am following your given link (https://msdn.microsoft.com/en-us/library/gg860898.aspx#Services) in order to Debug the Dynamics code. I have attached Ax32Serve.exe but now I don't know that where to find Dynamics code in Application Explorer so that I can set a break point there.

    Here is the screenshot of Application explorer.

    application_5F00_explorer.jpg

    Please help me in this regard as well. I am sorry for too many and dumb questions but I am new to Dynamics and its development.

    Thank You!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 39

#2
Michel ROY Profile Picture

Michel ROY 14

#3
imran ul haq Profile Picture

imran ul haq 8

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans