Temporary tables, created in server memory instead of the database, are widely used to store the interim results of complex calculations. Temp tables can be used as buffers for data presented to the user, when the dataset cannot be obtained directly from a table.
An example of such dataset can be a list of certificates issued or prolonged in the past year, that have not been revoked and not yet expired. While it is possible to construct a SQL query that will collect this data from a join of several tables, C/AL code for this task will be a little more intricate and requires a temporary storage for the records.
In this recipe, a temporary table is used to store a list of certificates collected in a C/AL function. The table will be used as a data source for a page to present the data to the user.
CollectProlongedNotRevokedCertificates
. To allow the function to be called from other objects, access its properties and set Local = No.TempItemCertificate
: Record "Item Certificate"
.
Name |
DataType |
Subtype |
|
Record |
Item Certificate Action |
|
Record |
Item Certificate |
TempItemCertificate.RESET; TempItemCertificate.DELETEALL;
WITH ItemCertificateAction DO BEGIN SETRANGE("Action Date", CALCDATE('<-1Y>',WORKDATE),WORKDATE); SETFILTER("Expiration Date",'>%1',WORKDATE); IF FINDSET THEN REPEAT IF NOT TempItemCertificate.GET( ItemCertificateAction."Certificate No.") THEN IF NOT ItemCertificate.IsCertificateRevoked( "Certificate No.") THEN BEGIN ItemCertificate.GET( ItemCertificateAction."Certificate No."); TempItemCertificate := ItemCertificate; TempItemCertificate.INSERT; END; UNTIL NEXT = 0; END;
IsCertificateRevoked
function, called in the code, should be declared in the table 50010 Item Certificate
. Create the function, change its return type to Boolean
, and add a local variable:
Name |
DataType |
Subtype |
|
Record |
Item Certificate Action |
IsCertificateRevoked
calls the function CertificateActionExists
to find out if there is a Revoked
action registered for the certificate:EXIT(CertificateActionExists( ItemCertificateAction."Action Type"::Revoked));
ViewActive
.OnAction
trigger:ItemCertificateMgt.CollectProlongedNotRevokedCertificates( TempItemCertificate); PAGE.RUN(PAGE::"Certificates List",TempItemCertificate);
TempItemCertificate
variable should be declared in the list of local variables in the OnAction
trigger:
Name |
DataType |
Subtype |
|
Record |
Item Certificate |
TempItemCertificate
and set Temporary to Yes.The CollectProlongedNotRevokedCertificates
function declared in codeunit Item Certificate Mgt., will analyze item certificates within the given period range and insert the resulting dataset into the record parameter TempItemCertificate
.
The function's parameter is declared as a record variable, but the variable passed to it from the action trigger in Step 11 has the TEMPORARY
attribute. This property does not change the way the table is accessed in the C/AL code, but data manipulation functions, such as FINDSET
, INSERT
, MODIFY
, and so on, called on this variable won't issue SQL queries. Instead, records will be retrieved, inserted, and modified in a recordset stored in memory, as we will see in the code sample.
First thing the function does, is remove all filters from the parameter variable and delete all data it may contain. We simply call DELETEALL
on the Item Certificate record, this statement won't affect records in the database.
After clearing the temporary buffer, we begin collecting data into it. For the sake of simplicity of the example, date range is limited to the period from work date - 1 year to the work date:
SETRANGE("Action Date",CALCDATE('<-1Y>',WORKDATE),WORKDATE);
This will filter out all certificates with no actions in the past year.
Next, we leave only certificates with an expiration date later than the application work date:
SETFILTER("Expiration Date",'>%1',WORKDATE);
Code will run a loop on all certificates' actions and rule out revoked certificates, inserting all active ones into the buffer.
Finally, from Step 9 to Step 13 we create a page action that will execute the function. First line in the action trigger calls the function CollectProlongedNotRevokedCertificates
passing a temporary variable into which the records will be inserted as shown:
ItemCertificateMgt.CollectProlongedNotRevokedCertificates( TempItemCertificate);
In the next line, PAGE.RUN
will open the page Certificates List with the temporary buffer as a source. The page will take the data to display from the buffer instead of the database:
PAGE.RUN(PAGE::"Certificates List",TempItemCertificate);
It is possible to build a page based on a temporary table. The SourceTableTemporary page property tells that the page won't select data from the database, but will read a buffer that must be filled on page initialization.
For details, see the Simpifying data access with queries recipe where we will fill the temporary page source using a query object.
3.145.16.23