Delete a record when validate a field

AlkroAlkro Member Posts: 115
I don't know if is possible this:

I have a list Form. One field called Exclude is editable (Text 50) and in OnValidate for this field in the Form is called a new Function.
This Function is in the Table with a local variable to the same Table (lvrec). This function do this:

Primary Key in table is "Document No.", "Line No."

lvrec.RESET;
lvrec.SETRANGE("Document No.", DocNo);
lvrec.DELETEALL;

When i input a value in Exclude field in Form, records for the same Document No. are deleted but give this error:

The Sale Detail does not exist.
Identification fields and values:
Document No.='DOC0001',Line No.='10000'

I try CurrForm.UPDATE, RESET... But anything is wrong recovering form record...

Any solution?

Answers

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Try to analyze what is happening.

    You have a code in OnValidate trigger. When you enter something on a form the form, at some point is supposed to update the record you have just changed. But before this happens the code on changed field deletes the record which is just about to be updated...
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AlkroAlkro Member Posts: 115
    All this events occurs:

    OnModify - Record
    OnValidate - Form
    Delete Function in Table

    Is possible that OnModify is executed the last? In this case... There is no way to do it?
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Experiment by writing a small function in each trigger writing to some file the trigger and the current object name, and the timestamp. In a pattern: Open a file, write trigger+timestamp, close the file. This will give you a lot of insight on how the things are executed in NAV.

    There is a way to do it.. or two. You do not have control over the order in which triggers are executed. But sometimes you can prevent the trigger from being executed. For example by changing the design and avoid modification of the record just before it is supposed to be deleted.




    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • DenSterDenSter Member Posts: 8,307
    Your code tries to MODIFY a record that you deleted first, so that error message is expected.

    Start by explaining in regular words what you want to accomplish.
  • kvbkvb Member Posts: 107
    edited 2018-02-06
    Good guys wanna teach you how to fish but you want only the fish itself and want it fast, right?)
    I suggest you to run your function not on the OnValidate trigger but on the Button OnRun trigger
    Dont forget to update from after
Sign In or Register to comment.