Using Function "StartSession" to Trigger a CU & Pass A parameter to it

I want to trigger a CU in another company from C/Al code. The approach which I am using is as follow:

  1. I created a CU which Insert a rec for me ----> This is the CU that I want to trigger (It takes a Rec as Parameter)

  2. I created another CU which will call the first code unit as follow:

Global variables:

OK ----> Type Boolean

Session Event ------->Rec (Table Active Session)

Resulttable ------- >Rec that I want to pass

// CODE****

Resulttable.FINDLASt; // Get the last rec in my result table

OK := STARTSESSION(SessionEvent.“Session ID”, CODEUNIT::“MZA Insert In Table”, ‘Test Company2’,Resulttable);
IF OK THEN
STOPSESSION(SessionEvent.“Session ID”, ‘Rec was Inserted’)
ELSE
ERROR(‘Session was stoped’);

// *****CODE

The issue is that I tried to fire the CU which include the start session function and it always works one time only!!

I have to restart Navision server 1 time before I can fire this CU once again. any idea why the CU is working only 1 times??

PS: The above code works only if I fire it while tracing it with the debugger! if I fire it without tracing it wonot work?? really strange?

Thanks

Which version of NAV is this issue in?

hi Erik, I am using Nav 2016.

I think you kill the session before it actually started. Startsession starts a parallel process. Why dont you just apply the changecompany function. There is much overhead to start a separate session only to insert a record.

“do not use background sessions for small tasks that occur often because the cost of starting the session for each tasks is high”

better use changecompany