Dynamics NAV 2015 Update Sales Line by Web service : Line already exists

spiky_goldenspiky_golden Member Posts: 44
Hi guys,
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:
Private Sub UpdateSalesLine()
        Dim _SalesOrder As New wsNAV_SalesHeaderAndLines.wsNAV_SalesHeaderAndLines_Service ' this is the Sales Header Service
        Dim salesOrder As New wsNAV_SalesHeaderAndLines.wsNAV_SalesHeaderAndLines  ' this is the Sales Header card
        _SalesOrder.UseDefaultCredentials = False
        _SalesOrder.Credentials = New System.Net.NetworkCredential("username", "password")

        Try
            salesOrder = _SalesOrder.Read("1", "P-20-B") ' get the Sales Header Card

            Dim salesLines(0) As wsNAV_SalesHeaderAndLines.WS_SalesLine ' Create an empty array

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


            If Not _SalesOrder.IsUpdated(salesOrder.Key) Then ' check if object is still the same


                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) ' set values
                        .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) ' update Sales Header Card

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Patrick Venner
programmeur-analyste
Cabico Custom Cabinetry

Best Answer

Answers

  • spiky_goldenspiky_golden Member Posts: 44
    does someone have at least a hint?
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
  • mdPartnerNLmdPartnerNL Member Posts: 802
    If you create a small project file I can test it for you here.

    For now I suggest you to see if writing new lines with a lineno=1000000, +10) works..
  • spiky_goldenspiky_golden Member Posts: 44
    I can give you both methods i use to create and update bu you will need to adapt to your Nav environment. is that ok?
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
  • mdPartnerNLmdPartnerNL Member Posts: 802
    ok
  • spiky_goldenspiky_golden Member Posts: 44
    here it is:
    
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        CreateSalesLine()
         UpdateSalesLine()
    End Sub
    
    
    
        Private Sub CreateSalesLine()
            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("username", "password")
    
            Try
                salesOrder = _SalesOrder.Read("1", "P-20-B") 
                Dim salesLines(0) As wsNAV_SalesHeaderAndLines.WS_SalesLine 
                If Not salesOrder.WS_SalesLine Is Nothing Then
                    salesLines = salesOrder.WS_SalesLine
                End If
    
    
                If Not _SalesOrder.IsUpdated(salesOrder.Key) Then 
    
                    If salesLines.Length = 0 Then 
                        ReDim Preserve salesLines(0) 
                    End If
    
                    Dim newSalesline As New wsNAV_SalesHeaderAndLines.WS_SalesLine()  
                    With newSalesline
                        .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 = salesLines.Length * 10000
                        .Line_NoSpecified = True
    
                        .Location_Code = "COATICOOK"
                        .Type = wsNAV_SalesHeaderAndLines.Type.Item
                        .No = "GCAISSON"
                        .Product_Group_Code = "CABINETS"
                        .Quantity = 10
                        .QuantitySpecified = True
    
                        .TypeSpecified = True
                        .Unit_Price = 1000
                        .Unit_PriceSpecified = True
                        .Variant_Code = Nothing
    
                    End With
    
                    salesLines(salesLines.Length - 1) = newSalesline 
    
    
                    salesOrder.WS_SalesLine = salesLines 
    
                    _SalesOrder.Update(salesOrder) 
    
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        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("username", "password")
    
            Try
                salesOrder = _SalesOrder.Read("1", "P-20-B") 
                Dim salesLines(0) As wsNAV_SalesHeaderAndLines.WS_SalesLine 
                If Not salesOrder.WS_SalesLine Is Nothing Then
                    salesLines = salesOrder.WS_SalesLine
                End If
    
    
                If Not _SalesOrder.IsUpdated(salesOrder.Key) Then 
    
    
                    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)  
                            .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) 
    
    
                  
    
    
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
Sign In or Register to comment.