AL complex types

APPLIES TO: Business Central 2021 release wave 1 (v18.0) and later

With the latest version of Business Central, it's possible to return most types from procedures - both user-defined types and most built-in types.

The method in the example below, will take a name, and return the first customer record that matches the name. The signature specifies the return type at the end of the procedure declaration, and the procedure exits by returning the found customer record.

/// <summary> 
/// Get the first customer with name starting with <paramref name="Name"/> 
/// </summary> 
/// <param name="Name">Name filter</param> 
/// <returns>First customer</returns> 

procedure GetCustomerByName(Name: Text): Record Customer
var
    Customer: Record Customer;
begin
    Customer.SetFilter(Name, '@' + Name + '*');
    Customer.FindFirst();
    exit(Customer);
end;

It's also possible to use a named return value. Internally, the exit-statement as seen in the example above causes an assignment to an allocated return value. The assignment will have a small performance cost based on the type. Since the record type is treated as a value-type, it's better.

procedure GetCustomerByName(Name: Text) Customer: Record Customer
begin 
   Customer.SetFilter(Name, '@' + Name + '*'); 
   Customer.FindFirst(); 
end; 

The method GetCustomerByName() returns a Customer record. It can be used as you would expect in the following example.

// Get the first customer with name starting with 'spo' 

Customer := GetCustomerByName('spo'); 

The returned value doesn't have to be used in an assignment statement. It can be used as part of an expression like in the following example.

// Use the returned value as an expression. 

DoSomethingWithSales(GetCustomerByName('spo').GetSalesLCY()); 

It doesn't only work for user-defined types like records, codeunits, etc., but also for built-in types. For example, when using the HttpClient Data Type, it's possible to write code as illustrated below.


/// <summary> 
/// Returns a bing-ready HttpClient 
/// </summary> 
/// <returns>Bing HttpClient</returns> 
procedure GetBingClient() Result: HttpClient
begin
    Result.SetBaseAddress('https://www.bing.com');
end;

/// <summary> 
/// Get the response from a request to bing. 
/// </summary> 
/// <returns>The response message</returns> 

procedure GetBingResponse() Response: HttpResponseMessage
begin
    GetBingClient().Get('', Response)
end;

/// <summary> 
/// Get the response from www.bing.com as an html-string.  
/// </summary> 
/// <returns>string with html</returns> 
procedure GetBingHtml() Result: Text
begin
    GetBingResponse().Content().ReadAs(Result);
end;

See also

Programming in AL
AL simple statements
Directives in AL
AL essential methods
HttpClient data type