Hi, Readers.
Today I would like to briefly talk about a point that developers who are new to Business Central should pay special attention to, the difference between Record.Validate() Method and assignment statement (:=).
First let’s take a brief look at assignment statements (:=), assignment statements assign a value to a variable. The value that you assign to the variable is an AL expression. It can be a constant or a variable, or it can consist of multiple elements of AL expressions. If you use a method call as the value to assign to a variable in an assignment statement, then the value that is assigned is the return value of the method.
You use the “:=” operator for assignment statements.
For example,
Count := 1;
Amount := 2 * Price;
When you work with records, addressing is created as record name, dot (period), and field name: <Record>.<Field>
For exmple,
CustomerRec."No." := '1234';
CustomerRec.Name := 'Windy City Solutions';
CustomerRec."Phone No." := '555-444-333';
CustomerRec.Address := '1241 Druid Avenue';
CustomerRec.City := 'Windy City';
More details: AL simple statements
Next is the Record.Validate() Method. This method cannot assign values to variables, but it can assign values to record fields.
Record.Validate(Any [, Any]) Method: Calls the OnValidate trigger for the field that you specify.
Syntax: Record.Validate(Field: Any [, NewValue: Any])
[Optional] NewValue: The value to insert into Field.
codeunit 50222 ZYTestCodeunit
{
local procedure MyProcedure()
var
CustomerRec: Record Customer;
begin
CustomerRec.Init();
CustomerRec.Validate("No.", '1234');
CustomerRec.Validate(Name, 'Windy City Solutions');
CustomerRec.Validate("Phone No.", '555-444-333');
CustomerRec.Validate(Address, '1241 Druid Avenue');
CustomerRec.Validate(City, 'Windy City');
CustomerRec.Insert();
end;
}
So when you work with records, what is the difference between Record.Validate() Method and assignment statement?
There is a very important difference here, whether the OnValidate (Field) Trigger defined in the table is run.
OnValidate (Field) Trigger: Runs when user input is validated.
What does it mean? Let’s take a look at a specific example. When we create a sales order, we first enter the “Customer No.”.
After entering the “Customer No.”, you can find that many fields on the order are automatically filled. And there are some additional processing, such as displaying notifications.
These processes are handled in the OnValidate (Field) Trigger of the table field. (A small amount of processing may also be done in the OnValidate (Page Fields) Trigger)
So if we only use the assignment statement to assign a value to “Customer No.”, none of these codes will run. For example, I created two actions that create a new order in different ways.
Create Order By Assignment Statement: Only the “Customer No.” field is assigned a value.
Create Order By Validate Method: Just like the operation on the UI, since the code in the OnValidate (Field) Trigger is run, the values of other related fields are also automatically filled in.
Test video:
This is especially important when there are some background processing fields. For example, if we try to insert sales line through assignment statement, the following error may occur because OnValidate (Field) Trigger is not executed.
Gen. Bus. Posting Group must have a value in Sales Line: Document Type=Order, Document No.=101029, Line No.=0. It cannot be zero or empty.
Another example, the (Base) fields on the Sales Line (37) table are automatically updated when the user enters or updates a quantity in the table.
If we only use assignment statement to assign values, the following error will occur when posting.
You cannot post these lines because you have not entered a quantity on one or more of the lines.
Although the quantity is shown on the page……
In addition, when there is error handling in OnValidate (Field) Trigger, the assignment statement will not be triggered. For example, Item Tracking Code on the Item Card page
You cannot change Item Tracking Code because there are one or more ledger entries for this item.
Update Item Tracking Code By Validate Method: Same error as when modifying on UI.
Update Item Tracking Code By Assignment Statement: The data will be updated without any error message.
Test video:
PS: Dynamics 365 Business Central: Can we skip/ignore field validation errors?
Therefore, there are certain risks in assigning values to record fields using only assignment statements. But do we have to use Record.Validate() Method 100% of the time? No, that’s not the case.
For example, the following field does not contain OnValidate (Field) Trigger.
At this time, the two methods are the same and there is no difference.
In addition, when using an assignment statement and then executing Record.Validate(Field: Any) it is the same as assigning a value directly using Record.Validate(Field: Any [, NewValue: Any]).
Well, that’s all. This is a problem that BC/NAV developers will definitely encounter at the beginning, of course, I am the same. I hope this post can help you understand.
PS: The OnValidate trigger is also a field trigger at the page level. For more information, see OnValidate (Page Fields) Trigger. If both the table field and page field triggers are defined, then the OnValidate trigger on the table field is run before the OnValidate trigger on the page field.
END
Hope this will help.
Thanks for reading.
ZHU
コメント