web
You’re offline. This is a read only version of the page.
close
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

This code is freezing business central.

(0) ShareShare
ReportReport
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?

I have the same question (0)
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    What field are you running the validate trigger for?

    These 3 lines can be replaced

    Job.Reset();

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

    If FindFirst() then

    Insted use:

    If Job.get("Job No.") then message.........

  • Suggested answer
    YUN ZHU Profile Picture
    95,595 Super User 2025 Season 2 on at

    Hi, try the following.

                trigger OnValidate()
                var
                    Job: Record Job;
                begin
                    If Rec."Job Task Type" = "Job Task Type"::Posting then
                        if Job.Get(Rec."Job No.") then
                            Message('Job is %1 and %2', Job."No.", Job.Description);
                End;
    Hope this helps.
    Thanks
    ZHU
  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    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;

  • Suggested answer
    ShanAbeywicrema Profile Picture
    948 on at

    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

  • Romryan Profile Picture
    on at

    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
    Nitin Verma Profile Picture
    21,708 Moderator on at

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

    Thanks

  • Romryan Profile Picture
    on at

    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
    Govinda Kumar Profile Picture
    2,304 Moderator on at

    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

  • Suggested answer
    ShanAbeywicrema Profile Picture
    948 on at

    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
    MetaKa Profile Picture
    on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,010

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,270 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,085 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans