Skip to main content

Notifications

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

OnAfterInsertEvent looping through the repeat until returns to the first line of the procedure code stack

(0) ShareShare
ReportReport
Posted on by 431

Hi All,

BC version: 19.1
deployment: OnPrem
issue: looping of records inside Repeat Until returns to the first line of the procedure code stack, after first record is inserted in the repeat until statement.

Detail description of the issue:

1. custom table no.=50000 - "ABC" with all the fields as expected. Table trigger OnInsert is not used in the table at all.

2. custom codeunit no.=50000 - "ABC Mgt." with OnRun trigger (is empty) and event subsriber is called to insert records in T5000 from another table T50001

below is the code of the CU:

Codeunit 5000 "ABC Mgt."

{
    Description='DZ01';
    trigger OnRun()
    begin
    end;
    
    [EventSubscriber(ObjectType::Table, Database::"ABC", 'OnAfterInsertEvent', '', false, false)]
    procedure InsertMissingTimesFromIntegrationTable(var ABC: Record "ABC")
    var:
        DCERec: Record "DCE";
        FoundRecords: Integer;
        ABC2Rec: Record "ABC";
    begin
        DCERec.RESET;
        DCERec.SETRANGE(DCERec."No.",Rec."No.");
        IF DCERec.FINDSET THEN BEGIN
            FoundRecords := DCERec.COUNT;
            REPEAT
                ABC2Rec."No." := DCERec."No.";
                ABC2Rec.Name := DCERec.First Name;
                ABC2Rec.INSERT(TRUE);
            UNTIL DCERec.NEXT() = 0;
        END;
    end;
}

in this repeat until statement code moves back to line numer=14 - DCERec.RESET; and this is happening in endless loop.

I just do not understand why the code jumps after the insert command in the loop of "Repeat Until" statement to the first code line of the procedure (after Begin). Do you have some idea?

Thank you

BR Damjan

  • Suggested answer
    Damjan Zakojc Profile Picture
    Damjan Zakojc 431 on at
    RE: OnAfterInsertEvent looping through the repeat until returns to the first line of the procedure code stack

    Hi,

    I resolved the issue, by adding additional field "Processed" to the table and on OnAfterInsertEvent the code checks if record has been processed or not, then some additional code is inserting new record in same table from integration table.

    Thanks Damjan

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: OnAfterInsertEvent looping through the repeat until returns to the first line of the procedure code stack

    You should also probably create an OnBeforeInsertEvent instead so you can modify the records before the default OnInsert trigger is executed.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: OnAfterInsertEvent looping through the repeat until returns to the first line of the procedure code stack

    Hi,

    below is the culprit line inthe code.

                   ABC2Rec.INSERT(TRUE);

    As you are using this statement inside the OnInsert Event and again this line calling the same event, and thats why its putting all your code into endless loop.

    Thanks.

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