2
votes

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.

2
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
You should try debugging web service session. There must be an error somewhere.Mak Sim
@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
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
@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

2 Answers

2
votes

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

0
votes

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;