📚 SAP Business One SDK Help

StartTransaction Method
See Also  Example

Description

Starts a transaction, allowing you to perform data operations on several business objects.

Use the EndTransaction method to end the transaction and free locked records, allowing other users to access them.

Syntax

Visual Basic
Public Sub StartTransaction() 

Remarks

Use this method when you want to perform data operations on several business objects:

  • If the operations succeed, either commit the transaction to save the data in the database, or roll back to discard the changes. 
  • If one of the operations fail, the DI API rolls back the transaction, which discards the changes. After a failed DI API call in the transaction, you must immediately exit the transaction (see the example below).

Example

When working with transactions, make sure to check the return code when executing SAP Business One APIs and, if an error occurs, immediately exit the transaction. If a call fails, SAP Business One automatically ends the transaction with a rollback (of all actions up to that point); if you do not exit the transaction code, all subsequent code is still executed even though an error occurred.
Starting a transaction (C#)Copy Code
try 

    int errorCode = 0; 
    bool findItem = false; 
    string errorMessage = string.Empty; 
    SAPbobsCOM.Company company = new SAPbobsCOM.Company(); 
 
    company.Server = "PVGD50059267A";//mandatory property 
    //mandatory property in release 8.8 and afterwards. Before release 8.8, dst_MSSQL (SQL Server 2000) is the default value. 
    company.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2005; 
    company.CompanyDB = "2009";//mandatory property 
    company.UserName = "manager";//mandatory property 
    company.Password = "1234";//mandatory property 
 
    company.DbUserName = "sa"; //optional in release 8.8 and afterwards 
    company.DbPassword = "sasa"; //optional in release 8.8 and afterwards 
    company.UseTrusted = false; //optional in release 8.8 and afterwards 
    company.language = SAPbobsCOM.BoSuppLangs.ln_English; //optional 
    //Optional, default value is from DI configuration file in release 8.8 and afterwards 
    company.SLDServer = "PVGD50059267A:40000"; 
    errorCode = company.Connect(); 
    if (errorCode != 0) 
    { 
        //You can also use GetLastError to get the error code and error message at the same time. 
        errorMessage = company.GetLastErrorDescription(); 
        MessageBox.Show("Fail to conect to SAP Business One. " + "Error Code: " + errorCode.ToString() + " Error Message: " + errorMessage); 
        return; 
    } 
 
    company.StartTransaction();  
    SAPbobsCOM.Items item = company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems) as SAPbobsCOM.Items; 
    findItem = item.GetByKey("itemcode"); 
    if (!findItem) 
    { 
        MessageBox.Show("Can not find the item."); 
        return; 
    } 
    item.BarCode = "1234567"; 
    errorCode = item.Update(); 
    if (errorCode != 0) 
    { 
        company.GetLastError(out errorCode, out errorMessage); 
        MessageBox.Show("Fail to update item master data. " + "Error Code: " + errorCode.ToString() + " Error Message: " + errorMessage); 
        //transaction started has been rolled back by SAP Business One automaticly. Just return the code and do your error handling. 
        return; 
    } 
 
    // Get predefined text service  
    SAPbobsCOM.CompanyService companyService = company.GetCompanyService(); 
    SAPbobsCOM.PredefinedTextsService textService = companyService.GetBusinessService(SAPbobsCOM.ServiceTypes.PredefinedTextsService) as SAPbobsCOM.PredefinedTextsService; 
 
    // Add predefined text  
    SAPbobsCOM.PredefinedText text = textService.GetDataInterface(SAPbobsCOM.PredefinedTextsServiceDataInterfaces.ptsPredefinedText) as SAPbobsCOM.PredefinedText; 
    text.TextCode = "text code"; 
    text.Text = "test content"; 
    try 
    { 
        SAPbobsCOM.PredefinedTextParams param = textService.AddPredefinedText(text); 
    } 
    catch (System.Runtime.InteropServices.COMException ex) 
    { 
        MessageBox.Show(ex.ErrorCode.ToString()); 
        MessageBox.Show(ex.Message); 
        //transaction started has been rolled back by SAP Business One automaticly. Just return the code and do your error handling. 
        return; 
    } 
 
    company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit); 
    company.Disconnect(); 

catch (Exception ex) 

    // ... unexpected error 
}

See Also