Delete SalesLine using SalesSalesOrderService

Hello,

I am trying to delete a single sales line by using the below code:

string recId = dtLine.Rows[0][“RecId”].ToString();
CustomService.EntityKey[] lineKey = new CustomService.EntityKey[1];
lineKey[0] = new CustomService.EntityKey();
lineKey[0].KeyData = new CustomService.KeyField[1];
lineKey[0].KeyData[0] = new CustomService.KeyField();
lineKey[0].KeyData[0].Field = “RecId”;
lineKey[0].KeyData[0].Value = recId;
proxy.delete(context, lineKey);

However i am receiving the following error: Wrong field ID in the key data container in entity key… on the last line (proxy.delete())

Any Help?

Thank you.

I would recommend debugging the delete() operation (in AX). If you need an advice about debugging, you’ll have to tell us which version of AX you’re using.

Hello Martin,

I am running Dynamics AX 2012 R2

Thank you

Please read Debugging Services in AX 2012 first (use a search engine to find such documents). Ask if you run into a particular problem that you can’t resolve by yourself.

Hello Martin,

i just did and i faced the below things.

Here is my code:

public void DeleteSalesLine()

{

SalesOrderServiceClient proxy = new SalesOrderServiceClient();
CallContext context = new CallContext();
context.Company = “USMF”;

EntityKey[] lineKey = new EntityKey[1];

lineKey[0] = new EntityKey();
lineKey[0].KeyData = new KeyField[1];
lineKey[0].KeyData[0] = new KeyField();
lineKey[0].KeyData[0].Field = “RecId”;
lineKey[0].KeyData[0].Value = “5637144576”;

proxy.delete(context, lineKey); //Wrong field ID in the key data container in entity key

}

After debugging the X++ classes i found the below:

In AxdBase.validateEntityKey:

while (listEnumerator.moveNext())

{

if (! keyDataMap.exists(listEnumerator.current()))

{

throw AifFault::fault("@SYS92355", #EntityKeyError);

}

}

it is throwing the following error: Exception of type ‘Microsoft.Dynamics.Ax.Xpp.ErrorException’ was thrown.

Any ideas?

Martin,

my problem is really simple, i couldn’t find any code online on how to delete a sales line. can you provide me with an example on how to delete certain sales line based on the record id?

Thank you man.

Aha, I see your problem. First of all, you’re using a wrong operation. You want to modify an order by removing a line, but you’re using the delete() operation, which is used for for deleting the whole order. Furthermore, if your RecId belongs to a sales line, it’s not a valid identifier of any sales order entity.

You’re changing an existing order, not deleting one, therefore use the update() operation. And set the action property of the sales line to AxdEnum_AxdEntityAction.delete.

Hello Martin,

I followed your advise. However I am now facing another problem.

My Error is logged in the Exception form in System Administration:

Subsystem: Line=1, Pos=9569, Xpath=/SalesOrder/SalesTable[1]/TableDlvAddr[1]

Exception Message: Invalid entity action.

My code is the below:

AxdEntity_SalesLine[] soSalesLine = salesOrder.SalesTable[0].SalesLine;

foreach (AxdEntity_SalesLine line in soSalesLine)

{

if (line.RecId.ToString() == “5637144576”)

{

line.action = AxdEnum_AxdEntityAction.delete;

line.actionSpecified = true;

}

}

salesOrder.SalesTable[0].SalesLine = soSalesLine;

salesOrder.SalesTable[0].action = AxdEnum_AxdEntityAction.update;
salesOrder.SalesTable[0].actionSpecified = true;

proxy.update(context, readRespKeys, salesOrder);

I’ve written a code sample for you: Delete order line through AIF.

Thank you Martin

Hello Martin,

as per my other question posted here, do you have any article on how to create a Credit Note against a sales order?

Reference to my posted question: http://dynamicsuser.net/forums/t/81680.aspx

Best Regards,

Ali K. Barada