Options

Table "Session Event" - OnAfterInsertEvent never fired?

MarHanMarHan Member Posts: 34
Hey guys,

has anyone of you ever managed to subscribe successfully to the "OnAfterInsertEvent" of Table "Session Event"?

I'm doing this:

[EventSubscriber(ObjectType::Table, Database::"Session Event", 'OnAfterInsertEvent', '', false, false)]
local procedure User_Session_Event_Inserted_Handler(var Rec: Record "Session Event")
begin
if Rec."Client Type" <> Rec."Client Type"::"Web Client" then
exit;
// Do some stuff here when a User logged in via Modern Client
end;

But neither my breakpoints are hit inside that handler nor my debugging textfile gets written. Am a bit confused now.

Regards

Markus



Answers

  • Options
    Developer101Developer101 Member Posts: 529
    Is this Onprem version of BC?
    United Kingdom
  • Options
    MarHanMarHan Member Posts: 34
    Is this Onprem version of BC?

    Yes it is, running in a dev container, build from BC container helper.

    I also fiddled a bit around with

    - table 2000000110 "Active Session" > OnAfterInsertEvent: Seems to works neither
    - codeunit 40 LogInManagement > OnAfterLogInEnd: Seems to be deprecated
    - codeunit 150 "System Initialization" > OnAfterLogin: No information about the user

    Could it be that some of the session related stuff is done in "a deeper layer" somehow between SQL and BC server and not in that place where other events get fired?
  • Options
    Developer101Developer101 Member Posts: 529
    What is requirement here? What is an end goal?
    United Kingdom
  • Options
    MarHanMarHan Member Posts: 34
    As shown in my code: // Do some stuff here when a User logged in via Modern Client
    I want to log a users last logon and logoff timestamps in my app for example.
  • Options
    ShaiHuludShaiHulud Member Posts: 228
    Why not use standard functionality? When "Register Time" is enabled on User Setup, a record is created in "User Time Register" when they first log in on a given day. The record is also updated with the number of minutes they are online. For each day, you can track $SystemCreatedAt and last $SystemModifiedAt.

    You're not going to get anywhere (good) trying to subsribe directly to a system table.

    As an alternative, you can use Codeunit 40 LogInManagement's event OnAfterCompanyOpen to get login events.
  • Options
    MarHanMarHan Member Posts: 34
    Thanks for your advice, but the time register stuff must be explicitly activated and I cannot go for sure that it is. Codeunits 40's "OnAfterCompanyOpen" is marked for removal with 20.0. We're currently merging in BC21.4 so could be not the best idea to build new stuff on that now.
  • Options
    ShaiHuludShaiHulud Member Posts: 228
    edited 2023-02-17
    When an event, function, table, etc. is marked for removal, in most cases Microsoft includes pointer to a replacement object:
    [Obsolete('Replaced with OnAfterLogin in codeunit "System Initialization"', '20.0')]
    [IntegrationEvent(false, false)]
        local procedure OnBeforeCompanyOpen()
        begin
        end;
    
  • Options
    MarHanMarHan Member Posts: 34
    The "OnAfterLogin" event in "codeunit 150 System Initialization" unfortunately does not provide access or information about the user that currently has logged in. It seems only to be a "neutral" hook to add your own initialization stuff after the login. Or am I missunderstanding something?
  • Options
    ShaiHuludShaiHulud Member Posts: 228
    You can always get current user with built-in system function UserID() or UserSecurityID(), depending on which one you need.
  • Options
    MarHanMarHan Member Posts: 34
    *Ashamed* you're so right - I totally forgot about those system functions 🙈. I managed to subscribe to the "OnAfterLogin"-Event of codeunit "System Initialization". Do you know something similar for logoff? Because I also need the timestamp when a user has finished his work and is no longer active inside the system.
  • Options
    ShaiHuludShaiHulud Member Posts: 228
    Users typically don't actively sign off, and when they just close the browser, the session remains open for a little while. Depending on how things go, even Active Session record might remain even if the session itself is gone. Your best bet would be to enable Time Registers and treat modifications to it as "Last interaction" or something
Sign In or Register to comment.