StartSession Method

Starts a session without a UI and runs the specified codeunit.

Syntax

[Ok := ]  Session.StartSession(var SessionId: Integer, CodeunitId: Integer [, Company: String] [, var Record: Record])

Note

This method can be invoked without specifying the data type name.

Parameters

SessionId
 Type: Integer
The ID of the new session that is started. The ID is assigned to the SessionID variable after the session is started successfully. This parameter is passed by reference to the method.

CodeunitId
 Type: Integer
The ID of the codeunit to run when the session is started.

Company
 Type: String
The company in which to start the session. By default, the session is started in the same company as the calling session.

Record
 Type: Record
A record that is passed to the OnRun trigger of the codeunit that runs when the session is started. This parameter is optional.

Return Value

Ok
 Type: Boolean
true if the operation was successful; otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.

Remarks

The session is started on the same Dynamics 365 Business Central service instance from which the method is called. The session that is started is a background session and therefore has no UI. The session executes using the same user credentials as the calling AL code.

The following table describes how dialog boxes are handled in a background session, which has no UI.

Method that creates the dialog box Behavior
CONFIRM Method (Dialog) - Dialog box is suppressed.
- The following error occurs on the Dynamics 365 Business Central service instance: Dynamics 365 Business Central service attempted to issue a client callback to show a confirmation dialog box.
ERROR Method (Dialog) - Dialog box is suppressed.
- AL code execution ends.
- The error is logged to the event log of the Dynamics 365 Business Central service instance.
- The error is added to the Comments field of the Session Event table.
MESSAGE Method (Dialog) - Dialog box is suppressed.
- The message is recorded in the event log of the computer that is running Dynamics 365 Business Central service instance. The log entry has type Information and includes the context of the message.
OPEN Method (Dialog) - Dialog box is suppressed.
- Dialog box text is not displayed or logged.

Each background session has the same impact on resources as a regular user session. In addition, it takes time and resources to start each background session. Therefore, we recommend that you consider when and how you use background sessions. For example, do not use background sessions for small tasks that occur often because the cost of starting the session for each tasks is high.

Example

In this example, the Cache Stress Test codeunit is a custom codeunit.

var
    CacheStressTestRec: Record Customer;
    SessionID: Integer;
    OK: Boolean;
begin  
    OK := STARTSESSION(SessionId, CODEUNIT::"Cache Stress Test", COMPANYNAME, CacheStressTestRec);  
    if OK then  
      STOPSESSION(SessionId, 'Logoff cache stress test session')  
    else  
      ERROR('The session was not started successfully.');  
end;

See Also

Session Data Type
Getting Started with AL
Developing Extensions