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 ?
*This post is locked for comments
You need to add code to check that the session is finished.
Something like this.
Session.setrange("Session ID",SessionID);
While Session.findfirst then begin
end;
//The rest of you code.
The code above will wait for the session to finish and exit before your current code.
Can you actually elaborate your exact requirement? There are lot many ways to achieve the expected results.
You should not write any code in CU1 as it is actually Single Instance Codeunit and would not execute all the times (as you mentioned, you need to restart the NAV server, which is not possible).
You should find out other ways to achieve your result. Use ChangeCompany Function, Make some scheduled reports, or trigger some other events.
Hi Ahmed,
I think your answer explain a lot. But what you mean by add some code and use SLEEP fucntion :)? can you explain more about the design of your Idea?
Thanks in advance,
Mohamed
You need to add SLEEP or some code to wait for the process to finish. The OK returned does not mean that the process will wait for it to finish and then return OK. The StartSESSION runs concurrently to your code. So add some code and use a table to see when it finishes.
The reason it works when you put a break point is that the startsession finishes the process and then your code runs.
Hi,
First thanks alot for your answer!
But my problem is not that I canot insert all Rec, I only want to insert the last Rec as showen in my code.
My problem is that the StartSession Function works only if I add a Red Point (F9) in side my code and then I fire the CU, after that the Debugger will go throw my code and It will work 100% of I keep pressing next step.
If I try to fire the CU without the Debugger (Red point ,F9) it will not insert my Rec. As if he canot see my Rec
Hi,
Try to wrap your code with a REPEAT.. UNTIL loop.
Example would be,
IF Resulttable.FIND('-') THEN //Im finding the first record
BEGIN
OK := STARTSESSION(SessionEvent."Session ID", CODEUNIT::"MZA Insert In Table", 'Test Company2',Resulttable);
REPEAT
.... // Do insert the record here, it goes in the loop until it inserts all records
..
.
UNTIL Resulttable.NEXT = 0;
IF OK THEN
STOPSESSION(SessionEvent."Session ID", '***Rec was Inserted***')
ELSE
ERROR('Session was stopped');
END;
Please understand as this is only the skeleton, not the full implementation.
Hope it helps.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156