Skip to main content

Notifications

Announcements

Dynamics 365 Community / Blogs / Jesús Almaraz blog / Returning complex types fro...

Returning complex types from AL functions.

I notice that AL lets us to return some complex types in functions. This function bellow saves recordref content in a Json object and returns it:
procedure GetJSonFromGeneric(var GenericRec: RecordRef) JsonGeneric: JsonObject
var
    i: Integer;
    GenericField: FieldRef;
begin
    for i := 1 to GenericRec.FieldCount() do begin
        GenericField := GenericRec.FieldIndex(i);
        JsonGeneric.Add(GenericField.Name,
            format(GenericField.Value));
    end;
end;
 
The amazing stuff begins when we consume this function:
    trigger OnRun()
    var
        Customer: record Customer;
        GenericRec: RecordRef;
        BodyText: Text;
    begin
        Customer.FindFirst();
        GenericRec.GetTable(Customer);
        GetJSonFromGeneric(GenericRec).WriteTo(BodyText);
        message(BodyText);
    end;
The function became an object itself with all methods and properties of Json object!!!

The subtle difference. Code highlights.

First, we declare the function with a complex type return:
procedure GetJSonFromGeneric(var GenericRec: RecordRef) JsonGeneric: JsonObject
Later we consume this function, handling it as an object:
GetJSonFromGeneric(GenericRec).WriteTo(BodyText);
To do the same in old C/AL we must send a parameter with Var mark, and then can save changes in this parameter:
   procedure GetJSonFromGenericWithPar(var GenericRec: RecordRef;var JsonGeneric: JsonObject)

You may think who cares?.

But it´s more important than you might think. I can give you two reasons:
  • Input/output parameters (marked with var) are generally avoided in all programming paradigms, functional and OOP as well. Modifying a parameter instead returning a new object could cause many errors hard to fix. Parameters by reference can get some state behind that turns code unsafe.
  • All the signs are that we are going to get new features in AL functions, comparable to JavaScript functions. The future could get us callbacks or the possibility of returns a record.

Objects that can be returned.

Not all the objects are valid return types. My strongest desire is return a record, the absolute king of Dynamics objects. But not possible:
ComplexType.png
But despite this, there are a lot of interesting objects to return:
local procedure MyProcedure1(): XmlDocument
begin   
end;
local procedure MyProcedure2(): HttpHeaders
begin
   
end;
I preferred to use Jason Object in the example because is another very important object: a JavaScript complete object definition.

Comments

*This post is locked for comments

  • Jalmaraz Profile Picture Jalmaraz 667
    Posted at

    Good point Slawek! That way you don´t have to do out of the function the Gettable to recordref and you can do it inside the function, making the call easier.

  • Community Member Profile Picture Community Member Microsoft Employee
    Posted at

    That is realy cool! Thanks for sharing!

    NB I would use Variant as the input to function to make it even simpler to use.