Skip to Content
avatar image
Former Member

How to get all COnnected company instances from Cookie.

Hi Guyz,

Scenario :

We have developed MPS wizard in SAP Business One. end of MPS Wizard we are calling EXE for MPS calculation asynchronusly.


If user opened 2 SAP Business one instances on same machine then my exe is taking latest instance (2nd instance) and trying to do all calculations on that instance, in this second instance this MPS related tables are not available becaus of that my exe shows the erros. i need to run my exe on on SAP's 1st instance.

i need help on this issue to how to get 1st company instance through Cookies/Cache.

I am using following code get Company :

PublicSharedFunction Get_Company(OptionalByVal dbname AsString = "") As OptProBaseCommon.Company

If dbname = ""Then

dbname = CurrentContext.SBOApplication.Company.DatabaseName


Dim pobjCompany AsNew OptProBaseCommon.Company(dbname)

With pobjCompany

If .IsConnected() = FalseThen

Dim oCompany AsNew SAPbobsCOM.Company

Dim sCookie AsString

Dim sConnectionContext AsString

sCookie = oCompany.GetContextCookie

sConnectionContext = CurrentContext.SBOApplication.Company.GetConnectionContext(sCookie)

If oCompany.Connected = TrueThen



If oCompany.SetSboLoginContext(sConnectionContext) <> 0 Then



Dim retVal AsInteger = oCompany.Connect()

If retVal <> 0 Then

Dim sErrMsg AsString = ""

oCompany.GetLastError(retVal, sErrMsg)



.CompanyDBName = dbname

.DBPassword = OptiProUtilities.ConfigurationData.GetSectionValue("appSettings", "DBPassword")

.DBServerName = CurrentContext.SBOApplication.Company.ServerName

.DBUserName = "OPTIPRO"

.UseTrusted = False

.UserName = CurrentContext.SBOApplication.Company.UserName

.SBOCompany = oCompany

.SBOCompanyLanguage = oCompany.language

.SBODBServerType = oCompany.DbServerType

If oCompany.DbServerType = BoDataServerTypes.dst_HANADB Then

OptGlobals.IsHanaDatabase = True

.DBUserName = "SYSTEM"


OptGlobals.IsHanaDatabase = False

.DBUserName = "SA"




Return pobjCompany


Thanks ?gards,

Pravin Baji

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jul 14, 2017 at 12:28 AM

    Hi Pravin,

    This application is not an add-on installed on SAP Business One client?


    As I can saw, your problem is how you got your application object. Probably you used a code similar with this:


    With this codes above you always will receive the last instance of SAP Business One client.

    The parameter of GetApplication is the AppId of SAP Business One client, each client opened receive the appid from the last client plus one.

    1° client opened = appid = 0

    2º client opened = appid = 1

    Suppose you close the first client and open a new one. So, your new client will receive the appid 2. The appid 0 is not valid anymore. If you close all clients, the appid will start again with 0.

    For solve your situation you can do a code like this:

    SAPbouiCOM.SboGuiApi gui = new SAPbouiCOM.SboGuiApi();
    var processes = System.Diagnostics.Process.GetProcessesByName(@"SAP Business One"); // Get all process of SAP Business One client running.
    foreach (var process in processes)
            int appId = gui.GetAppIdFromProcessId(process.Id); //For each process that you found get the appid
            SBO_Application = gui.GetApplication(appId); //Retrieve the application instance with the appid that you retrieved.
            if (SBO_Application.Company.DatabaseName.Equals("you database name")) //Check if the current application is connected on the database that you want.
                SBO_Company = SBO_Application.Company.GetDICompany(); //If yes, retrieve the company information.
        catch (Exception ex)
            //do something
    //If you found your client instance, with SBO_Application that you got above, you are able to perform your actions on database

    Hope it helps.

    Kind Regards,

    Diego Lother

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 14, 2017 at 02:24 PM

    Hi Diego Lother ,

    Yes, This is Working .

    Thank you very much for your valuable help.

    Thanks and Regards

    Pravin Baji

    Add comment
    10|10000 characters needed characters exceeded