Updating Sales line using web service

Hi guys,

[I have posted this on other forum but didn’t get any help yet]

I’m building some data synchronization using Dynamics NAV web service. I managed to create Sales Header and Sales Lines, but When I try to update a sales Line I got this message :

The Sales Lines Exists, Identifiation fields and values: Document Type = ‘Order’, Document No.=‘P-20-B’,Line No.=10 000

from what i understand it seems to be trying to recreate the line instead of doing an update. I’m stuck can you help me?

Here’s my test code:

Where “wsNAV_SalesHeaderAndLines” is a reference to a page wb service with a sales header and a sales line repeater linked to the sales header

Private Sub UpdateSalesLine()
Dim _SalesOrder As New wsNAV_SalesHeaderAndLines.wsNAV_SalesHeaderAndLines_Service
Dim salesOrder As New wsNAV_SalesHeaderAndLines.wsNAV_SalesHeaderAndLines
_SalesOrder.UseDefaultCredentials = False
_SalesOrder.Credentials = New System.Net.NetworkCredential(“wservice”, “aOer00%9”)

Try
salesOrder = _SalesOrder.Read(“1”, “P-20-B”) ’ va chercher la commande dans laquelle il faut travailler

Dim salesLines(0) As wsNAV_SalesHeaderAndLines.WS_SalesLine 'crée un array vide pour mettre les lignes

If Not salesOrder.WS_SalesLine Is Nothing Then
salesLines = salesOrder.WS_SalesLine
End If

If Not _SalesOrder.IsUpdated(salesOrder.Key) Then ’ s’assure que l’objet et encore parreil

Dim idx As Integer = 0
Dim blnLineFound As Boolean = False
While idx < salesLines.Length
Dim oLine As wsNAV_SalesHeaderAndLines.WS_SalesLine
oLine = DirectCast(salesLines(idx), wsNAV_SalesHeaderAndLines.WS_SalesLine)
If oLine.Line_No = 10000 Then
blnLineFound = True
Exit While
End If
idx = idx + 1
End While

If blnLineFound Then

With DirectCast(salesLines(idx), wsNAV_SalesHeaderAndLines.WS_SalesLine) ’ ajuste les valeurs
.Bin_Code = Nothing
.Description = “1 - W 24 X 32 x 17 *2”
.Description_2 = Nothing
.Document_No = “P-20-B”
'.Document_Type = wsNAV_SalesHeaderAndLines.Document_Type_1.Order
'.Document_TypeSpecified = True
.Item_Category_Code = “PF”
.Key = Nothing
'.Line_No = 10000
'.Line_NoSpecified = True
.Location_Code = “COATICOOK”
.Type = wsNAV_SalesHeaderAndLines.Type.Item
.TypeSpecified = True
.No = “GCAISSON”
.Product_Group_Code = “CABINETS”
.Quantity = 1
.QuantitySpecified = True
.Unit_Price = 10000
.Unit_PriceSpecified = True
.Variant_Code = Nothing
End With
End If

_SalesOrder.Update(salesOrder) ’ mets à jour le tout.

End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Hi Patrick,

I’m not a web service expert my self, but I have thought about just the same thing myself.

And with something as specific as this (and primarily being a NAV developer), then I would create a codeunit, and expose this via the web service. That would allow me to control exactly what happens. I.e. then I could validate the fields in the normal NAV way, thus the only thing I would need to expose was the base fields to create/update.

Assuming that the page you’re running the ws on is not the standard sales order page?

Hello Erik,

First thank for you reply!

in fact I did a custom page because I don’t want to modify the original one. On this page (from NAV) I can create sales header and sales line exactly as the “default page” and it works like a charm. But there is something in the WS that cause that me a headache. I guess I could create a CU with all the required parameters and “CRUD” (Create Read Update Delete) the sales line directly from the server itself. But it’s kind of reinvent the wheel as the web service is supposed to do it all as de “CRUD” function are common fonction to a page and it’s the primary fonction of WS to expose them.

If I don’t have any other choice I’ll create the CU but I wish I don’t have to…

Hi Patrick,

As said, then I’m still a newbie myself, when it comes to consuming NAV web services, if would just my self feel more comfortable using a codeunit.

Especially if I where to update fields normally not visible on a page, like line no. etc. - too much could go wrong I think.

It’s also a matter of not delegating too much business logic, to systems outside of NAV. Similar in your code, where do a lot of hard-coding. By using a codeunit, then you would only have to feed it the basic informations, not having to worry about the other “required” fields, but base it entirely on the setup in NAV for that particular customer or/and item. In general I would always go for using web services, which can be used, without having to know a lot about the business logic of the application.

Hello Erick,

I have just create a Code Unit expose in a WS and it worked like a CHARM! Well, it’s more work as you need to recreate the required méthodes but at least it works.

Thanks for the idea.