Visual Basic Control of API - Insert an Invoice

Discussion in 'Developers Forum' started by PCSG, Nov 20, 2010.

  1. PCSG New Member

    I have used the excellent article already in the forum to "add a customer" and now wish to extend this to adding an invoice.

    Here is my effort so far:
    Code:
    Dim myTransaction As New ServiceReference1.Invoice
            Dim bind As New ServiceModel.BasicHttpBinding
            bind.Security.Mode = ServiceModel.BasicHttpSecurityMode.Transport
    
            Dim endpoint As New ServiceModel.EndpointAddress("https://securedwebapp.com/api/service.asmx")
    
            Dim wsService As New ServiceReference1.KashFlowAPISoapClient(bind, endpoint)
    
            myTransaction.CustomerID = "23462"
            myTransaction.InvoiceDate = "01/04/2010"
            myTransaction.InvoiceNumber = "8123"
            myTransaction.NetAmount = "10.00"
            myTransaction.VATAmount = "1.75"
            myTransaction.CustomerReference = "Test Transaction"
    
            Dim status As String = ""
            MsgBox(wsService.InsertInvoice("", "", "MYID", "MYPASSWORD", myTransaction))
    
    The customer does exist. When I run this I just get a zero return (i.e. failure).

    Has anyone got something like this working or can they see what I am doing wrong. All help appreciated.

    Angus
  2. DuaneJackson Administrator

    Hi Angus,

    In the response you get, there should be a Status and StatusDetail field. The Status will probably be "NO" as it failed (you get "OK" if it was a success).

    What do you have in the StatusDetail field? That should tell you why it has failed.

    It's also worth pointing out that the NetAmount and VatAmount field are read-only. They are calculated automatically based on the collection of Lines. See KashFlow API - Manual - Classes - Invoice

    So your code above would create an invoice with no lines and a value of 0.
  3. PCSG New Member

    Sample Code (Now Working)

    Thanks for the hint on what I may be doing wrong. I have now got a fully working VB subroutine to insert an invoice and can start developing the other aspects. Just to say you have a really powerful system here that knocks the spots off the competition (especially Sage). Now on with the coding.....

    Here is my code (tidied up a bit).
    Code:
            ' Dimension Local Variables - Return Values
            Dim iResult As Integer
            Dim sStatus As String = ""
            Dim sStatusDetail As String = ""
    
            ' Dimension Local Variables - Function Call
            Dim uTransaction As New ServiceReference1.Invoice
            Dim uTransactionLine As New ServiceReference1.InvoiceLine
            Dim uBinding As New ServiceModel.BasicHttpBinding
            uBinding.Security.Mode = ServiceModel.BasicHttpSecurityMode.Transport
            Dim uEndpoint As New ServiceModel.EndpointAddress("https://securedwebapp.com/api/service.asmx")
            Dim uService As New ServiceReference1.KashFlowAPISoapClient(uBinding, uEndpoint)
    
            ' Define Main Invoice Transaction
            uTransaction.InvoiceNumber = "1234"
            uTransaction.InvoiceDate = "01/04/2010"
            uTransaction.CustomerID = "2874275"
            uTransaction.CustomerReference = "Test Transaction"
    
            ' Insert Invoice / Display Result
            iResult = uService.InsertInvoice(sStatus, sStatusDetail, "YOURUSERID", "YOURPASSWORD", uTransaction)
            MsgBox("Return: " & Format(iResult, "0") & Chr(13) & "Status: " & sStatus & Chr(13) & "Detail: " & sStatusDetail)
    
            ' Define Invoices Line
            uTransactionLine.Quantity = "1"
            uTransactionLine.Description = "Consultancy"
            uTransactionLine.Rate = "10.00"
            uTransactionLine.ChargeType = "4000"
            uTransactionLine.VatAmount = "1.75"
            uTransactionLine.VatRate = "17.5"
    
            ' Insert Invoice Line / Display Result
            iResult = uService.InsertInvoiceLineWithInvoiceNumber(sStatus, sStatusDetail, "YOURUSERID", "YOURPASSWORD", iResult, uTransactionLine)
            MsgBox("Return: " & Format(iResult, "0") & Chr(13) & "Status: " & sStatus & Chr(13) & "Detail: " & sStatusDetail)
    
  4. PCSG New Member

    Insert an Invoice - Working Code

    Thanks so much for the pointers. I then worked out what I was doing wrong and now have a working piece of VB Code. Still looking to refine it but thought it worth posting my test transaction to help others (It is "rough and ready" so does not include full error checking).

    Code:
            ' Dimension Local Variables - Return Values
            Dim iResultInvoice, iResultInvoiceLine As Integer
            Dim sStatus As String = ""
            Dim sStatusDetail As String = ""
    
            ' Dimension Local Variables - Function Call
            Dim uTransaction As New KashFlowAPIRef.Invoice
            Dim uTransactionLine As New KashFlowAPIRef.InvoiceLine
            Dim uBinding As New ServiceModel.BasicHttpBinding
            uBinding.Security.Mode = ServiceModel.BasicHttpSecurityMode.Transport
            Dim uEndpoint As New ServiceModel.EndpointAddress("https://securedwebapp.com/api/service.asmx")
            Dim uService As New KashFlowAPIRef.KashFlowAPISoapClient(uBinding, uEndpoint)
    
            ' Define Main Invoice Transaction
            uTransaction.InvoiceNumber = "1234"
            uTransaction.InvoiceDate = "01/04/2010"
            uTransaction.CustomerID = "2874275"
            uTransaction.CustomerReference = "Test Transaction"
    
            ' Insert Invoice / Display Result
            iResultInvoice = uService.InsertInvoice(sStatus, sStatusDetail, My.Settings.UserName, My.Settings.Password, uTransaction)
            MsgBox("Return: " & Format(iResultInvoice, "0") & Chr(13) & "Status: " & sStatus & Chr(13) & "Detail: " & sStatusDetail)
    
            ' Define Invoices Line
            uTransactionLine.Quantity = "1"
            uTransactionLine.Description = "Consultancy"
            uTransactionLine.Rate = "10.00"
            uTransactionLine.ChargeType = "4000"
            uTransactionLine.VatAmount = "1.75"
            uTransactionLine.VatRate = "17.5"
    
            ' Insert Invoice Line / Display Result
            iResultInvoiceLine = uService.InsertInvoiceLineWithInvoiceNumber(sStatus, sStatusDetail, My.Settings.UserName, My.Settings.Password, iResultInvoice, uTransactionLine)
            MsgBox("Return: " & Format(iResultInvoiceLine, "0") & Chr(13) & "Status: " & sStatus & Chr(13) & "Detail: " & sStatusDetail)
    

Share This Page