Skip to content
Arthur edited this page Jan 22, 2024 · 4 revisions

Do not assign a text to a target with smaller size.

The rule is an addition on the AA0139 rule to cover some more scenario's.

SetFilter with Text variable

procedure SetFilterWithMethod()
var
    Customer: Record Customer;
begin
    Customer.SetFilter("No.", '%1', PopulateFilter()); // Possible overflow assigning 'Text' to 'Code[20]'
    Customer.SetFilter("No.", PopulateFilter());
end;

procedure PopulateFilter(): Text
begin
    // Some Business Logic here
end;

When the PopulateFilter method returns a text with a length of more then 20 characters, an runtime error will occur. One of the possible solutions could be to place the PopulateFilter method directly in the filter expression parameter.

SetFilter with Label variable

procedure SetFilterWithLabel()
var
    Customer: Record Customer;
    MySearchLbl: Label '<>10000&<>20000';
begin
    Customer.SetFilter("No.", '%1', MySearchLbl); // Possible overflow assigning 'Label' to 'Code[20]'.
end;

When applying placeholders (%1, %2, ..) with a string literal text in the filter expression parameter, a runtime exception can occur when one of the placeholders has a length greater then the referring field from the first parameter.

The user will receive the following error message: The length of the string is <integer>, but it must be less than or equal to <integer> characters

In the example above the Label could, in theory, be different in runtime due to a likelihood of a possible translation of this label.

MySearchLbl: Label '<>10000&<>20000', Locked = true;
MySearchLbl: Label '<>10000&<>20000', MaxLength = 20;

Make sure it doesn't exceed more then 20 characters by setting the property Locked or MaxLength on the label.

Customer.SetFilter("No.", StrSubstNo('%1', MySearchLbl));

An other option would be to wrap this inside a Text.StrSubstNo() Method. The filter expression parameter isn't a string literal text then anymore.

Clone this wiki locally