Dynamics 365 Business Central: SessionSettings Data Type (A complex data type)

Dynamics 365 Business Central

Hi, Readers.
Today, I would like to talk about SessionSettings Data Type in Business Central.

SessionSettings data type is a complex data type for passing user personalization settings for a client session as an object. The object contains properties that correspond to the fields in the system table 2000000073 User Personalization, including: App ID, Company, Language ID, Locale ID, Profile ID, Scope, and Time Zone.

Table 2000000073 “User Personalization”:

You can force Business Central to run the “User Personalization” table by adding the ?table=2000000073 parameter to the URL, such as in the following example: 
https://businesscentral.dynamics.com/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox?table=2000000073

Sample Data:

User SIDUser IDFull NameProfile IDApp IDScopeLanguage IDLanguageCompanyLocale IDRegionTime ZoneLicense TypeCustomization StatusRoleSystem IDCreated AtCreated ByModified AtModified By
{fe0d7bcc-ac1c-4763-8bad-2189b49c92b5}YZHU 1YUN ZHUBUSINESS MANAGER{437dbf0e-84ff-417a-965d-ed2bb9650972}Tenant1041Japanese (Japan)My Company1041Japanese (Japan)Transbaikal Standard TimeFull UserRecompilation NeededBusiness Manager{d67b3a99-00be-eb11-9f0a-002248571331}5/26/2021 5:58 PM{fe0d7bcc-ac1c-4763-8bad-2189b49c92b5}5/26/2021 5:58 PM{fe0d7bcc-ac1c-4763-8bad-2189b49c92b5}
{4e0a9bba-d205-4034-989b-cc8f172c4b11}YZHUZHU YUNBUSINESS MANAGER{437dbf0e-84ff-417a-965d-ed2bb9650972}Tenant0 My Company0 Transbaikal Standard TimeFull UserRecompilation NeededBusiness Manager{627b3793-00be-eb11-9f0a-002248571331}5/26/2021 5:58 PM{4e0a9bba-d205-4034-989b-cc8f172c4b11}5/26/2021 5:58 PM{4e0a9bba-d205-4034-989b-cc8f172c4b11}
{36cbf404-7136-419c-a469-f2d9849503cc}ADMINMOD AdministratorBUSINESS MANAGER{437dbf0e-84ff-417a-965d-ed2bb9650972}Tenant1033English (United States)My Company1033English (United States)Transbaikal Standard TimeFull UserUpdatedBusiness Manager{a87541d2-e5b1-eb11-9b52-002248571331}5/11/2021 8:17 AM{36cbf404-7136-419c-a469-f2d9849503cc}5/26/2021 11:21 AM{36cbf404-7136-419c-a469-f2d9849503cc}

You can use the AL methods of the SessionSettings data type to get, set, and send the user personalization settings for the current client session.

The following methods are available on instances of the SessionSettings data type:

Method nameDescription
Init()Populates the instance of a SessionsSettings with the current client user’s personalization properties (such as Profile ID and Company) that are stored in the database.
Company([String])Gets or sets the company property in a SessionSettings object.
LanguageId([Integer])Gets or sets the language ID property in a SessionSettings object.
LocaleId([Integer])Gets or sets the locale ID property in a SessionSettings object.
ProfileAppId([Guid])Gets or sets the ID of an extension, which provides a profile, in a SessionSettings object.
ProfileId([String])Gets or sets the profile ID property in a SessionSettings object.
ProfileSystemScope([Boolean])Gets or sets the profile scope property in a SessionSettings object.
TimeZone([String])Gets or sets the time zone property in a SessionSettings object.
RequestSessionUpdate(Boolean)Passes a SessionSettings object to the client to request a new session that uses the user personalization properties that are set in the object. The current client session is abandoned and a new session is started.

Next, let us try it.

1. Get the user personalization settings

SessionSettings.Init Method: Populates the instance of a SessionsSettings with the current client user’s personalization properties (such as Profile ID and Company) that are stored in the database.

Syntax: SessionSettings.Init()

The method gets the data from the following fields in system table 2000000073 User Personalization: App ID, Company, Language ID, Locale ID, Profile ID, Scope, and Time Zone. In the SessionSettings object, the data is stored in properties that correspond to the fields of the system table.

