Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

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

Posted on by 460

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

  • Rashed Profile Picture
    Rashed 3,765 on at
    RE: Using Function "StartSession" to Trigger a CU & Pass A parameter to it

    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.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Using Function "StartSession" to Trigger a CU & Pass A parameter to it

    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.  

  • M.Zayed Profile Picture
    M.Zayed 460 on at
    RE: Using Function "StartSession" to Trigger a CU & Pass A parameter to it

    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

  • Rashed Profile Picture
    Rashed 3,765 on at
    RE: Using Function "StartSession" to Trigger a CU & Pass A parameter to it

    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.  

  • M.Zayed Profile Picture
    M.Zayed 460 on at
    RE: Using Function "StartSession" to Trigger a CU & Pass A parameter to it

    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

  • Suggested answer
    RE: Using Function "StartSession" to Trigger a CU & Pass A parameter to it

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans