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 Member Posts: 724
    I have done the same
    Follow me on my blog juhl.blog
  • nicolassaleronnicolassaleron Member Posts: 11
    Thank you, I will implement the trick :)
  • zeonzeon Member Posts: 130
    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 Member Posts: 211
    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 Member Posts: 211
    Bump, anyone knows? Please :)
    Martin Bokůvka, AxiomProvis
  • MariocvMariocv Member Posts: 6
    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 Member Posts: 724
    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 Member Posts: 6
    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 Member Posts: 249
    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 Member Posts: 19
    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 Member Posts: 6
    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 Member Posts: 6
    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).
  • Rafed_RFRafed_RF Member Posts: 2
    Hi Mariocv, Where can I finde this fix? Is it in customersource or partnersouce?
  • MariocvMariocv Member Posts: 6
    edited 2019-11-14
    Rafed_RF wrote: »
    Hi Mariocv, Where can I finde this fix? Is it in customersource or partnersouce?

    Actually, I don't know whether it is possible to find it anywhere. I discussed it with an MS support specialist and he advised me that this server property exists and I can change it...
  • jbridgukjbridguk Member Posts: 5
    Mariocv wrote: »
    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).

    I just had this issue on 2017 CU30 and added the key manually to the CustomSettings.config file for the NST, restarted and it solved the problem
  • I just applied the modification on Nav 2018 CU27 and now it works perfectly!
    Thank you very much
  • SunsetSunset Member Posts: 200
    Does not work on 2017 CU 6.
    Server administration module will tell you that "The configuration setting 'EnableUserConsistencyValidationOnTasks' is not valid for Microsoft Dynamics NAV Server."
    Don't just take my word for it, test it yourself
Sign In or Register to comment.