Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Pass any record as a parameter to procedure

(0) ShareShare
ReportReport
Posted on by

How can I create a method like Page.SetTableView(var Record: Record)?

It is possible to pass record of any table to this method.

Signature like 

procedure DoSomething(anyRec:Variant)
 is not suitable, becase in this case we can pass any type (Record, Integer, Date).

Example:

procedure DoSomethingWithRecords()
var
    c:Record Customer;
    i: Record Item;
begin
//...
    i.FindFirst();
   c.FindFirst();
//...
    DoSomething(i);  //ok
    DoSomething(c);  // ok
    DoSomething(1); // should not be possible to compile (rise AL0133)
end;


What signature should be correct for the method DoSomething in this case?

  • Inge M. Bruvik Profile Picture
    1,021 Moderator on at
    RE: Pass any record as a parameter to procedure
    [quote user="kkrtsv"]

    Inge M. Bruvik, Teddy Herryanto

    Thanks for your answers, recordref is a helpfull workaround like a variant.

    But it is not the answer.

    To be honest I don't know why this question is so difficult. =(

    We already have the method "Page.SetRecord" in the core, but nobody can explain how it works.

    This method does not use variant or recordref, this method accepts any record and only record.

    Just look on this example:

    procedure Example()

       var

           somePage: Page "Customer Card";  

           rec1: Record Customer;

           rec2: Record Item;

           rec3: Record "Incoming Document";

       begin

           somePage.SetRecord(rec1);

           somePage.SetRecord(rec2);

           somePage.SetRecord(rec3);

       end;

    All three lines are correct. It does not require any specific table we can pass any.

    And this code will be successfully compiled.

    [/quote]

    The example you should does not accept any record. It only accept record of the type Customer with any filter applied to it. And that is no problem to get working as long as you know what record type you send.

    In the function you only have to declare t he record parameter as VAR.

     procedure customer (var customer: Record Customer)
        begin
           
        end;
    But if you do not know what kind of record you will pass to the function then you will have to use record ref.
  • Community Member Profile Picture
    on at
    RE: Pass any record as a parameter to procedure

    Inge M. Bruvik, Teddy Herryanto

    Thanks for your answers, recordref is a helpfull workaround like a variant.

    But it is not the answer.

    To be honest I don't know why this question is so difficult. =(

    We already have the method "Page.SetRecord" in the core, but nobody can explain how it works.

    This method does not use variant or recordref, this method accepts any record and only record.

    Just look on this example:

    procedure Example()

       var

           somePage: Page "Customer Card";  

           rec1: Record Customer;

           rec2: Record Item;

           rec3: Record "Incoming Document";

       begin

           somePage.SetRecord(rec1);

           somePage.SetRecord(rec2);

           somePage.SetRecord(rec3);

       end;

    All three lines are correct. It does not require any specific table we can pass any.

    And this code will be successfully compiled.

  • Suggested answer
    YUN ZHU Profile Picture
    81,489 Super User 2025 Season 1 on at
    RE: Pass any record as a parameter to procedure

    Hi, I know this might not be a great example, but hopefully it gives you some inspiration.

    [View:/cfs-file/__key/communityserver-discussions-components-files/758/0825.Test.mp4:822:617

    page 50100 MyPage
    {
        PageType = Card;
        ApplicationArea = All;
        UsageCategory = Administration;
    
        layout
        {
            area(Content)
            {
                group(GroupName)
                {
                    field(TableID; TableID)
                    {
                        ApplicationArea = All;
                        TableRelation = AllObjWithCaption."Object ID" where("Object Type" = filter('Table'));
                    }
                }
            }
        }
    
        actions
        {
            area(Processing)
            {
                action(ActionName)
                {
                    ApplicationArea = All;
    
                    trigger OnAction()
                    var
                        RecRef: RecordRef;
                    begin
                        RecRef.Open(TableID);
                        MyProcedure(RecRef);
                    end;
                }
            }
        }
    
        var
            TableID: Integer;
    
        local procedure MyProcedure(RecRef: RecordRef)
        var
            MyFieldRef: FieldRef;
        begin
            MyFieldRef := RecRef.Field(1);
            if RecRef.FindSet() then
                Message(MyFieldRef.Value);
        end;
    }

    Thanks.

    ZHU

  • Teddy Herryanto (That NAV Guy) Profile Picture
    13,474 Moderator on at
    RE: Pass any record as a parameter to procedure

    Use RecordRef as your parameter. You can retrieve any tables using it.

  • Inge M. Bruvik Profile Picture
    1,021 Moderator on at
    RE: Pass any record as a parameter to procedure

    If i understand your question correctly then i think the recordref datatype is what you are looking for.

    The recordref can be used to pass any record to a procedure

    docs.microsoft.com/.../recordref-data-type

  • Community Member Profile Picture
    on at
    RE: Pass any record as a parameter to procedure

    Unfortuantely, this answer is absolutely unsuitable. You did not get the question at all.

    Firts of all, your method DoSomething uses 2 arguments what is already wrong.

    Second, you specified tables, what is also wrong. One method should be able to process any record (customer, item, company, whatever, etc). No one table must be specified.

    Check how the mentioned method works docs.microsoft.com/.../page-settableview-method.

    You can pass every record to it.

  • Vaishnavi J Profile Picture
    3,058 on at
    RE: Pass any record as a parameter to procedure

    Hi,

    You can write the above logic in this way.

    //Creating a procedure which takes two references.

    local procedure DoSomething(var Rec_Customer: Record Customer; var Rec_Item: Record Item)

       begin

           Message('The first customer name is %1', Rec_Customer.Name);

           Message('this first item name is %1', Rec_Item.Description);

       end;

    // How can you call the above procedure.

    1. You can call in using an action button click.

     action("Accept Order")
                    {
                        ApplicationArea = All;

                        trigger OnAction()
                        var
                            c: Record Customer;
                            i: Record Item;
                        begin

                            i.FindFirst();
                            c.FindFirst();
                            DoSomething(c, i); // You need to pass two refrences as you have created procedure which takes two value. if you want seperate procedure which does different things then create  a sperate procedure and take one record as parameter.
                        end;
                    }
    2. Or you can call using a field validation.
      field(SOStatus; rec.SOStatus)
                    {
                        ApplicationArea = All;
                        ToolTip = 'Sales Order Status';
                        Editable = true;

                        trigger OnValidate()
                        var
                            c: Record Customer;
                            i: Record Item;
                        begin
                            c.FindFirst();
                            i.FindFirst();
                            DoSomething(c, i);
                        end;
                    }
    3. Or you can call your procedure in some other procedure.
    procedure DoSomethingWithRecords()
    var
        c:Record Customer;
        i: Record Item;
    begin

        i.FindFirst();
        c.FindFirst();

        DoSomething(c, i)
    end;
    If my answer was helpful to you, please verify it so that other users know it worked. Thank you very much

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,145 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,917 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans