Skip to main content

Notifications

Announcements

No record found.

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

Business Central - AL Code

Posted on by 97

Hi All,

I'm trying to write some code for the following;

I need the field "Qty Per Unit of Measure"  from the "Item Unit Of Measure " table to be produced on the Purchase order report.

I created the Table , page and report extension.

The issue I'm having is, If for example the Unit of Measure Code for the Item is "Box", the table extension i created, 'Qty per UoM" doesn't automatically populate that field, i can either select from the drop down, or type it in manually.

It shows on the report just fine. What i need it for this field to be populated once the Unit of Measure is selected and ideally be hidden on the Purchase Order page.

Here's my code - I tried a few triggers but they didn't work so i removed them.

Any help would be greatly appreciated!

tableextension 50128 "Qty Per UoM2" extends "Purchase Line"
{
    fields
    {
        field(50128; "Qty Per UoM2"; Decimal)
        {

            Caption = 'Qty Per UoM2';
            TableRelation = "Item Unit of Measure"."Qty. per Unit of Measure"
where(Code = field("Unit of Measure Code"));
            ValidateTableRelation = false;

        }
    }
}
  • rajivsewsarran Profile Picture
    rajivsewsarran 97 on at
    RE: Business Central - AL Code

    Hi ZHU,

    Thanks for all your help, this now works.

    Really appreciate it!

    Regards

    Rajiv

  • Verified answer
    YUN ZHU Profile Picture
    YUN ZHU 73,647 Super User 2024 Season 2 on at
    RE: Business Central - AL Code

    Sorry, I missed a primary key, and there is actually another mistake.

    I checked on the screen this time, try the code below.

    tableextension 50128 "Qty Per UoM2" extends "Purchase Line"
    {
        fields
        {
            modify("Unit of Measure Code")
            {
                trigger OnAfterValidate()
                var
                    ItemUnitOfMeasure: Record "Item Unit of Measure";
                begin
                    if Rec.Type = Rec.Type::Item then
                        if ItemUnitOfMeasure.Get(Rec."No.", Rec."Unit of Measure Code") then
                            Rec."Qty Per UoM2" := ItemUnitOfMeasure."Qty. per Unit of Measure";
                end;
            }
    
            field(50128; "Qty Per UoM2"; Decimal)
    
            {
                Caption = 'Qty Per UoM2';
                TableRelation = "Item Unit of Measure"."Qty. per Unit of Measure" where(Code = field("Unit of Measure Code"));
                ValidateTableRelation = false;
                DataClassification = CustomerContent;
            }
        }
    }

    pastedimage1652843135266v1.png

    Thanks.

    ZHU

  • rajivsewsarran Profile Picture
    rajivsewsarran 97 on at
    RE: Business Central - AL Code

    Hi Alok,

    Thanks for your response.

    This is my code now , i still can't get  it to work. Apologies i'm very new to this.

    tableextension 50108 "Quantity Per UoM" extends "Purchase Line"

    {

       fields

       {

           modify("Unit of Measure")

           {

               trigger OnAfterValidate()

               var

                   ItemUnitOfMeasure: Record "Item Unit of Measure";

               begin

                   if ItemUnitOfMeasure.Get(Rec."No.", Rec."Unit Of Measure Code") then

                       if ItemUnitOfMeasure.Get(Rec."No.") then

                           Rec."Quantity Per UoM" := ItemUnitOfMeasure."Qty. per Unit of Measure";

               end;

           }

           field(50108; "Quantity Per UoM"; Decimal)

           {

               Caption = 'Quantity Per UoM';

               TableRelation = "Item Unit of Measure"."Qty. per Unit of Measure" where(Code = field("Unit of Measure Code"));

               ValidateTableRelation = false;

               DataClassification = CustomerContent;

           }

       }

    }

  • Suggested answer
    Alok.kulshreshtha Profile Picture
    Alok.kulshreshtha 934 on at
    RE: Business Central - AL Code

    Hi,

    In Zhu's code make below correction  and rest all ok. with this change it should work

    if ItemUnitOfMeasure.Get(Rec."No.",Rec."Unit Of Measure Code") then

    Regads,

    Alok Kulshreshtha

  • rajivsewsarran Profile Picture
    rajivsewsarran 97 on at
    RE: Business Central - AL Code

    Hi ZHU,

    Thanks for your response.

    I tried this, but still not getting the "Qty Per UoM2" field to populate.

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,647 Super User 2024 Season 2 on at
    RE: Business Central - AL Code

    Hi, this is because you need to add some code to the validate trigger of the Unit of Measure Code.

    For example.

    tableextension 50128 "Qty Per UoM2" extends "Purchase Line"
    
    {
        fields
        {
            modify("Unit of Measure")
            {
                trigger OnAfterValidate()
                var
                    ItemUnitOfMeasure: Record "Item Unit of Measure";
                begin
                    if Rec.Type = Rec.Type::Item then
                        if ItemUnitOfMeasure.Get(Rec."No.") then
                            Rec."Qty Per UoM2" := ItemUnitOfMeasure."Qty. per Unit of Measure";
                end;
            }
    
            field(50128; "Qty Per UoM2"; Decimal)
    
            {
                Caption = 'Qty Per UoM2';
                TableRelation = "Item Unit of Measure"."Qty. per Unit of Measure" where(Code = field("Unit of Measure Code"));
                ValidateTableRelation = false;
                DataClassification = CustomerContent;
            }
    
        }
    
    }

    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