cancel
Showing results for 
Search instead for 
Did you mean: 

The dreaded -5002

Former Member
0 Kudos

I am creating invoices from sales-orders through the DI-API, and I get the -5002 error

However, most of the invoices are created fine, but on some, I get:

Item no. is missing [INV1.ItemCode][line: 343]

And since it works on most invoices, the error is not because I have done 1 add() too much...

Does anyone know what I may have done wrong?

Here is my code:


rs.DoQuery("SELECT T0.DocEntry FROM ORDR T0 WHERE T0.CardCode = '17' AND T0.DocDueDate <= '01.31.06' AND T0.DocStatus <> 'C' AND T0.DocType = 'I' ORDER BY T0.DocNum");
if (rs.RecordCount != 0)
{
SAPbobsCOM.Documents InvoiceObject = (SAPbobsCOM.Documents)MainClass.SBOCompanyObject.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInvoices);
MainClass.SBOCompanyObject.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oCreditNotes);
InvoiceObject.CardCode = bpName;
InvoiceObject.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Items;
SAPbobsCOM.Document_Lines InvoiceLinesObject = InvoiceObject.Lines;
while (rs.EoF == false)
{
int entry = (int)rs.Fields.Item("DocEntry").Value;
SAPbobsCOM.Documents salesorder = (SAPbobsCOM.Documents)MainClass.SBOCompanyObject.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders);
salesorder.GetByKey(entry);
SAPbobsCOM.Document_Lines orderlines = salesorder.Lines;
for (int t = 0; t < orderlines.Count; t++)
{
InvoiceLinesObject.BaseEntry = salesorder.DocEntry;
InvoiceLinesObject.BaseLine = t;
InvoiceLinesObject.BaseType = (int)SAPbobsCOM.BoObjectTypes.oOrders;
if (t != orderlines.Count - 1)
{
InvoiceLinesObject.Add();
}
}
rs.MoveNext();
if (rs.EoF == false)
{
InvoiceLinesObject.Add();
}
}
int retVal = InvoiceObject.Add();

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hey Runar,

Is that your complete code you have posted? Maybe i am missing something but where do u assign the ItemCode property of the SAPbobsCOM.Document_Lines object? The only time I have received that error stating item no is missing is when I forgot to set that value or the itemcode no longer existed in the system.

former_member201110
Active Contributor
0 Kudos

Hi Runar,

I can't see anything obviously wrong with the code. When you get the error have you tried to manually create the invoice in SBO through the user interface? Maybe doing this will highlight the root of the problem.

Hope this helps,

Owen

Former Member
0 Kudos

Hi Curtis. As the invoice is based on other documents, I only have to set the base-values. And as I said, it works on most invoices.

Owen: I tried creating the documents through the Document Generation Wizard, and then the documents are created fine..

Former Member
0 Kudos

More info:

I have managed to isolate salesorders that raises the errors.

If I run the addon with a flag that those shall not be included, then it works ok.

But when trying to generate an invoice even from that single salesorder it raises the error.

But they don't look any different than the other salesorders, and I may even duplicate them, and then the duplicate generates fine.

If I generate an invoice manually from the sales order, it generates fine.

I'm at a loss here I am afraid... Anyone that has any idea what the error can be?

Former Member
0 Kudos

Hurray! 😄

After LOTS of tedious debugging, I finally found the answer!

And it looks like even the SDK-sample has it wrong! (05.OrderAndInvoice)

What finally brought me to the solution, was to delete one and one line from a sales-order until it was invoiced with my routine.

I had all the original linedata, and when the salesorder was invoiced, I could compare the line to the others, and What finally drew my attention, was a gap in RDR1.LineNum Linenumbering starts at 0, but Row 17 had LineNum 16, whereas Row 18 had LineNum 18. I guess this is what happens if someone deletes a row in the middle of the document.

So, the solution:

Replace this:


InvoiceLinesObject.BaseEntry = salesorder.DocEntry;
InvoiceLinesObject.BaseLine = t;
InvoiceLinesObject.BaseType = (int)SAPbobsCOM.BoObjectTypes.oOrders;

With this:


orderlines.SetCurrentLine(t);
InvoiceLinesObject.BaseEntry = salesorder.DocEntry;
InvoiceLinesObject.BaseLine = orderlines.LineNum;
InvoiceLinesObject.BaseType = (int)SAPbobsCOM.BoObjectTypes.oOrders;

And it will allways work. Using the incrementor to set the BaseLine is unsafe!

I hope this info will help someone. It certainly helped my day..

barend_morkel2
Active Contributor
0 Kudos

Hi Runar,

You should not use an incrementor to set a baseline number (if you are importing only specific lines). The baseline refers to the line number in the parent document. So you must specify which line number you want to create in your target doc (starting with line number 0 for the first line). Using the incrementor will work if you import all the lines of the base document starting with 0 to line.count - 1.

Former Member
0 Kudos

Barend Morkel wrote:

Using the incrementor will work if you import all the lines of the base document starting with 0 to line.count - 1

NO! That is exactly what I thought too, and even the SAP-example does it this way, but my routine WAS designed to import all of the lines of the base document, and it DIDN'T work, there was a gap in the linenum-series, I guess being a result from a row-deletion or something. I therefore say: NEVER base your routine on an incrementor, even if you are going to get all the lines!

barend_morkel2
Active Contributor
0 Kudos

Runar,

Now I get what you are saying - thanks for the heads up!

Former Member
0 Kudos

This was also reported to SAP as a bug by our company. here is the response we received.

I'm sorry but this is a bug.

This issue will be fixed in a patch - please look for the Note Number

897479 in the "Info File.txt" in the patches.

EMAIL 2

========================================

Symptom

Using setCurrentLine in order to update or delete the row with the LineNum numerator will cuase the DI to update / remove the wrong row.

Other terms

DI API, update, delete, SetcurrentLine, visorder, remove, SAP Business One

Reason and Prerequisites

Bug

Solution

This issue will be fixed in a patch - please look for the Note Number in the "Info File.txt"

Until this patch is available there is a workaround available:

1. Use a DI recordset to look for the correct row

2. Use SetCurrentLine with the VisOrder numerator in order to point to the correct line

3. Ignore the data in the Lines Object and update the row with new data or remove the row

Sample project can be found at

http://sapmats-de.sap-ag.de/download/download.cgi?id=LASFXIDGHMIGZKQAF6G758YXOQRYVZPTPQE2YLNQAS7C8IQ...