Options

make a field mandatory

kikkomankikkoman Member Posts: 57
hello everyone,

i have searched the forums and read some of the post on this subject, but i still can't seem to get it to work. it seems very simple, yet i can't figure it out.

i have added a new field to the job table called Work Order. Every work order will be tied to a job number. The new field is type text 50. What I would like is if a new job is created, the work order field has to be filled.

i tried going into the job card form and changing the "delayed insert" property to yes, and then placing code in the OnInsert and OnModify triggers on the job table. The code is just either

TESTFIELD("Work Order");
or
IF "Work Order" = '' THEN
ERROR("Please insert a Work Order");

but when i try a combination of these things, i either can NOT create a new job b/c once i try to create a new job, i get the constant error message to fill in the work order, but i can't even click the new field on the form to change it, or the job is created and inserted and modified without having a Work Order or the error pops us and then the form closes.

I even tried to place those code bits in the OnValidate trigger of the new field Work Order.

I even read where someone metioned to :
I can suggest you another way:
1. Block Items after insert (field Blocked must be TRUE, and it couldn't be editable )
2. Provide some function to test the item
if required fields are not blank, then you "Unblock" item, in the other case
you can demand to fill up your field

I was wondering if there was a simplier solution? I must be missing something..it seems so basic, yet i can't get it.... ](*,) DOH!!!

Comments

  • Options
    triciaallisontriciaallison Member Posts: 20
    I have done a similar thing in sales orders. However, I just made a field mandatory in order to post the order, so I put the code in the OnRun trigger of the posting codeunit. Just a thought...
  • Options
    SavatageSavatage Member Posts: 7,142
    I've done something similar on the Vendor card for Payment terms. I want the user to know if they enter a vendor and leave the field blank. I put the code on the form not the table.

    OnCloseForm
    TESTFIELD("Payment Terms Code");

    This is fine for us.. I believe there is a mandatory post that doesn't allow the user to leave until it's entered. lemme see.....

    ah found it...
    http://www.mbsonline.org/forum/topic.as ... ORY,FIELDS
  • Options
    kikkomankikkoman Member Posts: 57
    oh yeah...that did the trick!!! thanks for everyone's input...i really appreciate it!!! \:D/

    hmmm....somehow the code from that link above allows you to create a new record and then when you hit the -> (next) button, you can create another new record, but when you try to close, it won't let you until you place a value into the "Work Order", which is what I want, but then somehow, it creates 2 records. the 1st one is missing a Work Order, but the 2nd one has it.... :-k

    Here is the code that I used...

    in OnQueryCloseForm ...
    IF "Work Order" = '' THEN
    ERROR('Please insert a value into the Work Order field for Job No. %1!', "No.");


    in OnAfterGetRecord ... No. is the PK
    IF ("No." <> xRec."No.") AND (xRec."No." <> '') THEN BEGIN
    IF xRec."Work Order" = '' THEN BEGIN
    Rec := xRec;
    xRec.TRANSFERFIELDS(Rec);
    CurrForm.UPDATE(FALSE);
    MESSAGE('Please fill in the Work Order field!');
    END;
    END;

    im assuming that "No." is the PK that you have to use, that is why I choose "No." b/c that is the PK in table Job. this may be the reason why i am getting that problem.

    If you run this code and create 1 record at a time, it works like a charm...

    I LOVE THESE FORUMS!!!!
  • Options
    giulio.cipogiulio.cipo Member Posts: 77
    other solution that i use always and is more simple is :
    in the trigger on_modify of table check the information and block the table if the value is not insered.

    IF "Work Order" = '' THEN begin
    blocked := true;
    Message( 'item will be blocked because yod don't insert ..')
    end;


    I use this solution because i don't want to use the error, with error if you make 2 modify the sistem rallback all and this his a problem.

    anyway there are other topic i the forum that speack about this problem
  • Options
    sggsgg Member Posts: 109
    Have you tried the following ?:

    1. On the Property of the Field (In the table, Set "NOT BLANK" to True). This works only when you enter into the field (when the cursor is inside the field).

    2. On the OnBeforePutRecord() Trigger of the Form, put a TESTFIELD to ensure that the value is set. It this is done, the system generates an error Whenever you do not put a value in the field.

    This System will work whether you modify the record or not.

    However, you may choose not to use a testfield to avoid Error Message. then you can use a message function to show the message.
    Sunday, Godwin G
  • Options
    kimwkimw Member Posts: 6
    when i try the testfield method on the field <name> of an company address card it doesn't work... when i push the button to create a new datarecord navision creates a new record and jump to the new record and tells me that the name field is emtpy... for sure it is empty becouse it is the field of the new record and not the name field in the used record...

    :(

    where is my mistake?
  • Options
    SavatageSavatage Member Posts: 7,142
    What trigger did you put your code on?

    for example
    on my Vendor Card - to make sure Payment Terms Code is filled. When the user closed the form & the field is blank a message appears.

    OnCloseForm()
    TESTFIELD("Payment Terms Code");
  • Options
    SavatageSavatage Member Posts: 7,142
    too funny i realized as i scrolled thru the post - that i already posted that example :oops:
  • Options
    themavethemave Member Posts: 1,058
    sgg wrote:
    1. On the Property of the Field (In the table, Set "NOT BLANK" to True). This works only when you enter into the field (when the cursor is inside the field).

    You would think that since this has been part of Navision since at least 2.0, and since every user of Navision since the beginning of time has requested this option, that it would actually work without doing additional coding, but alas, it doesn’t work without extra coding.

    Maybe in Navision 5.0 you will actually be able to set this property on a form and it will work

    We can always hope.
  • Options
    kimwkimw Member Posts: 6
    when i try the testfield my form is closing.. why?
    what can i do to keep it open?
  • Options
    SavatageSavatage Member Posts: 7,142
    kimw wrote:
    when i try the testfield my form is closing.. why?
    what can i do to keep it open?

    TESTFIELD does not stop the form from closing

    http://www.mibuso.com/forum/posting.php ... te&p=17988

    Doesn't help? Maybe you're doing something wrong?
  • Options
    ShenpenShenpen Member Posts: 386
    Sorry, but there is NO way to make a field 100% mandatory. You can warn users and whatever, but 1) users can always close a form with the mouse, even when they are prevented from closing it with ESC 2) users can PgUp to another record and then close the form - you need really ugly hacking in OnFindRecord/OnNextRecord to prevent it 3) whan ERROR in OnModify can do is to not save modifications, but cannot prevent closing the form.

    I suggest to make a new field, a checkmark, called "approved", put the checking of mandatory fields in the OnValidate, and tell users to check this field and their work is not finished until they manage to check it. Make a simple report showing "not approved" records and tell managers to check it every evening and shout the hair off people if it shows any records. Also make the name/description of the record red in the list form so others know that that record is not finished. If it is an Item, you can also block until it is approved.

    Do It Yourself is they key. Standard code might work - your code surely works.
  • Options
    wsmithwsmith Member Posts: 4
    ](*,) For all of you banging your heads against the wall, here is a quick solution that I came up with:

    1. On the table (this example is using the Job table, Person Responsible column) put the following code on the OnValidate trigger of the column that needs to be mandatory:

    IF ("Person Responsible" = '') THEN
    IF (CONFIRM(error10, TRUE, FIELDCAPTION("Person Responsible"), "No.")) THEN
    ERROR(error11, FIELDCAPTION("Salesperson Code"))
    ELSE
    Rec := xRec;


    This will confirm with the user that the field is required and give them th option of reentering it. If they say yes, then the ERROR aborts the update to the table and keeps the form open. If they say no, then the form is closed and the record is set back to what it was prior to the field being updated.

    2. Place the following code in the OnQueryCloseForm trigger of the Form:

    VALIDATE("Person Responsible");

    This forces the OnValidate trigger to fire even though the user has not entered anything in the field in question.

    I hope this helps! Take Care! 8)
  • Options
    Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    With a commenter's help on my blog, I have a 95% right working solution for mandatory fields.

    1. Set Form to DelayedInsert.

    2. Create a Button called Save, code: IF NOT INSERT THEN MODIFY;

    3. Create a function called SaveData, with code:

    IF NOT CONFIRM(’Do you want to save changes?’) THEN ERROR(’’);

    You can put TESTFIELD here for all the mandatory fields.

    4. Put a call to SaveData to OnInsertRecord and OnModifyRecord.

    What you got?

    Whenever the user changes data and either moves away from the record or pushes save, the confirmation question aroses to protect against unwanted data entry, and then the fields are checked.

    The reason it's a 95% right solution is that there is a minor inconvenienced that if the users gets an error message during a modify, all his changes are lost. However, during an insert, luckily, not, so it's not a big problem.
  • Options
    Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Savatage wrote:
    What trigger did you put your code on?

    for example
    on my Vendor Card - to make sure Payment Terms Code is filled. When the user closed the form & the field is blank a message appears.

    OnCloseForm()
    TESTFIELD("Payment Terms Code");

    The message is displayed, but the form is also closed.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • Options
    SavatageSavatage Member Posts: 7,142
    LOL - that's an old post - & yes it does that - but that's what we were looking for at the time :lol:

    Now i tend to go for
    http://www.mibuso.com/forum/viewtopic.php?t=9646
  • Options
    vyankuvyanku Member Posts: 791
    hi,
    I also try this type of coed.
    Here I want to make Dimension code mandatory at the time of posting.
    So I insert this code in G/L journal =>Post button =>Onpush trigger.
    IF xRec."Shortcut Dimension 1 Code" ='' THEN
    BEGIN
    MESSAGE('Please enter the dimension code');
    CurrForm.CLOSE;
    END
    ELSE
    BEGIN
    rest of code
    end;
    

    And it works very well.
  • Options
    Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    I think in the long run the best idea is to block the record at creation, and only allow unblocking if mandatory fields are populated and so on. This is the easiest to do and creates the least of inconvenience for the user - they can leave the record half-populated and go and find out the answers for the missing fields and come back to finish it later on.
  • Options
    Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Correct! We now have the InitValue of the Blocked-field set to TRUE. When people want to uncheck this Blocked-field, some tests are performed to be sure critical fields are filled in. If they are not, the record cannot be unblocked.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • Options
    Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Yes, because all the other ideas of mandatoriness (about not allowing to save the record or to leave the form etc.) create a lot of annoyance. F.e. let's assume a new employee is joining and somebody is entering their data into a payroll system: they ask for the name, enter it, ask for the address, enter it, ask for the date of birth, enter it, ask for the tax number... and then the guy says I dunno, I'll look it up for tomorrow. Now it's a real annoyance if you can't just save the data you entered up to that point.
  • Options
    gvolkovgvolkov Member Posts: 196
    I developed this little solution for mandatory fields. you have a table with all the tables and fields you want to be mandatory. Then you have a function on each table which generates index of fieldrefs. and checks if that field is in the table, then a dynamic error message is displayed. i am still in beta on this solution.
    Microsoft Certified Technology Specialist
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified Business Management Solutions Specialist

    http://www.navisiontech.com
  • Options
    ara3nara3n Member Posts: 9,255
    There is no issue with putting an error. The problem is that you lose all your work.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Options
    gvolkovgvolkov Member Posts: 196
    not if you
    COMMIT;
    
    Microsoft Certified Technology Specialist
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified Business Management Solutions Specialist

    http://www.navisiontech.com
Sign In or Register to comment.