Skip to Content
0

How to get all COnnected company instances from Cookie.

Jul 13, 2017 at 01:48 PM

66

avatar image

Hi Guyz,

Scenario :

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

Problem:

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

EndIf

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

oCompany.Disconnect()

EndIf

If oCompany.SetSboLoginContext(sConnectionContext) <> 0 Then

ReturnNothing

EndIf

Dim retVal AsInteger = oCompany.Connect()

If retVal <> 0 Then

Dim sErrMsg AsString = ""

oCompany.GetLastError(retVal, sErrMsg)

ThrowNewException(sErrMsg)

EndIf

.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"

Else

OptGlobals.IsHanaDatabase = False

.DBUserName = "SA"

EndIf

EndIf

EndWith

Return pobjCompany

EndFunction

Thanks ?gards,

Pravin Baji

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

DIEGO LOTHER Jul 14, 2017 at 12:28 AM
0

Hi Pravin,

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

Explanation:

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

gui.GetApplication(-1);
or
gui.GetApplication();

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();
gui.Connect((string)Environment.GetCommandLineArgs().GetValue(1));

var processes = System.Diagnostics.Process.GetProcessesByName(@"SAP Business One"); // Get all process of SAP Business One client running.
foreach (var process in processes)
{
    try
    {
        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.
            break;
        }
    }
    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

Share
10 |10000 characters needed characters left characters exceeded
Pravin Baji Jul 14, 2017 at 02:24 PM
0

Hi Diego Lother ,


Yes, This is Working .

Thank you very much for your valuable help.


Thanks and Regards

Pravin Baji



Show 1 Share
10 |10000 characters needed characters left characters exceeded

Hi Pravin,

Great. Don't forget to click on accept button below my answer.

Kind Regards,

Diego Lother

0