After you call the Init method, you can get the values in the object by calling the following methods:

Let’s see a simple example:

Test Video:

2. Set the user personalization settings

SessionSettings.RequestSessionUpdate Method: Passes a SessionSettings object to the client to request a new session that uses the user personalization properties that are set in the object. The current client session is abandoned and a new session is started.

Syntax: SessionSettings.RequestSessionUpdate(saveSettings: Boolean)

saveSettings: Boolean: Specifies whether to save the personalization properties in the SessionSettings object to the table 2000000073 User Personalization in the database for the current client user. true saves the settings; false does not. If you set this parameter to true, before sending the request to the client, the server instance will store the property values of the SessionSettings object to the corresponding fields in the table 2000000073 User Personalization. If you set this to false, when the new client session is closed, the next time the user signs in, the session will return to the previous personalization settings. This enables you to use the SessionSettings object to temporarily change the personalization settings for the current session.

Let’s see a simple example: This example uses the Init method to create a SessionSettings object that includes the current client user’s personalization settings from the database, and then uses the LanguageId method and LocaleId method to set the new language and region.

Note: The Init method is useful before calling the RequestSessionUpdate method to ensure that all properties are initialized before sending the request to the server instance to start a new client session.

Test Video:

You can refer to the above method to do a quick switch between Company, Profile ID, Time Zone, etc.

Source Code:

codeunit 50100 GetSessionInfo
{
    trigger OnRun()
    var
        MySessionSettings: SessionSettings;
        Company: Text[30];
        LanguageID: Integer;
        LocalID: Integer;
        ProfileAppID: Guid;
        ProfileID: Code[30];
        ProfileSystemScope: Boolean;
        Timezone: Text[180];
        Msg: Label 'The Company is ''%1''.\The LanguageID is ''%2''.\The LocalID is ''%3''.\The ProfileAppID is ''%4''.\The ProfileID is ''%5''.\The ProfileSystemScope is ''%6''.\The Timezone is ''%7''.\';
    begin
        MySessionSettings.Init();
        Company := MySessionSettings.Company;
        LanguageID := MySessionSettings.LanguageId;
        LocalID := MySessionSettings.LocaleId;
        ProfileAppID := MySessionSettings.ProfileAppId;
        ProfileID := MySessionSettings.ProfileId;
        ProfileSystemScope := MySessionSettings.ProfileSystemScope;
        Timezone := MySessionSettings.TimeZone;
        Message(Msg, Company, LanguageID, LocalID, ProfileAppID, ProfileID, ProfileSystemScope, Timezone);
    end;
}

codeunit 50101 SessionUpdateChangeToJapanese
{
    trigger OnRun()
    var
        MySessionSettings: SessionSettings;
    begin
        MySessionSettings.Init();
        MySessionSettings.LanguageId(1041);
        MySessionSettings.LocaleId(1041);
        MySessionSettings.RequestSessionUpdate(true);
    end;
}

codeunit 50102 SessionUpdateChangeToEnglish
{
    trigger OnRun()
    var
        MySessionSettings: SessionSettings;
    begin
        MySessionSettings.Init();
        MySessionSettings.LanguageId(1033);
        MySessionSettings.LocaleId(1033);
        MySessionSettings.RequestSessionUpdate(true);
    end;
}
pageextension 50136 BusinessManagerRCExt extends "Business Manager Role Center"
{
    actions
    {
        addafter(Action41)
        {
            group("ZY New Menu")
            {
                Caption = 'ZY New Menu';

                action("GetSessionInfo")
                {
                    ApplicationArea = Basic, Suite;
                    Caption = 'Get Session Info';
                    Image = Change;
                    RunObject = codeunit GetSessionInfo;
                }

                action("ChangeToJapanese")
                {
                    ApplicationArea = Basic, Suite;
                    Caption = 'Change to Japanese';
                    Image = Change;
                    RunObject = codeunit SessionUpdateChangeToJapanese;
                }
                action("ChangeToEnglish")
                {
                    ApplicationArea = Basic, Suite;
                    Caption = 'Change to English';
                    Image = Change;
                    RunObject = codeunit SessionUpdateChangeToEnglish;
                }
            }
        }
    }
}

Find out more about SessionSettings Data Type from Microsoft Docs.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL