Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

AL coding issues

(0) ShareShare
ReportReport
Posted on by 97

Hi All,

I'm having some problems (again) getting my code to work.

On my purchase invoice page, i have a column "code" which is the Code field from the Dimension Vale table.

I need that to auto populate a a table extension i created called Desc2. This is extension extends the Purchase Line table

I can select it manually, but i need it to auto populate.

Here's my code, can someone help show me where i went wrong?

AS always , thanks for your help.

Rajiv

tableextension 50134 "Desc 2" extends "Purchase Line"
{
    fields
    {
        field(50134; "Desc 2"; text[50])
        {
            DataClassification = ToBeClassified;
            Caption = 'Desc 2';
            TableRelation = "Dimension Value".Name where("Code" = field("Shortcut Dimension 1 Code"));
            ValidateTableRelation = false;
        }
    }
    local procedure OnValidate(var "Code": Record "Dimension Value")
    var
        "Desc 2": Record "Purchase Line";
    begin
        if "Code".get(Rec."Shortcut Dimension 1 Code") then begin
            "Desc 2" := "Desc 2"
        end;
    end;


}


pageextension 50134 "Desc 2 " extends "Purch. Invoice Subform"
{
    layout
    {
        addafter("Description 2")
        {
            field("Desc 2"; Rec."Desc 2")
            {
                Caption = 'Desc 2';
                ApplicationArea = all;
            }
        }
    }
}

  • rajivsewsarran Profile Picture
    rajivsewsarran 97 on at
    RE: AL coding issues

    Hi Zhu, sorry i'm only now seeing your reply.

    I'll test this and let you know.

    Thanks!

  • rajivsewsarran Profile Picture
    rajivsewsarran 97 on at
    RE: AL coding issues

    Hi Nitin,

    Thanks for your assistance.

    However, the field still isn't auto populating for itself.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: AL coding issues

    Hi, Please change with the following after done some correction.

    tableextension 50134 "Desc 2" extends "Purchase Line"

    {

       fields

       {

           field(50134; "Desc 2"; text[50])

           {

               DataClassification = CustomerContent;

               Caption = 'Desc 2';

               TableRelation = "Dimension Value".Name where("Code" = field("Shortcut Dimension 1 Code"));

           }

           modify("Shortcut Dimension 1 Code")

           {

               trigger OnAfterValidate()

               var

                   GeneralLedgerSetup: Record "General Ledger Setup";

                   DimensionValue: Record "Dimension Value";

               begin

                   "Desc 2" := '';

                   GeneralLedgerSetup.Get();

                   DimensionValue.Reset();

                   DimensionValue.SetRange("Dimension Code", GeneralLedgerSetup."Shortcut Dimension 1 Code");

                   if DimensionValue.FindFirst() then

                       "Desc 2" := DimensionValue.Name;

               end;

           }

       }

    }

    pageextension 50134 "Desc 2 " extends "Purch. Invoice Subform"

    {

       layout

       {

           addafter("Description 2")

           {

               field("Desc 2"; Rec."Desc 2")

               {

                   Caption = 'Desc 2';

                   ApplicationArea = all;

               }

           }

       }

    }

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,698 Super User 2024 Season 2 on at
    RE: AL coding issues

    Hi, Thanks for the addition, I understand now, please try the following.

    tableextension 50134 "Desc 2" extends "Purchase Line"
    {
        fields
        {
            field(50134; "Desc 2"; text[50])
            {
                DataClassification = CustomerContent;
                Caption = 'Desc 2';
                TableRelation = "Dimension Value".Name where("Code" = field("Shortcut Dimension 1 Code"));
                ValidateTableRelation = false;
            }
    
            modify("Shortcut Dimension 1 Code")
            {
                trigger OnAfterValidate()
                var
                    GeneralLedgerSetup: Record "General Ledger Setup";
                    DimensionValue: Record "Dimension Value";
                begin
                    GeneralLedgerSetup.Get();
                    if DimensionValue.Get(GeneralLedgerSetup."Shortcut Dimension 1 Code", "Shortcut Dimension 1 Code") then
                        "Desc 2" := DimensionValue.Name;
                end;
            }
        }
    }
    
    
    pageextension 50134 "Desc 2 " extends "Purch. Invoice Subform"
    {
        layout
        {
            addafter("Description 2")
            {
                field("Desc 2"; Rec."Desc 2")
                {
                    Caption = 'Desc 2';
                    ApplicationArea = all;
                }
            }
        }
    }

    I have tested it.

    pastedimage1653636427830v1.png

    pastedimage1653636435047v2.png

    Thanks.

    ZHU

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: AL coding issues

    Hi,

    Please replace your code with below.

    tableextension 50134 ModifyPurchLine extends "Purchase Line"

    {

       fields

       {

           field(50134; "Desc 2"; text[50])

           {

               DataClassification = ToBeClassified;

               Caption = 'Desc 2';

               TableRelation = "Dimension Value".Name where("Code" = field("Shortcut Dimension 1 Code"));

               ValidateTableRelation = true;

               trigger OnValidate()

               var

                   DimensionValue: Record "Dimension Value";

               begin

                   if DimensionValue.get(Rec."Shortcut Dimension 1 Code") then begin

                       "Desc 2" := DimensionValue.Name;

                   end;

           }

       }

    }

    pageextension 50134 ModifyPurchInvSubform extends "Purch. Invoice Subform"

    {

       layout

       {

           addafter("Description 2")

           {

               field("Desc 2"; Rec."Desc 2")

               {

                   Caption = 'Desc 2';

                   ApplicationArea = all;

               }

           }

       }

    }

    Let me know if it works for you.

  • rajivsewsarran Profile Picture
    rajivsewsarran 97 on at
    RE: AL coding issues

    Hi ZHU,

    thanks as always for you response.

    I've adjusted the code and had no luck

    pastedimage1653617102263v1.png

    This is the purchase line

    the entity code is "Shortcut dimension 1" i need that to be able to auto populate "Desc2", which is the table extension i created above.

    I was using the "Code" field from the dimension value table, but i realize that this may not be suitable.

    I dont get the "OnValidate" Option coming up for my trigger.

    Below is the adjustment i made - i can select the correct value manually

    tableextension 50134 "Desc 2" extends "Purchase Line"
    {
        fields
        {
            field(50134; "Desc 2"; text[50])
            {
                DataClassification = ToBeClassified;
                Caption = 'Desc 2';
                TableRelation = "Dimension Value".Name where("Code" = field("Shortcut Dimension 1 Code"));
                ValidateTableRelation = false;
            }
        }
        procedure OnValidate
    ()
    
           "Desc 2": Record "Purchase Line"
        begin
            if "Desc 2".get(Rec."Shortcut Dimension 1 Code") then begin
                "Desc 2" := "Desc 2"
            end;
        end;
    
    
    }
    
    
    pageextension 50134 "Desc 2 " extends "Purch. Invoice Subform"
    {
        layout
        {
            addafter("Description 2")
            {
                field("Desc 2"; Rec."Desc 2")
                {
                    Caption = 'Desc 2';
                    ApplicationArea = all;
                }
            }
        }
    }

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,698 Super User 2024 Season 2 on at
    RE: AL coding issues

    Hi, This looks like a problem with your OnValidate trigger usage.

    I didn't find the Code field in your source code. (Maybe in Purchase Header?)

    You need to find the Code field and add the OnValidate Trigger in this field, not a local OnValidate procedure. 

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/triggers-auto/field/devenv-onvalidate-field-trigger

    If this is a standard field, you need to use the OnAfterValidate trigger in your TableExtension.

    pastedimage1653607562229v1.png

    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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans