Hi
I have beloe code for creating connection in Sap b1. I want to add button in Sales Order form.
C#
int res = 0;
string strret = "";
SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company();
oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008;
oCompany.Server = "127.0.0.1";
oCompany.UseTrusted = false;
oCompany.CompanyDB = "db_name";
oCompany.UserName = "SomeUser";
oCompany.Password = "******";
oCompany.Connect();
oCompany.GetLastError(out res, out strret);
strret = oCompany.GetLastErrorDescription();
MessageBox.Show(strret);Can anyone help
Thanks

Sarthak VarshneyRank #46 · 32K reputationPosted Feb 6, 2025, 11:08 AM EST
You can add a button to the Sales Order form in SAP Business One using the SAP Business One UI API. Below is an example of how you can achieve this:
Steps to Add a Button in the Sales Order Form
ORDR- Form Type 139).Updated Code to Add a Button
using SAPbouiCOM; using SAPbobsCOM; using System; using System.Windows.Forms; class SAPAddon { private static SAPbouiCOM.Application SBO_Application; private static SAPbobsCOM.Company oCompany; public static void Main() { try { // Connect to SAP Business One UI API SboGuiApi sboGuiApi = new SboGuiApi(); string sConnectionString = Environment.GetCommandLineArgs().GetValue(1).ToString(); sboGuiApi.Connect(sConnectionString); SBO_Application = sboGuiApi.GetApplication(); // Connect to SAP Business One DI API oCompany = new SAPbobsCOM.Company(); oCompany.Server = "127.0.0.1"; oCompany.CompanyDB = "db_name"; oCompany.UserName = "SomeUser"; oCompany.Password = "******"; oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008; oCompany.UseTrusted = false; oCompany.Connect(); int res; string strret; oCompany.GetLastError(out res, out strret); MessageBox.Show(strret); // Add button to Sales Order form SBO_Application.ItemEvent += new _IApplicationEvents_ItemEventEventHandler(SBO_Application_ItemEvent); AddButtonToSalesOrderForm(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } private static void AddButtonToSalesOrderForm() { try { Form oForm = SBO_Application.Forms.GetForm("139", 1); // 139 = Sales Order Form Type // Create button item Item oButtonItem = oForm.Items.Add("btnCustom", BoFormItemTypes.it_BUTTON); oButtonItem.Left = oForm.Items.Item("2").Left + 100; // Positioning oButtonItem.Top = oForm.Items.Item("2").Top; oButtonItem.Width = 100; oButtonItem.Height = 20; // Create button object Button oButton = (Button)oButtonItem.Specific; oButton.Caption = "Click Me"; SBO_Application.StatusBar.SetText("Button added successfully!", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success); } catch (Exception ex) { SBO_Application.StatusBar.SetText("Error adding button: " + ex.Message, BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Error); } } // Handle button click event private static void SBO_Application_ItemEvent(string FormUID, ref ItemEvent pVal, out bool BubbleEvent) { BubbleEvent = true; if (pVal.FormTypeEx == "139" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED && pVal.BeforeAction == false) { if (pVal.ItemUID == "btnCustom") { SBO_Application.StatusBar.SetText("Custom Button Clicked!", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning); } } } }Explanation of Code
Connecting to SAP Business One
SboGuiApito connect to the UI API.Adding a Button to the Sales Order Form
btnCustom).Handling the Button Click Event
ItemEvent.btnCustom).Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 6, 2025, 12:18 PM EST
Hello Ramco,
To add a button to the Sales Order form in SAP Business One (SAP B1), you need to use the SAP B1 SDK (Software Development Kit) to create an add-on or extension. The code you provided is for connecting to the SAP B1 company database, but you need to extend it to include the logic for adding a button to the Sales Order form.
Set Up Your Development Environment
Ensure you have the SAP B1 SDK installed.
Use Visual Studio to create a new C# project (e.g., a Class Library or Windows Forms project).
Add references to the SAP B1 SDK libraries (e.g.,
SAPbobsCOM.dllandSAPbouiCOM.dll).Create the Button on the Sales Order Form
Use the
SAPbouiCOMnamespace to interact with the SAP B1 user interface.Add a button to the Sales Order form (
139is the form type for Sales Orders).Here’s an example of how to add a button:
using SAPbouiCOM; using SAPbobsCOM; using System; using System.Windows.Forms; public class AddButtonToSalesOrder { private static Application SBO_Application; private static Company oCompany; public static void Main() { ConnectToSAP(); AddButton(); } private static void ConnectToSAP() { oCompany = new Company(); oCompany.DbServerType = BoDataServerTypes.dst_MSSQL2008; oCompany.Server = "127.0.0.1"; oCompany.UseTrusted = false; oCompany.CompanyDB = "db_name"; oCompany.UserName = "SomeUser"; oCompany.Password = "******"; int res = oCompany.Connect(); if (res != 0) { string errorMsg = oCompany.GetLastErrorDescription(); MessageBox.Show("Connection failed: " + errorMsg); return; } SBO_Application = new Application(); SBO_Application = (Application)SAPbouiCOM.Framework.Application.SBO_Application; } private static void AddButton() { try { // Get the Sales Order form Form oForm = SBO_Application.Forms.GetFormByTypeAndCount(139, 1); // Create a new button Item oItem = oForm.Items.Add("MyButton", BoFormItemTypes.it_BUTTON); oItem.Left = 500; // Position the button oItem.Top = 10; oItem.Width = 100; oItem.Height = 30; Button oButton = (Button)oItem.Specific; oButton.Caption = "My Button"; // Set button text // Add event handler for the button click oButton.Click += new _IButtonEvents_ClickEventHandler(Button_Click); } catch (Exception ex) { MessageBox.Show("Error adding button: " + ex.Message); } } private static void Button_Click(object sboObject, SBOItemEventArg pVal, out bool BubbleEvent) { BubbleEvent = true; MessageBox.Show("Button clicked!"); } }Key Notes:
Ensure you have the necessary permissions to modify forms in SAP B1.
The button position (
Left,Top,Width,Height) can be adjusted as needed.You can extend the
Button_Clickmethod to perform any custom logic you needTuhin PaulRank #7 · 71.9K reputationPosted Feb 6, 2025, 2:50 PM EST
To add a button to the Sales Order form in SAP Business One (SAP B1) using C#, you need to use the SAP Business One SDK . Specifically, you will work with the User Interface API to customize forms and add buttons.
Below is an explanation of how to modify your existing code to add a button to the Sales Order form. See how you can modify your code to add a button to the Sales Order form:
using System; using System.Windows.Forms; using SAPbouiCOM; namespace SAPB1_AddButtonToSalesOrder { class Program { static void Main(string[] args) { // Connect to SAP Business One int res = 0; string strret = ""; SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company(); oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008; oCompany.Server = "127.0.0.1"; oCompany.UseTrusted = false; oCompany.CompanyDB = "db_name"; oCompany.UserName = "SomeUser"; oCompany.Password = "******"; if (oCompany.Connect() != 0) { oCompany.GetLastError(out res, out strret); MessageBox.Show("Connection failed: " + strret); return; } MessageBox.Show("Connected successfully!"); // Get the SAP Business One Application object SAPbouiCOM.Application oApplication = null; try { oApplication = (SAPbouiCOM.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SAPbouiCOM.Application"); } catch (Exception ex) { MessageBox.Show("Failed to get SAP Business One application object: " + ex.Message); return; } // Access the Sales Order form Form oForm = null; try { oForm = oApplication.Forms.Item("139"); // "139" is the unique ID for the Sales Order form } catch (Exception ex) { MessageBox.Show("Failed to access Sales Order form: " + ex.Message); return; } // Add a new button to the Sales Order form try { Item oItem = oForm.Items.Add("MyCustomButton", BoFormItemTypes.it_BUTTON); // Add a button oItem.Left = 10; // Set the button's position (X coordinate) oItem.Top = 10; // Set the button's position (Y coordinate) oItem.Width = 100; // Set the button's width oItem.Height = 20; // Set the button's height Button oButton = (Button)oItem.Specific; oButton.Caption = "Click Me"; // Set the button's caption // Attach an event handler to the button oApplication.SetStatusBarMessage("Custom button added to Sales Order form.", BoMessageTime.bmt_Short, false); // Event handling (optional) oApplication.ItemEvent += new _IApplicationEvents_ItemEventEventHandler(Application_ItemEvent); } catch (Exception ex) { MessageBox.Show("Failed to add button: " + ex.Message); } } // Event handler for button click private static void Application_ItemEvent(string FormUID, ref ItemEvent pVal, out bool BubbleEvent) { BubbleEvent = true; if (pVal.FormTypeEx == "139" && pVal.EventType == BoEventTypes.et_CLICK && pVal.ItemUID == "MyCustomButton") { if (pVal.BeforeAction == false) { MessageBox.Show("Custom button clicked!"); } } } } }Connecting to SAP Business One:
oCompany.Connect()method establishes a connection to the SAP Business One database.Accessing the SAP Business One Application Object:
System.Runtime.InteropServices.Marshal.GetActiveObjectmethod retrieves the active instance of the SAP Business One application.Accessing the Sales Order Form:
oApplication.Forms.Item("139")method accesses the Sales Order form. The"139"is the unique identifier for the Sales Order form.Adding a Button:
Items.Addmethod.ItemandButtonobjects.Handling Button Click Events:
Application_ItemEventmethod handles events on the form.MyCustomButton) is clicked, a message box is displayed.