top of page

Use code to clean code – Wrapper-Funktionen

Our next AL-Go template and the whole source code can be found on github. See also there our Web Request helper and ControlAddin Helper functions. https://github.com/byndit/BeyondAL


Many new functionalities in AL help us to implement formerly complex problems (e.g. parsing JSON or XML) more easily. However, such source code can still become unnecessarily complex or long.


Using the example of parsing a JSON object, we would like to illustrate how code can be effectively reduced by writing wrapper functions.


The following JSON shall serve as an example:

{ 
    "id": "7eba17ec-399e-4be8-b7a4-1aceeb155b5f", 
    "name": "Max Mustermann", 
    "address": "Musterstraße 1", 
    "postCode": "12345", 
    "city": "Musterstadt", 
    "attributes": { 
  "transfer": true, 
        "homepage": "https://www.mustermann.de" 
    }, 
    "shippingAddresses": [{ 
            "name": "Max Mustermann", 
            "address": "Musterstraße 116", 
            "postCode": "67891", 
            "city": "Musterdorf" 
        }, 
        { 
            "name": "Maxi Musterfrau", 
            "address": "Musterstraße 202", 
            "postCode": "41342", 
            "city": "Musterstadt" 
        } 
    ] 
} 

The AL code should now read all values from this JSON object and create a customer with the associated delivery-to addresses.


Without wrapper functions, the source code could look something like this:


With wrapper functions, the above source code can be shortened as follows:


Our codeunit "BYD Json Helper" provides small helper functions that cleverly package the existing functions of JsonObject, JsonArray, JsonToken and JsonValue and make them easier to use:


procedure FindValue(JObject: JsonObject; Path: Text): JsonValue 
var 
    JToken: JsonToken; 
begin 
    if JObject.SelectToken(Path, JToken) then 
        if JToken.IsValue() then 
            exit(JToken.AsValue()); 
    Error(UnableToFindJsonValueErr, Path); 
end; 
 
procedure ValAsTxt(JObject: JsonObject; Path: Text): Text 
begin 
    exit(FindValue(JObject, Path).AsText()); 
end; 
 
procedure ValAsBool(JObject: JsonObject; Path: Text): Boolean 
begin 
    exit(FindValue(JObject, Path).AsBoolean()); 
end; 
 
procedure ReadJArrayFromObj(JObject: JsonObject; Path: Text): JsonArray 
var 
    JToken: JsonToken; 
begin 
    if JObject.SelectToken(Path, JToken) then 
        if JToken.IsArray() then 
            exit(JToken.AsArray()); 
end; 

Here we make use of the "SelectToken" function to work with property names as well as with paths or to read values from the JSON structure.


This way we are able to shorten the original source code significantly, write more understandable and readable source code and save variable declarations.


Since these functions are stored in a dedicated codeunit, they can be used over and over again in different parts of the application!


Have fun!

The whole source code can be found here: https://github.com/byndit/BeyondAL


177 views0 comments
bottom of page