cancel
Showing results for 
Search instead for 
Did you mean: 

Get company object

Former Member
0 Kudos

Hello

I'm creating an addon and it's my first time using the DI API. Everything's working fine so far but whenever I try to debug the addon it takes up to 10 seconds to load the screen. That's because the connection is taking too long (oCompany var). I was wondering if there's a better way to get the oCompany var, maybe using SBO's current connection or something like that. My code is as follow:

private int SetConnectionContext()
        {
            int setConnectionContextReturn = 0;

            string sCookie = null;
            string sConnectionContext = null;

            // First initialize the Company object

            oCompany = new SAPbobsCOM.Company();

            // Acquire the connection context cookie from the DI API.
            sCookie = oCompany.GetContextCookie();

            // Retrieve the connection context string from the UI API using the
            // acquired cookie.
            sConnectionContext = Application.SBO_Application.Company.GetConnectionContext(sCookie);

            // before setting the SBO Login Context make sure the company is not
            // connected

            if (oCompany.Connected == true)
            {
                oCompany.Disconnect();
            }

            // Set the connection context information to the DI API.
            setConnectionContextReturn = oCompany.SetSboLoginContext(sConnectionContext);

            return setConnectionContextReturn;
        }

        private int ConnectToCompany()
        {
            int connectToCompanyReturn = 0;

            // Establish the connection to the company database.
            connectToCompanyReturn = oCompany.Connect();

            return connectToCompanyReturn;
        }

Accepted Solutions (1)

Accepted Solutions (1)

former_member185682
Active Contributor
0 Kudos

Hi Rudá,

I usually do this:

SAPbouiCOM.SboGuiApi oSboGuiApi = new SAPbouiCOM.SboGuiApi();

try
{
	// Connect in a SAP Application                                                 
	oSboGuiApi.Connect(param);

	SBO_Application = oSboGuiApi.GetApplication(-1);

	SBO_Application.StatusBar.SetText("Add-on connected: " + appName, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success);

	SBO_Company = SBO_Application.Company.GetDICompany();
}
catch (Exception ex)
{
	Log.WriteExceptionLog(ex.Message, ex, this);
	System.Windows.Forms.MessageBox.Show("It was not possible to connect to SAP Business One: " + ex.Message, "Connection Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);                                                             
	System.Environment.Exit(0);
}

Hope it helps.

Kind Regards,

Diego Lother

Answers (0)