Hi all!
I'm trying to export item definitions to SAP B1 database. To do that I use the following approach:
int result = -1;
SAPbobsCOM.Items doc = (SAPbobsCOM.Items)cmp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);
/... here goes some code that checks whether the item already existst or not .../
doc.ItemCode = itemCode;
doc.ItemType = SAPbobsCOM.ItemTypeEnum.itItems;
doc.ItemsGroupCode = groupID;
doc.ItemName = itemName;
doc.VatLiable = SAPbobsCOM.BoYesNoEnum.tYES;
if (ItemIsNew)
{
result = doc.Add();
}
else
{
result = doc.Update();
}
ItemIsNew variable points whether the item is new (true) or already exists(false).
If the item is new the variable 'itemCode' is set to a value that ensures its uniqueness, if the item already exists the 'itemCode' variable points to the corresponding record.
Variable 'groupID' points to a valid item group and 'itemName' variable is just the name of the item.
This code works fine if the item already exists - goes through the Update method which returns 0. On the other hand the code fails to insert new items - when through the Add method - the result is 208 which says "Could not commit transaction".
I'm using PL 36. Do you have any ideas what's wrong with my code?
Thanks in advance for your reply
Hi Yavor,
I had a similar problem. I had some custom operations related to OITM table placed in sbo_sp_TransactionNotification, when I moved the code in the new stored procedure sbo_sp_PostTransactionNotice the problem was solved.
May be this will solve your problem too.
Regards,
Svilen
Here is a sample method that demonstrates the problem.
private string CreateNewItem(SAPbobsCOM.Company cmp)
{
if (null != cmp)
{
int result = 0;
try
{
SAPbobsCOM.Items doc = (SAPbobsCOM.Items)cmp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);
doc.ItemCode = "NEWItem_001";
doc.ItemType = SAPbobsCOM.ItemTypeEnum.itItems;
doc.ItemsGroupCode = 100; //at least on my test machine this corresponds to Items group
doc.ItemName = "Name of new item to add";
doc.VatLiable = SAPbobsCOM.BoYesNoEnum.tYES;
//bool val = cmp.InTransaction; //at this point there is no transaction
result = doc.Add(); // the result equals 208
//val = cmp.InTransaction; //at this point the Company object reports being in transaction
if (0 != result)
{
return cmp.GetLastErrorDescription();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
return ex.Message;
}
}
return string.Empty;
}
When executed it returns a non-empty string that says "Could not commit transaction". The interesting thing is that before calling doc.Add() method the company reports not being in a transaction, but after the method that same company states it is in transaction. I don't get it really - I make no calls to cmp.StartTransaction() but when doc.Add() completes with error 208 it seems to have created a transaction which for some reason didn't commit successfully.
Am I missing some data initialization - maybe the item requires some more properties to be set? That code worked on PL 26 and earlier PLs.
Hi Yavor,
Why not try using the GetByKey method of the Items object? The following (vb6) code works fine for me on PL29:
Dim oItm As SAPbobsCOM.Items
Dim sItemC As String
Dim lRet As Long
sItemC = "TestItem"
Set oItm = oCmp.GetBusinessObject(oItems)
If oItm.GetByKey(sItemC) Then
'Update item.
oItm.itemType = SAPbobsCOM.ItemTypeEnum.itItems
oItm.ItemsGroupCode = "135"
oItm.ItemName = "item desc..."
oItm.VatLiable = SAPbobsCOM.BoYesNoEnum.tYES
lRet = oItm.Update
Else
'Add item.
oItm.ItemCode = sItemC
oItm.itemType = SAPbobsCOM.ItemTypeEnum.itItems
oItm.ItemsGroupCode = "135"
oItm.ItemName = "item desc..."
oItm.VatLiable = SAPbobsCOM.BoYesNoEnum.tYES
lRet = oItm.Add
End If
Regards,
Andrew.
hi...
use this code... like this..
IRetCodeLine is a integer value... which gets the status code..
if (IRetCodeLine == 0)
{
if (oCompany.InTransaction)
oCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);
oSBOApplication.StatusBar.SetText("Opertaion Completed Successfully", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success);
}
else
{
if (oCompany.InTransaction)
oCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);
}
Add a comment