SBX - Search With Button

SBX - Forum Post Title

Testing procedure

Dynamics 365 Business Central Forum

Gregor Soln asked a question on 13 Jun 2019 6:15 AM

Question Status

Suggested Answer

Hi,

I have a situation with test extension which I am building.

Let say I have a two companies in Business Central. I want to perform test a procedure which will include both companies. On first company I will generate some Sales Orders and I will generate Purch. Orders and I will send them to another company to process until the end.

I know that CHANGECOMPANY method exists for records but this does not help. 

How can I in AL code declare or call some method to change company and then on some step i call a method which will change company to another, again.

Anyone did something similar? I heard about sessioninfo variable but honestly I didn't find anything useful for my case. Please help if someone have any idea.

Thanks in advance guys!

Reply
Hannes Holst responded on 13 Jun 2019 6:32 AM
My Badges

hi,

CHANGECOMPANY does change the company of the current record-variable only.

E.g. when you use SalesHeader.CHANGECOMPANY('whatever company it is') all other tables (e.g. Customers) would remain in the original company.

 

To really work completely in a company, you have to start a new session using StartSession: https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/session/session-startsession-method

 

You must set a codeunit as parameter which will be executed in the new company.
E.g. STARTSESSION(SessionId, CODEUNIT::"Create Sales Order", 'whatever company it is', OptionalRecord);

Reply
Gregor Soln responded on 13 Jun 2019 7:54 AM
Suggested Answer

Hi Hannes, thank you for your reply. 

I tired to work with sessions but when I start a session in test function i get error:

"Sessions cannot be started in tests that are run by a TestRunner that has TestIsolation set to Test or Codeunit."

The solution I see in my case is to refactor the test scenario to smaller pieces to avoid sessions. Changing the Test Isolation doesn't make sense. 

Reply
Hannes Holst responded on 13 Jun 2019 9:43 AM
My Badges

Hi,

Yes. Maybe think per company only.

That means, your test creates data in your current company only.

When testing the functions in the other company, you create data accordingly to feed the test functions.

Reply
Gregor Soln responded on 14 Jun 2019 1:36 AM

Yes¸, test per company will be the only option.

Thank you Hannes for your thoughts! I appreciate it!

Reply
Gregor Soln responded on 13 Jun 2019 7:54 AM
Suggested Answer

Hi Hannes, thank you for your reply. 

I tired to work with sessions but when I start a session in test function i get error:

"Sessions cannot be started in tests that are run by a TestRunner that has TestIsolation set to Test or Codeunit."

The solution I see in my case is to refactor the test scenario to smaller pieces to avoid sessions. Changing the Test Isolation doesn't make sense. 

Reply

SBX - Two Col Forum

SBX - Migrated JS