Search code examples
dynamics-business-centraldynamics-albusinesscentral

If then statement inside a page field? Business central AL


Im trying to create page for giving my to customers discounts on my products, but for a short period of time. I have created a field "start date" and "end date" of this promotion. Next thing i want to do, is to validate the input of the date.

By "Validate" i mean, that start date cannot be greater then end date. I decided to try preventing from writing into "end date" field unless there is a value in "Start date" field, but i ran into some syntax errors... Can you help me with that? Here is the logic i want to write for my page:

field("Starting Date"; Rec."Starting Date")
            {
                ApplicationArea = All;
            }

            field("End Date"; Rec."End Date")
            {
                ApplicationArea = All;
                if Rec."Starting Date" = '' then 
                    Editable = false;
            }

Here is the full page code i have so far for better understanding:

    page 95012 "ArKe Provision Subform"
{
    Caption = 'ArKe Provision Subform';
    PageType = ListPart;
    ApplicationArea = All;
    UsageCategory = Administration;
    SourceTable = ArKeProvisionLine;
    SourceTableView = sorting(Status, "Line No.") order(descending);

    layout
    {
        area(Content)
        {
            repeater(ProvisionLineRepeater)
            {
                field(Status; Rec.Status)
                {
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        CurrPage.Update();
                    end;
                }

                field("Customer Type"; Rec."Customer Type")
                {
                    ApplicationArea = All;
                }

                field("Product Type"; Rec."Product Type")
                {
                    ApplicationArea = All;
                }

                field("Starting Date"; Rec."Starting Date")
                {
                    ApplicationArea = All;
                }

                field("End Date"; Rec."End Date")
                {
                    ApplicationArea = All;
                    if Rec."Starting Date" = '' then begin
                        Editable = false;
                    end
                }

                field("Provision %"; Rec."Provision %")
                {
                    ApplicationArea = All;
                }

                field("Line No."; Rec."Line No.")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
            }

        }
    }
}

Solution

  • You do not have to format "End Date" or "Starting Date", you can just compare them to 0D (or 0DT if their DataType is DateTime) Like This:

    field("Starting Date"; Rec."Starting Date")
            {
                ApplicationArea = All;
    
                trigger OnValidate()
                begin
                    if Rec."End Date" <> 0D then begin//0DT in case field is DateTime
                        if rec."End Date" < rec."Starting Date" then begin
                            Error('Starting date greater then end date!');
                        end;
                    end;
                end;
            }
    
    
            field("End Date"; Rec."End Date")
            {
                ApplicationArea = All;
                trigger OnValidate()
                begin
                    if Rec."Starting Date" <> 0D then begin//0DT in case field is DateTime
                        if rec."Starting Date" > rec."End Date" then begin
                            Error('Starting date greater then end date!');
                        end;
                    end;
                end;
            }