Skip to main content

Notifications

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

AL Code Help

(1) ShareShare
ReportReport
Posted on by 190

Hello all, 

i want to restrict some users to reopen the Document once released 

Pls help me to fix the issue in this

tableextension 55010 "User SetupPage(For Job Crate)" extends "User Setup"
{

    fields
    {
        field(55010; "Allow ReOpen"; Boolean)
        {
            Caption = 'Allow ReOpen';
            DataClassification = CustomerContent;
        }
    }
}



pageextension 55011 "User Setup Job Cr" extends "User Setup"
{
    layout
    {
        addafter("Register Time")
        {
            field("Allow ReOpen"; Rec."Allow ReOpen")
            {
                ApplicationArea = All;
                Caption = 'Allow ReOpen';
            }
        }
    }
}

pageextension 55012 "Purchase Order" extends "Purchase Order"
{
    layout
    {
        // Add changes to page layout here
    }

    actions
    {
        modify(Reopen)
        {
            ApplicationArea = all;
            trigger OnAction()
            var
                UserSetup: Record "User Setup";
            begin
                if UserSetup.Get(UserId) then
                    if UserSetup."Allow ReOpen" then
                        exit;

                Rec.Modify(false)
                Confirm: Label 'You Dont have a permission to Reopen';
                // CurrPage.Editable(false);
            end;

        }

    }
}

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: AL Code Help

    You can use this code:

     [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Purchase Document", 'OnBeforeReopenPurchaseDoc', '', false, false)]

       local procedure OnBeforeReopenPurchaseDoc(var PurchaseHeader: Record "Purchase Header"; PreviewMode: Boolean; var IsHandled: Boolean);

       var

           UserSetup: Record "User Setup";

       begin

    If PurchaseHeader."Document Type" = "PurchaseHeader."Document type" :: Order then begin

           UserSetup.Get(UserId);

           if not UserSetup."Allow ReOpen" then

               Error('You Dont have Permission to Reopen');

    end;

       end;

  • aryaps Profile Picture
    aryaps 151 on at
    RE: AL Code Help

    if i use the same code unit, then how can i mention the Type = Order 

        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Purchase Document", 'OnBeforeReopenPurchaseDoc', '', false, false)]
        local procedure OnBeforeReopenPurchaseDoc(var PurchaseHeader: Record "Purchase Header"; PreviewMode: Boolean; var IsHandled: Boolean);
        var

            UserSetup: Record "User Setup";

        begin

            UserSetup.Get(UserId);

            if not UserSetup."Allow ReOpen" then
                Error('You Dont have Permission to Reopen');

        end;
  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: AL Code Help

    Then you use the document type filed:

    If "Document type" = "Document Type" :: Order then begin

    //your processing

    end;

  • aryaps Profile Picture
    aryaps 151 on at
    RE: AL Code Help

    if we want to put this condition only in Purchase order, if we put this it will affect in Purchase invoice also. so how do we mention the scope as order in this code unit

  • pankaj.k Profile Picture
    pankaj.k 962 on at
    RE: AL Code Help

    Good to know that your problem is resolved :-)

  • LearnBC Profile Picture
    LearnBC 190 on at
    RE: AL Code Help

    Thanks Pankaj..its working perfect for both scenarios

  • Verified answer
    pankaj.k Profile Picture
    pankaj.k 962 on at
    RE: AL Code Help

    Hi 

    Try with this event that will work for Both "Purchase Order List Page"  and "Purchase Order card Page"

    4130.Capture.PNG


        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Purchase Document", 'OnBeforeReopenPurchaseDoc', '', false, false)]
        local procedure OnBeforeReopenPurchaseDoc(var PurchaseHeader: Record "Purchase Header"; PreviewMode: Boolean; var IsHandled: Boolean);
        var

            UserSetup: Record "User Setup";

        begin

            UserSetup.Get(UserId);

            if not UserSetup."Allow ReOpen" then
                Error('You Dont have Permission to Reopen');

        end;
    Note: If you use this event then please comment the previous Event Code.
  • Suggested answer
    pankaj.k Profile Picture
    pankaj.k 962 on at
    RE: AL Code Help

    Hi..

    1. you can add more event in the same Codeunit. you need to add same event for the "Purchase order List Page".
    2. if you want to modify "purchase Order List Page" then you need to create separate Page object  in same extension.

    Note: you can add more objects in same extension.

  • LearnBC Profile Picture
    LearnBC 190 on at
    RE: AL Code Help

    i have one more doubt. Right now we want to do the same function from purchase order List also

    can we add purchase order list also in this code unit. or again we want to create sep page extension for Purcahse  order list

    Codeunit 55013 "Purchase Order"

    {

       [EventSubscriber(ObjectType::Page, Page::"Purchase Order", 'OnBeforeActionEvent', 'Reopen', true, true)]

       local procedure OnBeforeActionEventWithPO(var Rec: Record "Purchase Header")

       var

           UserSetup: Record "User Setup";

           ErrorLabels: Label 'You Do not have Permission to Reopen.';

       begin

           UserSetup.Get(UserId);

           if not UserSetup."Allow ReOpen" then

               Error(ErrorLabels);

       end;

    }

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

    Good to know that we could help you.

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

News and Announcements

Announcing Category Subscriptions!

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans