I am working on creating a script to automate the creation of S/O Invoices. It is creating the first invoice without issue, but the second invoice (and everyone after that) has an issue with the Job No even though I am starting a new invoice. If I run the script again (it would start with the second invoice from the first run), that invoice will now go in fine, but the second (and everyone after that) will not go in.
The data would look something like this:
Invoice No | Customer No | Job No |
1 | ABF | 123 |
2 | BCG | 234 |
3 | CDH | 345 |
4 | CDH | 456 |
If I keep running it, eventually invoice 3 in the table above would be the first invoice. In this case, both invoices go in fine as the customer numbers match. Here are the errors I am getting on the SetValue of CustomerNo and JobNo and when I try to write the invoice:
Cust Error: The customer does not match for this job.
Job Error: The customer number must be set.
Invoice Error: The Customer Number is required.S
It seems that the SetKey with a new invoice should have started a new invoice and all previous data would be discarded. I have verified that a new invoice number is being used when it should be. Do I need to perform an additional function that clears out data before starting a new invoice to address this issue? If yes, what would it be?
Here is the (simplified) script for reference. I left out the parts that read and pre-validate the data:
<<READ DATA LOOP>>
<<VALIDATE DATA>>
' Process record
If GoodRecord = 1 Then
Dim InvoiceNumber
InvoiceNumber = ""
If LastCaseNumber=SVMXCaseNumber Then
InvoiceNumber = LastInvoiceNumber
Else
retVal = oInv.nGetNextInvoiceNo(InvoiceNumber)
End If
retVal = oInv.nSetKey(InvoiceNumber)
If retVal = 0 Then
GoodRecord = 0
ErrorMsg = ErrorMsg & ErrHeader & "Inv Set Key Error: " & oInv.sLastErrorMsg & Chr(13) & Chr(10)
End If
LastInvoiceNumber = InvoiceNumber
LastCaseNumber = SVMXCaseNumber
' My attempt to clear the customer and job after creating a new invoice. I get the same results with or without this.
If retVal = 2 Then
retVal = oInv.nSetValueNoValidate("JobNo$", "")
retVal = oInv.nSetValueNoValidate("ARDivisionNo$", "")
retVal = oInv.nSetValueNoValidate("CustomerNo$", "")
End If
retVal = oInv.nSetValue("InvoiceDate$", InvoiceDate)
retVal = oInv.nSetValue("InvoiceType$", "IN")
retVal = oInv.nSetValue("ARDivisionNo$",ARDivisionNo)
retVal = oInv.nSetValue("CustomerNo$",CustomerNo)
If retVal = 0 Then
GoodRecord = 0
ErrorMsg = ErrorMsg & ErrHeader & "Cust Set Value Error: " & oInv.sLastErrorMsg & Chr(13) & Chr(10)
End If
retVal = oInv.nSetValue("JobNo$",SageJobNumber)
If retVal = 0 Then
GoodRecord = 0
ErrorMsg = ErrorMsg & ErrHeader & "Job Set Value Error: " & oInv.sLastErrorMsg & Chr(13) & Chr(10)
End If
retVal = oInv.oLines.nAddLine()
If retVal = 0 Then
GoodRecord = 0
ErrorMsg = ErrorMsg & ErrHeader & "Error adding line record, Error: " & oInv.oLines.sLastErrorMsg & Chr(13) & Chr(10)
Else
If LineType = "Parts" Then
retVal = oInv.oLines.nSetValue("ItemCode$", ProductCode)
retVal = oInv.oLines.nSetValue("WarehouseCode$", WarehouseCode)
Else
If LineType = "Inspections" Then
retVal = oInv.oLines.nSetValue("ItemCode$","/PM")
retVal = oInv.oLines.nSetValue("SalesAcctKey$",SalesAcctKey)
Else
retVal = oInv.oLines.nSetValue("ItemCode$","/LABOR")
retVal = oInv.oLines.nSetValue("SalesAcctKey$",SalesAcctKey)
End If
End If
retVal = oInv.oLines.nSetValue("QuantityShipped",Quantity)
retVal = oInv.oLines.nSetValue("UnitPrice",PriceEach)
retVal = oInv.oLines.nWrite()
If retVal = 0 Then
GoodRecord = 0
ErrorMsg = ErrorMsg & ErrHeader & "Error writing line record, Error: " & oInv.oLines.sLastErrorMsg & Chr(13) & Chr(10)
Else
retVal = oInv.nWrite()
If retVal = 0 Then
GoodRecord = 0
ErrorMsg = ErrorMsg & ErrHeader & "Error writing Invoice record, Error: " & oInv.sLastErrorMsg & Chr(13) & Chr(10)
Else
GoodRecordCount = GoodRecordCount + 1
End If
End If
End If
End If
<<END OF LOOP>>