Skip to main content

Notifications

Announcements

No record found.

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

This code is freezing business central.

Posted on by

Dear All,

I have the following code inside a field of Job Tasks subform.

This code is freezing business central and during the debugging is showing out of scope when I hover over the Job.No. I want to read the content of a field from the job table and use it

trigger OnValidate()
var
Job: Record Job;
begin
If "Job Task Type" = "Job Task Type"::Posting then
Job.Reset();
Job.SetRange("No.", "Job No.");
If FindFirst() then
Message('Job is %1 and %2', Job."No.", Job.Description);
End;

What could be the issue with the code and how well can I achieve my goal?

Categories:
  • Romryan Profile Picture
    Romryan on at
    RE: This code is freezing business central.

    If I remove this piece of code, there is no more freezing, but I need it because I want to pick some details from Job table.

    If Rec."Job Task Type" = "Job Task Type"::Posting then

                          If Job.Get(Rec."Job No.") then begin

                              Message('Job is %1 and %2', Job."No.", Job.Description);

                          End;

  • Romryan Profile Picture
    Romryan on at
    RE: This code is freezing business central.

    People Eater,

    Contract sum is a field I have customized in the job table.

    If I remove modify, BC will not record what I will have entered.

  • Suggested answer
    MetaKa Profile Picture
    MetaKa on at
    RE: This code is freezing business central.

    The problem is:

    Until Next = 1;

    In this example, Are u sure about this line? U said that u just have 1 JOB; so the next line must be 0 but never 1.

    Dunno if u understand why its a infinite loop, but its pretty self explanatory why ur program is infinitely looping over it, since u are tellig him to repeat the loop until the next register will be = 1; and there is no other register here.

  • Suggested answer
    ShanAbeywicrema Profile Picture
    ShanAbeywicrema 940 on at
    RE: This code is freezing business central.

    I am not sure why do you use 

     Modify(); and Reset();

    Also, is it necessary to put calcsum after findset? should it before that? 

    SetRange("Job Task Type", "Job Task Type"::Posting); 

    CalcSums("Estimated % Duration Weights");    <---- like this?

    Definitely it become slow with  CalcFields("Schedule (Total Cost)"); when you have more records.      

    This is my reading, you can reduce the code by removing unwanted coding, Use separate function, pass the rec, do the calculation, get the value and update.               

    Hope this will help you.

      

  • Suggested answer
    Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: This code is freezing business central.

    Hi,

    I have tried running your code and it is not freezing at all.. I just wanted to confirm if "Contract Sum" in your code is added by you? because I could not find it in the Job table. Also, please remove "Modify();" from the code

    Regards

  • Romryan Profile Picture
    Romryan on at
    RE: This code is freezing business central.

    Nitin Verma,

    field("Estimated % Duration Weights"; "Estimated % Duration Weights")

               {

                   ApplicationArea = All;

                   trigger OnValidate()

                   var

                       Job: Record Job;

                   begin

                       If Rec."Job Task Type" = "Job Task Type"::Posting then

                           If Job.Get(Rec."Job No.") then begin

                               Message('Job is %1 and %2', Job."No.", Job.Description);

                           End;

                       If "Job Task Type" = "Job Task Type"::Posting then Begin

                           Modify();

                           Reset();

                           SetCurrentKey("Job No.");

                           SetRange("Job No.", "Job No.");                        

                           SetRange("Job Task Type", "Job Task Type"::Posting);

                           If Findset() then

                               Repeat

                                   CalcSums("Estimated % Duration Weights");

                                   CalcFields("Schedule (Total Cost)");

                                   MESSAGE('%1 Weights %2 Budget Cost %3', "Estimated % Duration Weights", "Schedule (Total Cost)", Job."Contract Sum");

                                   If "Estimated % Duration Weights" > 100 then

                                       Error('%1 Weights shouldn''t be greater than 100%!', "Estimated % Duration Weights");

                                   If "Schedule (Total Cost)" > Job."Contract Sum" then

                                       Error('The total lines amount %1 cannot be greater than %2 the contract sum!', "Schedule (Total Cost)", Job."Contract Sum");

                               Until Next = 1;                                          

                           CurrPage.Update();

                       End

                       else Begin

                           Error('Percentage Weights can only be applied to Job Tasks of type Posting');

                       End;

                   end;

               }

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: This code is freezing business central.

    Share your complete code once again  how you have written your logic

    Thanks

  • Romryan Profile Picture
    Romryan on at
    RE: This code is freezing business central.

    I am still experiencing the freezing. I have a trigger on field, Estimated % Duration Weights as shown below encircled in RED. This is a customized field where I put the percentages of Job tasks. All jobs tasks are assigned percentages that don't exceed 100. When I enter 100% since it's one task, BC freezes and I don't get a message as per code that I have shared here.

    I really don't know what's causing the freezing. Kindly advice.

    pastedimage1681982451919v3.png

  • Suggested answer
    ShanAbeywicrema Profile Picture
    ShanAbeywicrema 940 on at
    RE: This code is freezing business central.

    As all mentioned you can you GET function instead of setrange and findfirst.  GET retrieves one record based on values of the primary key fields.

    You can get theoretical idea from here learn.microsoft.com/.../devenv-get-find-and-next-methods

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: This code is freezing business central.

    HI,

    The problem in your code is, you have not put Begin..End after If condition which is the culprit.

    trigger OnValidate()
    var
    Job: Record Job;
    begin
        If "Job Task Type" = "Job Task Type"::Posting then
        Begin
            Job.Reset();
            Job.SetRange("No.", "Job No.");
            If FindFirst() then
            Message('Job is %1 and %2', Job."No.", Job.Description);
        end;    
    End;

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!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans