Skip to main content

Notifications

Announcements

No record found.

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

strange Setrange

(0) ShareShare
ReportReport
Posted on by 221

table 52002 "NYT Best Sellers"
{
    Caption = 'Best Sellers Book';
    DataClassification = CustomerContent;

    fields
    {
        field(10; "List Name"; Text[250])
        {
            Caption = 'List Name';
            DataClassification = CustomerContent;
        }
        field(20; "Line No."; BigInteger)
        {
            Caption = 'Line No.';
            DataClassification = CustomerContent;
        }
        field(30; "Book Title"; Text[250])
        {
            Caption = 'Book Title';
            DataClassification = CustomerContent;
        }
        field(40; "Book Description"; Text[2048])
        {
            Caption = 'Book Description';
            DataClassification = CustomerContent;
        }
        field(50; "Book Author"; Text[250])
        {
            Caption = 'Book Author';
            DataClassification = CustomerContent;
        }
        field(60; "Amazon URL"; Text[250])
        {
            Caption = 'Amazon URL';
            DataClassification = CustomerContent;
            ExtendedDatatype = URL;
        }
    }
    keys
    {
        key(PK; "List Name", "Line No.")
        {
            Clustered = true;
        }
    }
    trigger OnInsert()
    begin
        "Line No." := GetNextLineNo();
    end;

    procedure GetNextLineNo(): BigInteger
    var
        NYTBestSellers: Record "NYT Best Sellers";
    begin
        NYTBestSellers.SetRange("List Name", "List Name");
        if NYTBestSellers.FindLast() then
            exit(NYTBestSellers."Line No."   1);
        exit(1);
    end;
}

here setrange applied on same table and with same filed .Generally we use setrange to field data from two table with one field from table 1 and other filed from table 2 then we get mating value then we perform any operation like findfirst or findset and do operation on fields 

but what is purpose of setrange on same table with same field ?

may be answer is easy but i don't know answer please experts can you guide me on this 

thanks in advance....

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 993 Super User 2024 Season 1 on at
    RE: strange Setrange

    The first parameter is the field name you want to set range on and the second one is the value you want to set the range on.

    Setrange is basically just another way for adding a filter.

    So

    setfilter(Fieldname,filtervalue);

    would have the same effect.

    But your conclusion is correct. Line number is increased by 1.

  • DineshM Profile Picture
    DineshM 221 on at
    RE: strange Setrange

    Hello

    what I understand here two parameter in setrange  actually but one from same table .one is record variable which provide independent  Record NYT Best Sellers table and one is Rec. Listname from same table.

    when oninsert trigger invoke by inserting value in listname it will check in Record variable filed list name and then if last line match then it will increment line no by 1

  • Verified answer
    YUN ZHU Profile Picture
    YUN ZHU 75,883 Super User 2024 Season 2 on at
    RE: strange Setrange

    Hi, NYTBestSellers."List Name" and "List Name" are the same field, but not in a same variable.

    As Inge M. Bruvik said, the latter "List Name" should be Rec."List Name", The purpose of these codes are to find last "Line No." in "NYT Best Sellers" table which "List Name" is same as opening record.

    Hope this will help.

    Thanks.

    ZHU

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 993 Super User 2024 Season 1 on at
    RE: strange Setrange

    The purpose here is to get a set of records from the NYT Best Sellers table with the same "List Name" that is just inserted.

    To make the code more readable i think it should be changed to:

    NYTBestSeller.SetRange("List Name",rec."List Name");

    Then it would be clearer that you actually set a filter on the list name with the value for the record that is just inserted since the GetNextLineNo is called from the OnInsert trigger.

    I hope this made sense.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,468 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans