Text constants

Text constants in C/AL are declared the same way as variables: through the C/AL Locals or C/AL Globals editors. In most cases, constants are declared as globals. It is good practice to avoid global variables when a local one is sufficient, but this is not the case with constants. A global variable often causes specific bugs; since it is available in any part of the code, and any function can change its value, it becomes very difficult to track the value of the variable while the volume of the code grows. This is not a problem with constants, because once assigned, the value of a constant cannot be changed.

With this in mind, in all code samples where text constants are involved, we will declare them as globals. A typical application of text constants is UI messages: process information or error notifications. And having a constant in the global scope is handy when you need to show the same message in different parts of the application.

To demonstrate the application of text constants, we will write a function that checks for overdue sales invoices and shows an appropriate message, depending on the result. This function is going to be placed in a new codeunit, so first of all, create one.

Declaring text constants is very similar to declaring functions or variables. To declare a global text constant, follow these steps:

  1. In the C/SIDE code editor, select the View | Globals menu action.
  2. Select the Text Constants tab in the C/AL Globals form.
  3. Enter the name of the constant in the Name field, and its value in the ConstValue field.

For this example, declare two constants in the codeunit you just created:

OverdueCountMsg : TextConst 'ENU=There are %1 overdue invoices on %2';
NoOverdueInvoicesMsg : TextConst 'ENU=All customer invoices are paid';

Besides two text constants, the following local variables should be declared in the function:

CustLedgerEntry : Record 21;
OverdueInvoicesCount : Integer;

Here is how we use the constants:

OnRun()
CustLedgerEntry.SETRANGE("Document Type",CustLedgerEntry."Document Type"::Invoice);
CustLedgerEntry.SETFILTER("Due Date",'<%1',WORKDATE);
CustLedgerEntry.SETRANGE(Open,TRUE);
OverdueInvoicesCount := CustLedgerEntry.COUNT;
IF OverdueInvoicesCount > 0 THEN
MESSAGE(OverdueCountMsg,OverdueInvoicesCount,WORKDATE)
ELSE
MESSAGE(NoOverdueInvoicesMsg);

If there are no unpaid overdue invoices on the WORKDATE, this function displays a message box with text from the NoOverdueInvoicesMsg text constant. Otherwise, the other constant, OverdueCountMsgwill be used.

Typically, the text constant is a part of a dialog text displayed to the user with one of the functions MESSAGE, ERROR, CONFIRM, or STRMENU. Note that the OverdueCountMsg text constant contains placeholders that should be replaced with actual values at runtime. The MESSAGE function, as well as ERROR and CONFIRM, do this substitution and do not require the string parameter to be formatted with STRSUBSTNO.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset