Error attaching a file using the DI API in SAP Business One 9

8.6k views Asked by At

I'm creating an application to attach scanned documents at SAP documents, but I have some problems with that process. I'm using SAP BO 9 PL8 and found the next problems:

When I try to add a new attachment line in a existing attachment (using the attachments2 object) with the update method, the DI try to check older lines, and it's possible that the file not exists in the original source path. So, update method reports an error. I use the code below:

Attachments2 oAtt = oCompany.GetBusinessObject(BoObjectTypes.oAttachments2);  
if (oAtt.GetByKey(doc.AttachmentEntry))  
{  
    oAtt.Lines.Add();  
    oAtt.Lines.FileName = oAttNew.Lines.FileName;  
    oAtt.Lines.FileExtension = oAttNew.Lines.FileExtension;  
    oAtt.Lines.SourcePath = oAttNew.Lines.SourcePath;  
    oAtt.Lines.Override = BoYesNoEnum.tYES;  
    if (oAtt.Update() != 0)  
        throw new Exception(oCompany.GetLastErrorDescription());  
}  

There are some documents in SAP who have a attachment tab, but via DI is not possible access to this functionality. For example the item master data (oItems) or the stock transfer (oStockTransfer). They have a AttachmentEntry field like the Documents object, but the objects haven't a property to add a attachment, so I have to create an activity for this documents.

Documents doc = oCompany.GetBusinessObject(oType);  
doc.GetByKey(int.Parse(docEntry));  
doc.AttachmentEntry = oAtt.AbsoluteEntry;  
StockTransfer oStock = .oCompany.GetBusinessObject(BoObjectTypes.oStockTransfer);  
// oStock.AttachmentEntry = oAtt.AbsoluteEntry FAIL  

When I modify the AttachmentEntry property in LandedCost object, the object fail when I try to update it. If the object have already an attachment (added manually), add a new attachment in a new line works. The error of the first case is: No matching records found (ODBC -2028). When I force a catch block I get this other information: "1320000126 - Incorrect update header field". I use the code below:

LandedCostsService service = oCompany.GetCompanyService().GetBusinessService(ServiceTypes.LandedCostsService);  
LandedCostParams oParam = service.GetDataInterface(LandedCostsServiceDataInterfaces.lcsLandedCostParams);  
LandedCost oLandedCost = service.GetDataInterface(LandedCostsServiceDataInterfaces.lcsLandedCost);  
oParam.LandedCostNumber = int.Parse(docEntry);  
oLandedCost = service.GetLandedCost(oParam);  
if (oAtt.GetByKey(oLandedCost.AttachmentEntry)) {  
   // Code similar to first code block I posted  
}  
else  
{  
    if (oAttNew.Add() != 0)  
        throw new Exception(oCompany.GetLastErrorDescription());  
    oAtt.GetByKey(int.Parse(oCompany.GetNewObjectKey()));  
    oLandedCost.AttachmentEntry = oAtt.AbsoluteEntry;  
    try  
    {  
        service.UpdateLandedCost(oLandedCost);  
    }  
    catch (Exception ex)  
    {  
        throw new Exception(ex.Message + oCompany.GetLastErrorDescription());  
    }  
}  

I need to know what I'm doing wrong or if I need to contact with SAP to inform about these DI issues. I hope you can help me. Thanks in advance.

Regards, Pedro

2

There are 2 answers

0
user8690149 On
{  
oAtt.Lines.Add();  <------ here i think you have a error 

oAtt.Lines.FileName = oAttNew.Lines.FileName;  
oAtt.Lines.FileExtension = oAttNew.Lines.FileExtension;  
oAtt.Lines.SourcePath = oAttNew.Lines.SourcePath;  
oAtt.Lines.Override = BoYesNoEnum.tYES;  
if (oAtt.Update() != 0)  
    throw new Exception(oCompany.GetLastErrorDescription());  
}  

usually in the objects of sap the first line is by default, so you do not need to add a line unless you are going to add more than one line.

maybe it should be like this :

for(int i = 0 ;i< xxxx; i++)
{
oAtt.Lines.setCurrentLine(i);

if(i>0)
{
oAtt.Lines.add();
}
oAtt.Lines.FileName = oAttNew.Lines.FileName;  
oAtt.Lines.FileExtension = oAttNew.Lines.FileExtension;  
oAtt.Lines.SourcePath = oAttNew.Lines.SourcePath;  
oAtt.Lines.Override = BoYesNoEnum.tYES; 
}
if (oAtt.Update() != 0)  
    throw new Exception(oCompany.GetLastErrorDescription()); 

xxxx could be the number of lines you want to add or the number of lines that the object has ( Att.lines.Count )

Blockquote

0
George Santos On

I usually do this and it works!

