Job Queue with Task Scheduler and User ID

Hello,

I am facing problems in NAV 2018 with the Task Scheduler that runs Job Queues.

When a new NAV session is opened, all Job Queues are rescheduled by the codeunit Job Queue User Handler (function RescheduleJobQueueEntriesOnCompanyOpen).

My troubles come from the fact that these tasks will be executed on behalf of the current User ID.
So, a Job Queue will always run with the permission of the last logged in user instead of the User ID on the Job Queue Entry.

Have you already faced this problem? Is there a workaround?

Thank you in advance for your help.

Best Answer

Answers

  • JuhlJuhl Posts: 670Member
    I have done the same
    Follow me on my blog juhl.blog
  • nicolassaleronnicolassaleron Posts: 11Member
    Thank you, I will implement the trick :)
  • zeonzeon Posts: 130Member
    I still have a problem with this in NAV 2018 CU6. Could be nice to see some of tyhe actual code changes you guys have made.

    The extremely annoying thing is that when a new NAV session is opened, all Job Queues are rescheduled by the codeunit Job Queue User Handler (function RescheduleJobQueueEntriesOnCompanyOpen).

    Even though I have tried to modify the code in COD453 the scheduled task is still created in the user name for the user who logs on to the company next time.

    So, could be nice to get a hint about the code change you've made.
  • DoomhammerDoomhammer Posts: 207Member
    Hi, I am struggling with Task Scheduler.
    At our customer with NAV 2017 CU 18, we had C/AL code, which changed user ID after Scheduled Task has been created.
    Now we merged CU 29 and our code, which changed user ID stopped working. Modification of system Table Scheduled Task has now some internal validation, which prevents to changing user ID:
    "The task cannot be run because the user account that is assigned to run the task has been altered. The task has been canceled."
    Customer does not want to run scheduled tasks under ID of user, who enabled scheduled task, they want to run tasks under another user ID.

    Please, can anyone help me? Thanks!
    Martin Bokůvka, AxiomProvis
  • DoomhammerDoomhammer Posts: 207Member
    Bump, anyone knows? Please :)
    Martin Bokůvka, AxiomProvis
  • MariocvMariocv Posts: 5Member
    Doomhammer wrote: »
    Bump, anyone knows? Please :)

    I am joining with the same question; we are facing the same issue on the BC130 on-prem CU08 platform.
  • JuhlJuhl Posts: 670Member
    Just subscribe to table "Job Queue Entry" OnAfterModify event, and set the userid.

    I do this in 2017, and works fine.
    Follow me on my blog juhl.blog
  • MariocvMariocv Posts: 5Member
    Juhl wrote: »
    Just subscribe to table "Job Queue Entry" OnAfterModify event, and set the userid.

    I do this in 2017, and works fine.

    Actually, there is not a problem with the "User ID" in the Job Queue Entry, but with the "User ID" in the Scheduled Task. Anything I do, I am not able to change it, therefore all the background sessions started by the scheduler run in the context of the user, who created the Job Queue Entry record. So, e.g. all the User Id's, everywhere in the system, are filled by the wrong user name and so on...
  • ReinhardReinhard Posts: 249Member
    Mariocv you are on the right track. Subscribe to the scheduled task and then make your changes there.

    ```

    [EventSubscriber(ObjectType::Table, Database::"Scheduled Task", 'OnBeforeInsertEvent', '', false, false)]
    local procedure ScheduledTaskOnBeforeInsert(var Rec: Record "Scheduled Task")
    var
    User: Record User;
    JobQueueEntry: Record "Job Queue Entry";
    JobQueueRecRef: RecordRef;
    FldRef: FieldRef;
    begin
    if Rec."Run Codeunit" <> 448 then exit;

    JobQueueRecRef := Rec.Record.GetRecord();
    if not JobQueueRecRef.Find() then exit;

    if JobQueueRecRef.Number() <> Database::"Job Queue Entry" then exit;

    FldRef := JobQueueRecRef.Field(2);

    User.SetRange("User Name", FldRef.Value());
    if not User.FindFirst() then exit;

    if Rec."User ID" = User."User Security ID" then exit;

    Rec."User ID" := User."User Security ID";
    Rec."User Name" := User."User Name";
    end;
    ```
  • manjumanju Posts: 4Member
    did anyone succeed in resolving '"The task cannot be run because the user account that is assigned to run the task has been altered. The task has been canceled." error?. If yes, please let me know how it is done.
  • MariocvMariocv Posts: 5Member
    Reinhard wrote: »
    Mariocv you are on the right track. Subscribe to the scheduled task and then make your changes there.

    ```

    [EventSubscriber(ObjectType::Table, Database::"Scheduled Task", 'OnBeforeInsertEvent', '', false, false)]
    local procedure ScheduledTaskOnBeforeInsert(var Rec: Record "Scheduled Task")
    var
    User: Record User;
    JobQueueEntry: Record "Job Queue Entry";
    JobQueueRecRef: RecordRef;
    FldRef: FieldRef;
    begin
    if Rec."Run Codeunit" <> 448 then exit;

    JobQueueRecRef := Rec.Record.GetRecord();
    if not JobQueueRecRef.Find() then exit;

    if JobQueueRecRef.Number() <> Database::"Job Queue Entry" then exit;

    FldRef := JobQueueRecRef.Field(2);

    User.SetRange("User Name", FldRef.Value());
    if not User.FindFirst() then exit;

    if Rec."User ID" = User."User Security ID" then exit;

    Rec."User ID" := User."User Security ID";
    Rec."User Name" := User."User Name";
    end;
    ```

    I have tried it right now, but it doesn't work for me. Even when I change the user in Scheduled Task OnBeforeInsert, it shouts the error :-(
    I am not sure if it can have an impact on the result, but I am writing the code in the "good old" C/AL...
  • MariocvMariocv Posts: 5Member
    A solution of my problem found (thanks to MS support guys): Server configuration now has a setting key EnableUserConsistencyValidationOnTasks, which is currently set to True by default. If it is set to False, it is possible to change the user in the Scheduled Task table. Works perfectly for the BC13 CU08 (I did not test it in NAV2017, I will be glad if someone will and tell me results).
Sign In or Register to comment.