Delete order line through AIF

The following code sample shows how to delete a line of an existing sales order through AIF from outside Dynamics AX 2012 . It uses a partial update, which means that we don’t have to send the whole document back to AX. As the documentation says, we have to include just the fields to change (none in my case) and “any fields required by the document” – that’s why I included fields such as PurchOrderFormNum. Note that you may have different fields set as mandatory.

Also notice how action properties are specified – we’re deleting the line, which means updating the order.

static void Main(string[] args)
{
    using (SalesOrderServiceClient client = new SalesOrderServiceClient())
    {
        EntityKey[] entityKeyList = EntityKeyForSalesId("SO00001");
 
        // Get the order to modify
        var order = client.read(new CallContext(), entityKeyList);
 
        // For demo, always the last order line is deleted
        var lastLine = order.SalesTable[0].SalesLine.Last();
 
        var salesLine = new AxdEntity_SalesLine()
        {
            RecId               = lastLine.RecId,
            RecIdSpecified      = true,
            SalesUnit           = lastLine.SalesUnit,
            action              = AxdEnum_AxdEntityAction.delete,
            actionSpecified     = true
        };
 
        var salesTable = new AxdEntity_SalesTable()
        {
            _DocumentHash           = order.SalesTable[0]._DocumentHash,
            PurchOrderFormNum       = order.SalesTable[0].PurchOrderFormNum,
            ReceiptDateRequested    = order.SalesTable[0].ReceiptDateRequested,
            action                  = AxdEnum_AxdEntityAction.update,
            actionSpecified         = true,
            SalesLine               = new[] { salesLine }
        };
 
        AxdSalesOrder newOrder = new AxdSalesOrder()
        {
            SalesTable = new[] { salesTable }
        };
 
        // Update the order
        client.update(new CallContext(), entityKeyList, newOrder);
    }            
}
 
// Helper method
private static EntityKey[] EntityKeyForSalesId(string salesId)
{
    KeyField field = new KeyField()
    {
        Field = "SalesId",
        Value = salesId
    };
 
    EntityKey key = new EntityKey()
    {
        KeyData = new[] { field }
    };
 
    return new[] { key };
}

3 Comments

  1. Hi Martin,

    Thanks for a post on partial updates.

    I was wondering whether you could do a partial update using X++ instead of .net/C#. I tried and couldn’t find a way to set the action for the SalesTable and SalesLine documents.

    Thanks.

  2. Hi Martin ,

    Thanks for the providing the code. I have one more doubt like how can we cancel the order using aif. Can you provide sample code if possible

    Thanks
    Bhagavan

Leave a Reply

Your email address will not be published.