Creating a function for our Factbox

For this application, we want our FactBox to display information relating to the specific Radio Show we are scheduling. The information to be displayed includes the five show segment requirements and the status of fulfillment (counts) of those requirements by the data entered to date. The requirements come from the Playlist Header fields: PSAs Required, Ads Required, News Required, Sports Required, and Weather Required. The counts come from summing up data in the Playline Line records for a show. We can use the Playlist Header fields' PSA Count and Ad Count for those two counts. These counts can be obtained through the FlowField property definitions we defined earlier for these two fields.

For the other three counts, we must read through the Playlist Lines and sum up each of the counts. To accomplish that, we'll create a Function that we can call from the FactBox page. Since our new function is local to the Playlist Header and Playlist Lines tables, we will define the function in the Playlist Header (table 50002).

The first step in defining the function is to enter its name in the Functions tab of the Globals screen for the object. Open the object, Table 50002, by clicking on Table | Design, then click on View | C/AL Globals, and then click on the Functions tab. Enter the name of the new function, access its Properties (Properties icon or Shift + F4), and set the Local property to No so the function can be called from the Factbox Page:

Then, after clicking on the Locals button and displaying the Parameters tab, we can enter the parameters we want to pass to the function. In this case, we want the parameter to be passed by value, not by reference, so we do not check the Var checkbox on the Parameter line. For more information about parameter passing, look at the Help sections, C/AL Function Calls and How to: Add a Function to a Codeunit, as well as the Create a Function section in Chapter 6, Introduction to C/SIDE and C/AL. The name of the Parameter is local to the function.

Since we want a single function that will serve to count Playlist Lines that are News, Weather, or Sports, the parameter we pass it in will be an Option code of News, Weather, or Sports. The Option Subtype sequence must be the same as those in the Type field in the Playlist Lines table; otherwise, we will have Options mismatching:

The return value we want back from this function is an Integer count. Again, the variable name is local. We define the Return Value on the appropriate tab, as shown in the following screenshot:

The logic of our counting process is described as follows:

  1. Filter the Playlist Line table for the Radio Show we are scheduling and for segment entries (Playlist Line) that represent shows.
  2. Look up the Radio Show record for each of those records.
  3. Using the data from the Radio Show record, look up the Radio Show Type code.
  4. In the Radio Show Type record, use the News, Weather, or Sports fields to determine which Playlist Line counter should be incremented.

Based on this logic, we must have Local Variables defined for the three tables: PlaylistLine, RadioShow, and RadioShowType. The following screenshot shows those Local Variables:

Translating our pseudocode into executable C/AL, our function looks like the following screenshot:

In the process of writing this code, we notice another design flaw. We defined the type of Radio Show with a code that allows users to enter their choice of text strings. We just wrote code that depends on the contents of that text string being specific values. A better design would be to have the critical field be an Option data type so we could depend on the choices being members of a predefined set. However, the Code field is our Primary Key field, and we probably shouldn't use an Option field as the Primary Key. We will continue with our example with the design as is, but you should consider how to improve it. Making that improvement will be excellent practice with C/AL for you.

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

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