Private Sub test_NonContinue_LandedCost()

    'Sample code for the non-continuous inventory system
    Dim svrLandedCost As SAPbobsCOM.LandedCostsService = oCompany.GetCompanyService().GetBusinessService(SAPbobsCOM.ServiceTypes.LandedCostsService)

    Dim oLandedCost As SAPbobsCOM.LandedCost = svrLandedCost.GetDataInterface(SAPbobsCOM.LandedCostsServiceDataInterfaces.lcsLandedCost)

    Dim oLandedCostEntry As Long = 0

    Dim GRPOEntry As Integer = 15

    'Landed cost document - item tab line 1
    Dim oLandedCost_ItemLine As SAPbobsCOM.LandedCost_ItemLine
    oLandedCost_ItemLine = oLandedCost.LandedCost_ItemLines.Add
    oLandedCost_ItemLine.BaseDocumentType = SAPbobsCOM.LandedCostBaseDocumentTypeEnum.asGoodsReceiptPO
    oLandedCost_ItemLine.BaseEntry = GRPOEntry
    oLandedCost_ItemLine.BaseLine = 0

    'Landed cost document - item tab line 2
    oLandedCost_ItemLine = oLandedCost.LandedCost_ItemLines.Add()
    oLandedCost_ItemLine.BaseDocumentType = SAPbobsCOM.LandedCostBaseDocumentTypeEnum.asGoodsReceiptPO
    oLandedCost_ItemLine.BaseEntry = GRPOEntry
    oLandedCost_ItemLine.BaseLine = 1

    'Landed cost document - item tab line 3
    'This is a split line –split from second line (BaseEntry = 13, BaseLine = 1)
    oLandedCost_ItemLine = oLandedCost.LandedCost_ItemLines.Add()
    oLandedCost_ItemLine.BaseDocumentType = SAPbobsCOM.LandedCostBaseDocumentTypeEnum.asGoodsReceiptPO
    oLandedCost_ItemLine.BaseEntry = GRPOEntry
    oLandedCost_ItemLine.BaseLine = 1
    oLandedCost_ItemLine.Quantity = 2
    oLandedCost_ItemLine.Warehouse = "02"

    'Landed cost document - cost tab line 1
    Dim oLandedCost_CostLine As SAPbobsCOM.LandedCost_CostLine
    oLandedCost_CostLine = oLandedCost.LandedCost_CostLines.Add
    oLandedCost_CostLine.LandedCostCode = "CB"
    'Suppose the vendor currency is Foreign Currency, if in local currency should set 'oLandedCost_CostLine.amount
    oLandedCost_CostLine.amount = 100
    'oLandedCost_CostLine.AllocationBy = SAPbobsCOM.LandedCostAllocationByEnum.asCashValueAfterCustoms

    'Landed cost document - cost tab line 2
    oLandedCost_CostLine = oLandedCost.LandedCost_CostLines.Add
    oLandedCost_CostLine.LandedCostCode = "EQ"
    oLandedCost_CostLine.amount = 100
    'oLandedCost_CostLine.AllocationBy = SAPbobsCOM.LandedCostAllocationByEnum.asCashValueAfterCustoms

    'Landed cost document - cost tab line 3
    oLandedCost_CostLine = oLandedCost.LandedCost_CostLines.Add
    oLandedCost_CostLine.LandedCostCode = "EQ"
    oLandedCost_CostLine.amount = 100
    'oLandedCost_CostLine.AllocationBy = SAPbobsCOM.LandedCostAllocationByEnum.asCashValueAfterCustoms
    oLandedCost_CostLine.CostType = SAPbobsCOM.LCCostTypeEnum.asVariableCosts


    Dim oLandedCostParams As SAPbobsCOM.LandedCostParams = svrLandedCost.GetDataInterface(SAPbobsCOM.LandedCostsServiceDataInterfaces.lcsLandedCostParams)

    'Add a landed cost
    Try
        oLandedCostParams = svrLandedCost.AddLandedCost(oLandedCost)
        oLandedCostEntry = oLandedCostParams.LandedCostNumber
    Catch ex As Exception
        'exception process
        MsgBox(ex.Message)
    End Try

    'Update a landed cost
    Dim oLandedCostUpdateParams As SAPbobsCOM.LandedCostParams = svrLandedCost.GetDataInterface(LandedCostsServiceDataInterfaces.lcsLandedCostParams)
    Dim oLandedCostUpdate As SAPbobsCOM.LandedCost = svrLandedCost.GetDataInterface(LandedCostsServiceDataInterfaces.lcsLandedCost)

    'Operate on the landed cost
    oLandedCostUpdateParams.LandedCostNumber = oLandedCostEntry
    'Get the landed cost
    Try
        oLandedCostUpdate = svrLandedCost.GetLandedCost(oLandedCostUpdateParams)
    Catch ex As Exception
        'exception process
        MsgBox(ex.Message)
    End Try


    'Split functionality, split line 1
    Dim oLandedCostUpdate_ItemLine As SAPbobsCOM.LandedCost_ItemLine
    oLandedCostUpdate_ItemLine = oLandedCostUpdate.LandedCost_ItemLines.Add()
    oLandedCostUpdate_ItemLine.OriginLine = 1
    oLandedCostUpdate_ItemLine.Quantity = 1
    oLandedCostUpdate_ItemLine.Warehouse = "02"


    Dim oLandedCostUpdate_CostLine As SAPbobsCOM.LandedCost_CostLine
    oLandedCostUpdate_CostLine = oLandedCostUpdate.LandedCost_CostLines.Add()

    oLandedCostUpdate_CostLine.LandedCostCode = "QA"
    oLandedCostUpdate_CostLine.amount = 50
    oLandedCostUpdate_CostLine.CostType = SAPbobsCOM.LCCostTypeEnum.asVariableCosts
    oLandedCostUpdate_CostLine.AllocationBy = LandedCostAllocationByEnum.asQuantity


    Try
        svrLandedCost.UpdateLandedCost(oLandedCostUpdate)
    Catch ex As Exception
        'exception process
        MsgBox(ex.Message)
    End Try

End Sub