To Validate or Not to Validate: Line validation in Dynamics 365 Business Central Documents

This is a short note on how to add further validation to the sales or purchase document lines in Dynamics 365 Business Central with a small AL code extension. 

There are a number of validations operating in the background for sales and purchase lines. Sometimes you need to add to this, in this example I'll build a small app to meet the following requirement.

  • Prevent sales lines with zero value

To meet this requirement I'll use the OnAfterInsert trigger which will carry out the checks after the system already validated at the database level.


Here is the example code:

Lets see what ChatGPT has to say about this

Pretty handy!

What if we want more? I asked to validate in a different way: "Please rewrite the code to also prevent the job and job task from being blank" 

Looks good, I liked the way it also updated the error message, very handy. Would need testing of course.

The original simple .app and code can be found here https://github.com/andywingate/d365bc-validatesalesline