Developer Digest Vol. 500: Not-so-Random Thoughts on Simplifying AL Code DevelopmentĀ 

Developer Digest Vol. 500: Not-so-Random Thoughts on Simplifying AL Code DevelopmentĀ 

Calling Business Logic in a Codeunit from a Rec VariableĀ 

The other day a colleague and I differed in our opinion of calling business logic in a codeunit from a Record variableĀ rather than calling the business logic from some other place, a Page for instance.Ā Ā 

As he was debugging my code, he didnā€™t like that I had a Rec.DoSomething() which then calls MyCodeUnit.DoSomething(Rec), where the business logic resides.Ā 

So, I thought about it and came to some conclusions:Ā 

Encapsulation with Flexibility

  • By adding a method toā€ÆRecā€Ælikeā€ÆRec.DoSomething(), you provide a convenient way for other parts of the system to trigger the behavior directly on theā€ÆRecā€Ævariable.Ā 
  • Internally, callingā€ÆMyCodeUnit.DoSomething(Rec)ā€Ækeeps the business logic centralized and decoupled from the record itself, promoting better separation of concerns.Ā 

Reusability

  • The business logic resides in the codeunit, making it reusable across different contexts or even other record types.Ā 
  • The method on theā€ÆRecā€Æserves as a convenient wrapper, making it easy to use without requiring every caller to know about the codeunit.

Maintainability

  • Since the core logic is in the codeunit, any changes to the logic require updates only in the codeunit, reducing the risk of inconsistencies and making the system easier to maintain.Ā 
  • The method on theā€ÆRecā€Æcan remain simple, just passingā€ÆRecā€Æto the codeunit.Ā 

Testability

  • You can still easily test the logic inā€ÆMyCodeUnit.DoSomething(Rec)ā€Æindependently, which is particularly useful for unit testing.Ā 
  • The method onā€ÆRecā€Æcan be tested as part of Integration and Unit tests to ensure it correctly delegates to the codeunit.Ā 

Readability and Usability

  • The method on theā€ÆRecā€Æ(Rec.DoSomething()) makes the code more readable and easier to use from the perspective of the Rec API consumers.Ā 
  • It hides the complexity of needing to know which codeunit to call, simplifying the interface for developers.Ā 

Code ExampleĀ 

Hereā€™s how you would implement this in AL:Ā 

Somewhere in code:Ā 

Var SalesHeader: Record ā€œSales Headerā€;Ā 

BeginĀ 

Ā  SalesHeader.DoSomething();Ā 

EndĀ 

table 36 “Sales Header”Ā 

{Ā 

Ā Ā Ā  procedure DoSomething()Ā 

Ā Ā Ā  varĀ 

Ā Ā Ā Ā Ā Ā Ā  MyCodeUnit: Codeunit 50100;Ā 

Ā Ā Ā  beginĀ 

Ā Ā Ā Ā Ā Ā Ā  MyCodeUnit.DoSomething(Rec);Ā 

Ā Ā Ā  end;Ā 

}Ā 

codeunit 50100 MyCodeUnitĀ 

{Ā 

Ā Ā Ā  procedure DoSomething(Rec: Record “Sales Header”)Ā 

Ā Ā Ā  beginĀ 

Ā Ā Ā Ā Ā Ā Ā  // Business logic hereĀ 

Ā Ā Ā  end;Ā 

}Ā 

Why use this technique?

This simple example shows how this pattern can be used and is a quite common practice by AL developers. In a real customer solution, you would see dozens of examples of this pattern with much more complexity. You can see that by calling the function from the Record variable, the developer doesnā€™t need to know about the complexity that exists in the business logic or even care if a codeunit is involved.Ā 

Using this approach is often a best practice in Business Central development. It combines the strengths of encapsulation and separation of concerns, leading to a codebase that is easier to understand, test, maintain, and extend.Ā 

Donā€™t miss these upcoming events

Trending Posts

Stay Informed

Choose Your Preferences
First Name
*required
Last Name
*required
Email
*required
Subscription Options
Your Privacy is Guaranteed