Skip to main content

Notifications

Announcements

No record found.

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

Making an option Field mandatory and not letting user create a job Planning line

(0) ShareShare
ReportReport
Posted on by 889

Hi ,

I have a field that I need to set to mandatory and stop the line to be created when the field is blank.

I tried to do this as a test field on Insert trigger in the page  ,even after entering the data in the field I get an alert "This field cannot be validated since the key fields are not valid."

Any idea how this could be achieved?

pastedimage1615213402708v2.png

  • Indira88 Profile Picture
    Indira88 889 on at
    RE: Making an option Field mandatory and not letting user create a job Planning line

    Hi Kulla,

    In business central there are in my knowledge two flows where the data gets inserted in the job Planning Lines.

    for the normal case through the Job Planning Line via New Line and through Job Ledger -> Transfer to job Planning Lines.

    if I keep the code without the codeunit then in all cases it gives the alert.

    Also I tried doing the simple onInsert Trigger Page Level,

    It was giving me that validation alert in the subject of the thread.

    Regards,

    Indira

  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Making an option Field mandatory and not letting user create a job Planning line

    Firstly, how your assignment of the page object will reflect on the table when you retrieve, I am not quite what you are trying to do with that code, if you just a add simple statement

    Rec.TestField(Rec."WCI Job Posting Group"); by subscribing to the existing event of the table.  You don't need a table ext for that.

  • Indira88 Profile Picture
    Indira88 889 on at
    RE: Making an option Field mandatory and not letting user create a job Planning line

    Sharing My Code:

    Table.Ext

     trigger OnBeforeInsert()

       begin

           if JPLMgmt.AssignObjectId() = 'Page Job Planning Lines' then begin

               if Rec."WCI Job Posting Group" = '' then begin

                   Rec.TestField(Rec."WCI Job Posting Group");

               end;

           end;

       end;

    Page.Ext Code:

    trigger OnAfterGetCurrRecord()

        begin

            ObjectId := (CurrPage.ObjectId(true));
            if ObjectId = 'Page Job Planning Lines' then begin
                JPLMgmt.SetObjectId(ObjectId);
            end;
        end;
     trigger OnClosePage()
        begin
            ObjectId := '';

            JPLMgmt.SetObjectId(ObjectId);


        end;

    CodeUnit Code:

     procedure SetObjectId(var ObjectId: Text): Text

       begin

           if ObjectId = 'Page Job Planning Lines' then begin

               ObjectIdP := ObjectId;

           end

           else begin

               ObjectId := '';

               ObjectIdP := ObjectId;

           end;

           exit(ObjectIdP);

       end;

       procedure AssignObjectId(): Text

       begin

           ObjectIdP := ObjectIdP;

           exit(ObjectIdP);

       end;

    The issue with this is when editing in excel the job Group is not considered mandatory, the user can save values without entering the Job Posting Group.

    trigger OnValidate cannot be used for existing fields.

    Does OnAfterValidate work?

  • Verified answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Making an option Field mandatory and not letting user create a job Planning line

    Can you share the code you have written? and which field you want to make the field mandatory, the highlighted field Posting Gropu is not an option field.

    As Josh mentioned the best place for you to add is by subscribing to the  OnInsertTrigger of table or you could add in OnValidate of another field that way you can check if your field is populated before they select a value in that.

  • Suggested answer
    JAngle Profile Picture
    JAngle 33,159 on at
    RE: Making an option Field mandatory and not letting user create a job Planning line

    You tried doing the check at table level instead? Use a codeunit and create an event subscriber to the job task table OnAfterInsert. You will need to verify if some other fields are populated before doing your check. Need at least Job No. and Job Task No. so that the primary key is set and you have correct data context.

    Another option might be to check if data is blank OnClosePage trigger for the job card

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 Stars!

Kudos to all of our 2024 community stars!

Meet the Top 10 leaders for December!

Congratulations to our December super stars!

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,579 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans