Skip to Content
0

Get company object

Jan 27, 2017 at 07:30 PM

344

avatar image

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;
        }
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
DIEGO LOTHER Jan 27, 2017 at 10:10 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded