Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

How to calculate 1 field based on boolean of 4 fields ?

Posted on by 20

Hi everyone,

I need some help to show a specific value depending on the value from 4 boolean fields ( 401 to 404 )

Part of my table :

        // Allergenes
        field(401; "Lait"; Boolean) { }
        field(402; "Fruits coque"; Boolean) { }
        field(403; "Céleri"; Boolean) { }
        field(404; "So2"; Boolean) { }
        field (510; "Allergen"; Text [250]) { }
Calcformula ?
When 401,402,403 and 404 are false : i want that the field "allergen" value =  "This product does not contain allergen"
 
When for an exemple the field 401 and 403 are true : i want this value in the field "Allergen" : "this product contains "Lait" and "Céleri"
If 402 are true : This product contains "Fruits coque"
I suppose that i need to write a formula on my Card page like this ( but i have absolutely no idea on how to write this formula...)
    procedure alllergene()
        begin
        if (rec.Lait=false) and (rec."Fruits coque"=false) and (rec."Céleri"=false) and (rec.So2=false)
        then rec.Allergenes := 'Ce produit ne contient pas d''allergenes'
        else
        if (rec.lait=true) or (rec."Fruits coque"=true) or (rec."Céleri"=true) or (Rec.So2=true)
        then rec.Allergenes := 'Ce produit contient les allergenes suivants :'(here i need the list of TRUE)
    end;
Its works when all are false, but when one are true, i need to write the list off wich are true...
Probably can be done with filter ? concatenate ? others ?   I have no idea how can it be done....
Thanks in advance for your help.
  • Neodep Profile Picture
    Neodep 20 on at
    RE: How to calculate 1 field based on boolean of 4 fields ?

    Many thanks Zhu, just tested now and its perfect !

  • Verified answer
    YUN ZHU Profile Picture
    YUN ZHU 73,647 Super User 2024 Season 2 on at
    RE: How to calculate 1 field based on boolean of 4 fields ?

    Sorry I didn't understand the question at all before. Now I understand.

    You can do something like below. You can define some functions to make it look more concise. In addition, for the convenience of testing, I only considered that when the page is modified, you can also add it to the trigger of the table.

    page 50010 "Lab Book"
    {
        ApplicationArea = All;
        UsageCategory = Administration;
        Caption = 'Lab Book';
        PageType = Card;
        SourceTable = "Lab Book";
    
        layout
        {
            area(content)
            {
                group(General)
                {
                    Caption = 'General';
    
                    field("No."; Rec."No.")
                    {
                        ToolTip = 'Specifies the value of the No. field.';
                    }
                    field(Lait; Rec.Lait)
                    {
                        ToolTip = 'Specifies the value of the Lait field.';
    
                        trigger OnValidate()
                        begin
                            if Rec.Lait then
                                if Contains = '' then
                                    Contains := Rec.FieldName(Lait)
                                else
                                    Contains := Contains   ' and '   Rec.FieldName(Lait);
                        end;
                    }
                    field("Fruits coque"; Rec."Fruits coque")
                    {
                        ToolTip = 'Specifies the value of the Fruits coque field.';
                    }
                    field("Céleri"; Rec."Céleri")
                    {
                        ToolTip = 'Specifies the value of the Céleri field.';
                    }
                    field(So2; Rec.So2)
                    {
                        ToolTip = 'Specifies the value of the So2 field.';
                    }
                    field(Allergen; Rec.Allergen)
                    {
                        ToolTip = 'Specifies the value of the Allergen field.';
                        MultiLine = true;
                    }
                }
            }
        }
        var
            Contains: Text[100];
    
        trigger OnModifyRecord(): Boolean
        begin
            if (not Rec.Lait) and (not Rec."Fruits coque") and (not Rec."Céleri") and (not Rec.So2) then begin
                Rec.Allergen := 'This product does not contain allergen';
            end else begin
                Contains := '';
                if Rec.Lait then
                    Contains := Rec.FieldName(Lait);
                if Rec."Fruits coque" then
                    if Contains = '' then
                        Contains := Rec.FieldName("Fruits coque")
                    else
                        Contains := Contains   ' and '   Rec.FieldName("Fruits coque");
                if Rec."Céleri" then
                    if Contains = '' then
                        Contains := Rec.FieldName(Céleri)
                    else
                        Contains := Contains   ' and '   Rec.FieldName(Céleri);
                if Rec.So2 then
                    if Contains = '' then
                        Contains := Rec.FieldName(So2)
                    else
                        Contains := Contains   ' and '   Rec.FieldName(So2);
                Rec.Allergen := StrSubstNo('This product contains %1', Contains);
            end;
        end;
    }

    Test Video:

    [View:/cfs-file/__key/communityserver-discussions-components-files/758/Test04251650.mp4:1024:768

    Hope this helps.

    Thanks.

    ZHU

  • Neodep Profile Picture
    Neodep 20 on at
    RE: How to calculate 1 field based on boolean of 4 fields ?

    Thanks Zhu for your help.

    So i suppose that i need to create all combination in test trigger, there is no way to just create a var that concatenate the value when the field are "on"

    btw : thanks for your website, its helped me a lot to create my dev.

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,647 Super User 2024 Season 2 on at
    RE: How to calculate 1 field based on boolean of 4 fields ?

    Hi, Just a simple example. But I think your logic is not rigorous enough....

    page 50010 "Lab Book"
    {
        ApplicationArea = All;
        UsageCategory = Administration;
        Caption = 'Lab Book';
        PageType = Card;
        SourceTable = "Lab Book";
    
        layout
        {
            area(content)
            {
                group(General)
                {
                    Caption = 'General';
    
                    field("No."; Rec."No.")
                    {
                        ToolTip = 'Specifies the value of the No. field.';
                    }
                    field(Lait; Rec.Lait)
                    {
                        ToolTip = 'Specifies the value of the Lait field.';
                    }
                    field("Fruits coque"; Rec."Fruits coque")
                    {
                        ToolTip = 'Specifies the value of the Fruits coque field.';
                    }
                    field("Céleri"; Rec."Céleri")
                    {
                        ToolTip = 'Specifies the value of the Céleri field.';
                    }
                    field(So2; Rec.So2)
                    {
                        ToolTip = 'Specifies the value of the So2 field.';
                    }
                    field(Allergen; Rec.Allergen)
                    {
                        ToolTip = 'Specifies the value of the Allergen field.';
                    }
                }
            }
        }
    
        trigger OnModifyRecord(): Boolean
        begin
            if (not Rec.Lait) and (not Rec."Fruits coque") and (not Rec."Céleri") and (not Rec.So2) then begin
                Rec.Allergen := 'This product does not contain allergen';
                exit;
            end;
    
            if (Rec.Lait) and (Rec."Céleri") then begin
                Rec.Allergen := 'This product contains "Lait" and "Céleri';
                exit;
            end;
    
            if Rec."Fruits coque" then begin
                Rec.Allergen := 'This product contains "Fruits coque"';
                exit;
            end;
        end;
    }
    

    Test video:

    [View:/cfs-file/__key/communityserver-discussions-components-files/758/Demo04250931.mp4:1024:768

    Hope this helps.

    Thanks.

    ZHU

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans