Skip to main content

Notifications

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

VALIDATION IN BUSSINESS CENTRAL

Posted on by 208
I want to apply validation on an existing field such that if i keep it empty and save it or send for approval it should throw validation error.Why this code is not working in my table extention.
 modify("Free Text")
        {
            trigger OnBeforeValidate()
           
            begin
                if (rec."Free Text" = '') then
                    Error('Narration field is blank on Purchase Order. Kindly fill the same to sent PO for approval');
            end;
        }
  • Verified answer
    Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    Hi Pragya,

    I don't know what your enum values are. But here, I have created a custom enum with a default value of blank.

    enum 50152 InvoiceCategory
    {
        Extensible = true;
    
        value(0; MyValue1)
        {
            Caption = '';
        }
    
        value(1; MyValue2)
        {
            Caption = 'Value 2';
        }
    
        value(3; MyValue3)
        {
            Caption = 'Value 3';
        }
    }

    To validate your "Invoice Category" field with the "Free Text" field, add this code

            if (PurchaseHeader."Free Text" = '') then
                Error('Narration field is blank on Purchase Order. Kindly fill the same to sent PO for approval');
    
            if (PurchaseHeader."Invoice Category" = InvoiceCategory::MyValue1) then
                Error('Narration field is blank on Purchase Order. Kindly fill the same to sent PO for approval');

    Regards

  • Suggested answer
    Pragya752 Profile Picture
    Pragya752 208 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    I used this.I wanted to make aone more field mandatory which is an Enum.Can i add that here as well like this ?

    codeunit 90125 TestCodeUnit
    {
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Approvals Mgmt.", 'OnBeforeCheckPurchaseApprovalPossible', '', false, false)]
        local procedure OnBeforeCheckPurchaseApprovalPossible(PurchaseHeader: Record "Purchase Header"; var Result: Boolean; var IsHandled: Boolean);
        begin
            if (PurchaseHeader."Free Text" = '') then
                Error('Narration field is blank on Purchase Order. Kindly fill the same to sent PO for approval');
                if(PurchaseHeader."Invoice Category".AsInteger() = 0 ) then
                  Error('Narration field is blank on Purchase Order. Kindly fill the same to sent PO for approval');
        end;
    }
  • Suggested answer
    Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    Hi Pragya752,

    Try this code. It will work for you in runtime 9.0

    codeunit 50156 TestCodeUnit
    {
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Approvals Mgmt.", 'OnBeforeCheckPurchaseApprovalPossible', '', false, false)]
        local procedure OnBeforeCheckPurchaseApprovalPossible(PurchaseHeader: Record "Purchase Header"; var Result: Boolean; var IsHandled: Boolean);
        begin
            if (PurchaseHeader."Free Text" = '') then
                Error('Narration field is blank on Purchase Order. Kindly fill the same to sent PO for approval');
        end;
    }

    Regards

  • Pragya752 Profile Picture
    Pragya752 208 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    I want this field to be mandatory before it sending for approval

  • Pragya752 Profile Picture
    Pragya752 208 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    Support for using Identifiers instead of Literals in event subscribers.' is not available in runtime version '9.0'. The supported runtime versions are: '11.0' or greater this error is coming.

  • Pragya752 Profile Picture
    Pragya752 208 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    let me try this

  • Suggested answer
    Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    just adding some additional information here.

    you can try using the OnBeforeCheckPurchaseApprovalPossible event,

    pastedimage1681724659100v1.png

    pastedimage1681724716472v2.png

    codeunit 52452 TestCodeUnit
    {
    
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Approvals Mgmt.", OnBeforeCheckPurchaseApprovalPossible, '', false, false)]
        local procedure OnBeforeCheckPurchaseApprovalPossible(var PurchaseHeader: Record "Purchase Header"; var Result: Boolean; var IsHandled: Boolean);
        begin
            if (PurchaseHeader."Free Text" = '') then
                Error('Narration field is blank on Purchase Order. Kindly fill the same to sent PO for approval');
        end;
    }

    It runs before the purchase order is sent for approval, so you can use it to add your validation check and prevent the order from being sent if the field is empty.

    pastedimage1681724785802v3.png

    Regards.

  • Suggested answer
    Mohana Yadav Profile Picture
    Mohana Yadav 59,127 Super User 2024 Season 2 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    yes, if you write code in above event then whenever you click on 'send approval request' the system will check if the field is entered or not.

    if not then show error

  • Pragya752 Profile Picture
    Pragya752 208 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    This field should be mandatory before sending the Sales Invoice under Approval Workflow.How can i make this field mandatory

  • Suggested answer
    Mohana Yadav Profile Picture
    Mohana Yadav 59,127 Super User 2024 Season 2 on at
    RE: VALIDATION IN BUSSINESS CENTRAL

    Please add your verification in OnAfterCheckSalesApprovalPossible event

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans