Pre-Execute Codeunit
The Pre Execute Codeunit provides a hook for running custom Business Central logic immediately before Business Portals reads and serializes a dataset table. This makes it possible to prepare, synchronize, validate, or log information before the dataset is returned to the portal.
The codeunit is configured separately for each dataset table. Because the hook is part of dataset loading, the same approach can be used with product lists, jobs, invoices, and other Business Central tables.
Sample Result
The sample writes an information entry to the existing Business Portals log. It does not modify the records displayed by the dataset.
The log entry includes the dataset code, dataset table code and type, DPS username, company, and selected parameter. To view it in Business Central, search for Business Portals – Log.
Each initial dataset load or refresh can create another entry. The Clear History action on the log page can be used to remove entries that are no longer required.
Info
The logging behavior is intended as a simple demonstration. Replace the WriteLogEntry procedure with the preparation logic required by the implementation, such as refreshing a staging table or synchronizing data.
Code Example
codeunit 5138173 "SIM_DPS Sample Pre Execute CU"
{
// A pre-execute codeunit must use "SIM_DPS Temp Table" so DPS can pass the execution context in Rec.
TableNo = "SIM_DPS Temp Table";
trigger OnRun()
var
LocalParamText: Text;
begin
// DPS runs the codeunit without a parameter from the setup lookup to request its available functions.
LocalParamText := 'GETFUNCTIONS';
// During dataset execution, Rec.Param contains the function selected in "Pre Execute Codeunit Param".
if (Rec.Param <> '') then
LocalParamText := Rec.Param;
// Route the setup request or execution request to the corresponding logic.
case LocalParamText of
'Write Log Entry':
WriteLogEntry(Rec);
'GETFUNCTIONS':
// "Function List" is displayed in the lookup. Separate multiple function names with commas.
Rec."Function List" := 'Write Log Entry';
end;
end;
// Write the context supplied by DPS to the existing Business Portals log.
local procedure WriteLogEntry(ParamRecordSIMDPSTempTable: Record "SIM_DPS Temp Table")
var
LocalCodeunitSIMDPSLogMgt: Codeunit "SIM_DPS Log Mgt";
LocalDescriptionLabelMsg: Label 'The pre-execute sample codeunit was executed. Dataset Code: %1; Dataset Table Code: %2; Dataset Table Type: %3; DPS Username: %4; Company: %5; Parameter: %6', Comment = '%1 = Dataset Code, %2 = Dataset Table Code, %3 = Dataset Table Type, %4 = DPS Username, %5 = Company, %6 = Pre Execute Codeunit Parameter';
LocalSourceLabelMsg: Label 'Sample Pre Execute Codeunit';
begin
// Add an information entry that users can inspect on the "Business Portals - Log" page.
LocalCodeunitSIMDPSLogMgt.AddInformationInTheLog(
// Replace the label placeholders with the values received from the serialization process.
StrSubstNo(
LocalDescriptionLabelMsg,
ParamRecordSIMDPSTempTable."Dataset Code",
ParamRecordSIMDPSTempTable."Dataset Table Code",
ParamRecordSIMDPSTempTable."Dataset Table Type",
ParamRecordSIMDPSTempTable.Username,
ParamRecordSIMDPSTempTable.Company,
ParamRecordSIMDPSTempTable.Param),
LocalSourceLabelMsg);
end;
}