Skip to content
Arthur van de Vondervoort edited this page Jan 30, 2025 · 7 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.

Validate

procedure MyProcedure(CustomerNumber: Text)
var
    Customer: Record Customer;
    CustomerNo: Code[20];
begin
    // Possible overflow assigning 'Text' to 'Code[20]'
    Customer.Validate("No.", CustomerNumber);

    // Handle possible overflow, but does NOT(!) run the validation trigger anymore of the field
    if not Evaluate(Customer."No.", CustomerNumber) then
        EmitErrorInfo(CustomerNumberToLong, CustomerNumber);

    // Handle possible overflow and run the validation trigger of the field
    if not Evaluate(Customer, CustomerNo) then
        EmitErrorInfo(CustomerNumberToLong, CustomerNumber);
    Customer.Validate("No.", CustomerNo);
end;

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.

.Get() procedure

procedure MyProcedure(OrderNumber: Code[20])
var
    SalesHeader: Record "Sales Header";
    SalesOrderPrefixTok: Label 'ORD', Locked = true;
begin
    SalesHeader.Get(Enum::"Sales Document Type"::Order, SalesOrderPrefixTok + OrderNumber); // Possible overflow assigning 'Text[23]' to 'Code[20]'.
end;
Clone this wiki locally