3

While creating a new sales order using dynamics NAV SOAP webservices, getting below error while attempting to create multiple line items. Order info with single line item being added successfully.

Error - The Sales Line already exists. Identification fields and values: Document Type='Order',Document No.='1111',Line No.='10000'

Here is my code that I have tried :

$client  = new SoapClient($soapWsdl, $options);
    
    // Create order header
    $create = new stdClass();
    $sq = new stdClass();  

    $sq->OrderType = "Order";
    $sq->OrderId = "1111";
     $create->SalesOrderWS = $sq;
    $result = $client->create($create);
    
    
    $key = $result->SalesOrderWS->Key;
    
    $update = new stdClass();
    $sq->Key = $key;
    $sq->CustomerID = "9999";
    
    
    $salesLineList = new stdClass();

     $salesLine = new stdClass();
    $salesLine->Order_Type = 'Order';
    $salesLine->OrderID = '1111';
    $salesLine->LineType = 'Item';
    $salesLine->OrderLineNo = '10000';
    $salesLineList->Sales_Order_Lines_WS[0] = $salesLine;
    $sq->SalesOrderLinesWS = $salesLineList;
    
    $salesLine = new stdClass();
    $salesLine->Order_Type = 'Order';
    $salesLine->OrderID = '1111';
    $salesLine->LineType = 'Item';
    $salesLine->OrderLineNo = '20000';
    $salesLineList->Sales_Order_Lines_WS[1] = $salesLine;
    $sq->SalesOrderLinesWS = $salesLineList;
    
    
    $update->SalesOrderWS = $sq;
    $result = $client->Update($update);

Certainly, something is missed here, but couldn't identify the problem.

Thanks.

Newbie
  • 787
  • 1
  • 7
  • 21
  • Check Edds answer (explanation)... 1. solution is to increment Line No. in your code for each new Line, 2. solution is to increment Line No. on NAV page inside OnInsert event... – lanicor Jul 31 '20 at 22:31
  • 1
    You should try debugging web service session. There must be an error somewhere. – Mak Sim Aug 01 '20 at 05:47
  • @MakSim I have gone through the detailed exception that webservice throw and got the similar info of the error mentioned in the question. Not getting any hint through this. Can you shade some light on another way how can I further debug this? – Newbie Aug 01 '20 at 13:32
  • Well you could try to post error log with stack trace here. As for debugger this depends on Nav version you have. If it is greater than 2009 then it is as simple as open debugger (session list) and click action `Debug next`. If it is 2009, then you need Visual Studio to debug. – Mak Sim Aug 01 '20 at 20:09
  • @MakSim thanks for the Info. I am doing the operations using NAV SOAP Webservice through PHP. I do not have NAV application installed on my end. I am developing for my client. Is there any way to debug this further using web service itself? – Newbie Aug 02 '20 at 06:03
  • Nope. Have you tried to create lines one by one? – Mak Sim Aug 02 '20 at 10:40
  • yes, in that case, the only first line is being added properly and when tried to push second line it shows me - sales lines already exists the same error that mentioned in the question. Spending almost 3 to 4 days behind this and yet have no solution. – Newbie Aug 03 '20 at 13:34

2 Answers2

2

in NAV, the Sales Line table has a primary key that consists of 3 fields:

  • Document Type
  • Document No.
  • Line No.

The system will not accept duplicates and the above combinations must be unique. The line No. is an arbitrary integer that makes line numbers unique, and can contain any number. However, by convention, line numbers are usually given in 10000 increments, i.e. first line is 10000, second line is 20000 etc.

I'm not an expert on PHP (in fact I know next to nothing), but I cannot see in the above code you setting a line number when you are writing sales lines. So, the first line insertion would work, but the next lines will result in a duplicate. I would suggest that as part of writing a sales line, you include a unique Line No. for each line that you write, then it would work ok. edd

Dharman
  • 30,962
  • 25
  • 85
  • 135
Edd
  • 21
  • 3
  • Actually, I have also tried by adding the Line No. in Incremental way, but this was giving me the same error and then I removed as NAV is inserting that number by default. I have updated my question including the Line No. Parameter and still the error is same. Any suggestion please? – Newbie Aug 01 '20 at 04:01
0

I'm not an expert on PHP, but I believe this line of code actually calls the web service that creates the lines:

$sq->SalesOrderLinesWS = $salesLineList;

The first time $salesLineList contains one element with OrderLineNo = 10000. The second time $salesLineList contains two elements - one with OrderLineNo = 10000 and one with OrderLineNo = 20000.

This means the second call will attempt to insert line 10000, which is not possible becuase it is already there.

So I suggest that you remove the first call and keep the second:

$salesLine = new stdClass();
$salesLine->Order_Type = 'Order';
$salesLine->OrderID = '1111';
$salesLine->LineType = 'Item';
$salesLine->OrderLineNo = '10000';
$salesLineList->Sales_Order_Lines_WS[0] = $salesLine;

$salesLine = new stdClass();
$salesLine->Order_Type = 'Order';
$salesLine->OrderID = '1111';
$salesLine->LineType = 'Item';
$salesLine->OrderLineNo = '20000';
$salesLineList->Sales_Order_Lines_WS[1] = $salesLine;

$sq->SalesOrderLinesWS = $salesLineList;
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67