on 09-28-2008 4:28 AM
Hi,
I am wondering if you could please help me understand how to add a batch to an order with DIAPI?
We are wanting to add, update, and delete batches, but I am not yet seeing how to do it.
Thank you,
Mike
Hi Michael,
The Document_Lines object has a BatchNumbers property. This returns a BatchNumbers object which is the batches allocated to this line of the document. The BatchNumbers object has an Add method and a SetCurrentLine method as well as a number of properties.
There's a sample of how to use the batch and serial objects with transactions in the SDK. It's sample 4 in the DI API section (by default you should find the samples in C:\Program Files\SAP\SAP Business One SDK\Samples\ if you have installed the SDK. This particular sample is in the COM DI subfolder and there's a C# and VB.Net version).
Kind Regards,
Owen
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Owen,
I am trying this code which I modelled after the sample app, though it isn't working. Can you please see where the mistake might be?
int docNum = 675487;
sapCompany = new Company();
// Set connection properties
sapCompany.Server = databaseServer;
sapCompany.CompanyDB = databaseName;
sapCompany.LicenseServer = diServerLicenseServer;
sapCompany.DbUserName = databaseUserName;
sapCompany.DbPassword = databasePassword;
sapCompany.language = SAPbobsCOM.BoSuppLangs.ln_English;
sapCompany.UserName = companyUserName;
sapCompany.Password = companyPassword;
sapCompany.UseTrusted = true;
// Try to connect
lRetCode = sapCompany.Connect();
if (lRetCode != 0)
{ // if the connection failed
sapCompany.GetLastError(out lErrCode, out sErrMsg);
Console.Write("Error: sErrMsg");
}
else
{
Console.WriteLine("Connection to SAP opened.");
}
//---------------------------------------------------------------------------------
// Connection opened
//---------------------------------------------------------------------------------
int docEntry = GetDocEntryFromDocNum("ORDR", docNum);
sapOrder = ((SAPbobsCOM.Documents)(sapCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)));
sapOrder.GetByKey(docEntry);
sapOrder.Lines.BatchNumbers.SetCurrentLine(0);
sapOrder.Lines.BatchNumbers.BatchNumber = "27509";
sapOrder.Lines.BatchNumbers.Quantity = 1;
sapOrder.Lines.BatchNumbers.BaseLineNumber = 0;
sapOrder.Lines.BatchNumbers.Add();
//---------------------------------------------------------------------------------
// Closing connection
//---------------------------------------------------------------------------------
sapCompany.Disconnect();
I hope it's clear where it is.
Thank you,
Mike
Michael,
I doubt you would not be able to create batch numbers with sales order document object. you will be able to accesss the batch number object succesfully only with documents which allow you to create batches in UI ( like Delivery, GRN PO, Good Receipt ).
you will also have to check the property like 'ManageBatchNumbers' and 'SRIAndBatchManageMethod' of item master object before giving batch numbers.
HTH,
regards,
Binita
Thank you Binita.
I am a little confused, because we can get to the batch assignment from the Sales Order UI, by right clicking on the quantity and selecting "Batch/Serial Numbers". From there, I can assign batches to the line item.
Can you please help me understand how I can assign batch numbers to order line numbers?
Thank you,
Mike
Michael,
My mistake. you can create batches with an order document. but by looking at your code, you are trying to add the batch after you have created the sales order document (you are doing GetByKey). I had similar requirement and this is what SAP support has answered.
hope that is not misleading.
regards,
Binita
Michael,
After the confirmation from SAP about the missing functionality, the way left to me was to create the order itself with DI API (instead of doing an update on batch) and if you want that code, let me know . that way you would be able to create batches. if any one still has a better suggestion, I am all ears.
regards,
Binita
Michael,
sorry for the late reply.
this is the code you want. I have hardcoded the values.
Dim orec As SAPbobsCOM.Documents
Dim oitem As SAPbobsCOM.Items
oitem = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems)
orec = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)
orec.DocDate = Today.Date
orec.DocDueDate = Today.Date
orec.CardCode = "C00001"
orec.Lines.ItemCode = "BAevrytrans"
orec.Lines.Quantity = 3.5
orec.Lines.WarehouseCode = "01"
orec.Lines.UnitPrice = 33
If oitem.GetByKey("BAevrytrans") = True Then
If oitem.ManageBatchNumbers = SAPbobsCOM.BoYesNoEnum.tYES And oitem.SRIAndBatchManageMethod = SAPbobsCOM.BoManageMethod.bomm_OnEveryTransaction Then
orec.Lines.BatchNumbers.BatchNumber = "090800000013" 'the batch should exist in system
orec.Lines.BatchNumbers.Quantity = 3.5
End If
End If
'if you want to add more items, write
'orec.Lines.Add
RetVal = orec.Add
If RetVal <> 0 Then
oCompany.GetLastError(ErrCode, ErrMsg)
MsgBox(ErrMsg)
End If
this will create the order as well as batches.
hope that helps,
regards,
Binita
Hi Binita,
Thank you so much for sharing your code! We've got the batch insert working for one batch, but for more than batch, it doesn't seem to be working.
Would you happen to know why this code works:
int docEntry = GetDocEntryFromDocNum("ORDR", docNum);
sapOrder = ((SAPbobsCOM.Documents)(sapCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)));
sapOrder.GetByKey(docEntry);
sapOrder.Lines.BatchNumbers.SetCurrentLine(0);
sapOrder.Lines.BatchNumbers.BatchNumber = "28194";
sapOrder.Lines.BatchNumbers.Quantity = 2;
sapOrder.Update();
but this code does not work:
int docEntry = GetDocEntryFromDocNum("ORDR", docNum);
sapOrder = ((SAPbobsCOM.Documents)(sapCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)));
sapOrder.GetByKey(docEntry);
sapOrder.Lines.BatchNumbers.SetCurrentLine(0);
sapOrder.Lines.BatchNumbers.BatchNumber = "28194";
sapOrder.Lines.BatchNumbers.Quantity = 2;
sapOrder.Lines.BatchNumbers.Add();
sapOrder.Lines.BatchNumbers.BatchNumber = "25709";
sapOrder.Lines.BatchNumbers.Quantity = 1;
sapOrder.Update();
I've also tried this, which also doesn't appear to work:
int docEntry = GetDocEntryFromDocNum("ORDR", docNum);
sapOrder = ((SAPbobsCOM.Documents)(sapCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)));
sapOrder.GetByKey(docEntry);
sapOrder.Lines.BatchNumbers.SetCurrentLine(0);
sapOrder.Lines.BatchNumbers.BatchNumber = "28194";
sapOrder.Lines.BatchNumbers.Quantity = 2;
sapOrder.Lines.BatchNumbers.Add();
sapOrder.Lines.BatchNumbers.SetCurrentLine(1);
sapOrder.Lines.BatchNumbers.BatchNumber = "25709";
sapOrder.Lines.BatchNumbers.Quantity = 1;
sapOrder.Update();
Thank you so much for your help,
Mike
Hi Binita,
I just realized that the reason adding more than one batch wasn't working is because of the quantities. If I added two batched that equalled less than the total quantity of the line item, it works. For example, this code works:
sapOrder = ((SAPbobsCOM.Documents)(sapCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)));
sapOrder.GetByKey(docEntry);
sapOrder.Lines.BatchNumbers.SetCurrentLine(0);
sapOrder.Lines.BatchNumbers.BatchNumber = "28194";
sapOrder.Lines.BatchNumbers.Quantity = 1;
sapOrder.Lines.BatchNumbers.Add();
sapOrder.Lines.BatchNumbers.BatchNumber = "25709";
sapOrder.Lines.BatchNumbers.Quantity = 1;
sapOrder.Update();
.. but if I changed the quantity to 3 on one of the batches, it wouldn't work because the line item itself is set to a quantity of 2.
I verified this by changing the line item quantity to 10, and then setting the two batches to quantity of 5 and 3, and that worked also.
So far so good. We're adding batches, and can update them, so long as the business rules make sense.
I wonder though, is there a way to delete a batch?
Thank you much,
Mike
Michael,
sorry again for the late reply. get to figure it out now only. Though the question is marked answered, I would like you to try this for sure. since it is working for me.
Dim orec As SAPbobsCOM.Documents
Dim oitem As SAPbobsCOM.Items
oitem = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems)
orec = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)
If orec.GetByKey("59") = True Then
orec.Lines.SetCurrentLine(0)
orec.Lines.BatchNumbers.Quantity = orec.Lines.Quantity
orec.Lines.BatchNumbers.BatchNumber = "090800000004"
orec.Lines.SetCurrentLine(1)
orec.Lines.BatchNumbers.Quantity = orec.Lines.Quantity
orec.Lines.BatchNumbers.BatchNumber = "090800000013"
orec.Lines.SetCurrentLine(2)
orec.Lines.BatchNumbers.Quantity = orec.Lines.Quantity
orec.Lines.BatchNumbers.BatchNumber = "batch010"
orec.Comments = "batch"
RetVal = orec.Update
If RetVal <> 0 Then
oCompany.GetLastError(ErrCode, ErrMsg)
MsgBox(ErrMsg)
End If
End If
the key is, first you set the current line of base document (Lines.SetCurrentLine) and for that row only you will add the batch.
if this hardcoded thing works, you will have to further code for making it sure that the batch quantity for a particular row should not exceed the allocated quantity for that batch. (see in UI). you will come across this error.
hope it works for you.
regards,
Binita
Hi Binita
Thank you very much for sharing the code to add a batch! It is working well and I do understand it.
I am also trying to figure out how to delete a batch, and it seems to be somewhat more complicated.
Do you happen to know how to do that?
Please have a look at this to see what I'm doing:
If you have any ideas, please let me know.
Thank you!
Mike
User | Count |
---|---|
107 | |
12 | |
11 | |
6 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.