How to restrict onDelete

ckndr47ckndr47 Member Posts: 100
Hello Everybody,

I am new to Navision and i need your help on a delete issue.
So here is my scenario:
I have two tables 1) Department 2) Channel Department

All the departments are linked with Channel Department in other words Channel Department is child table of Department.

So when i delete any department from Department table it doesn't prompts me that a particular department has a association in Channel Department; instead it deletes record from Department table only. Now in order to solve the above issue i have written few lines of code in OnDelete trigger that will delete departments from Channel Department as well.
Here is the code:
ChannelDepartment.SETRANGE("Department Code","Department Code");
ChannelDepartment.DELETEALL;

What i want is a confirm message which will Ask the user to delete to either the record or not. So for that i have written following code in OnDelete trigger
IF CONFIRM(ConfirmDelete,TRUE) THEN BEGIN
 ChannelDepartment.SETRANGE("Department Code","Department Code");
 ChannelDepartment.DELETEALL;
END

Now here in this situation during execution if i say YES then it works fine; but if i say NO then it deletes record from DEPARTMENT table. Tell me how do i resolve this issue.

Thanks and Regards,

Comments

  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    ckndr47 wrote:
    IF CONFIRM(ConfirmDelete,TRUE) THEN BEGIN
     ChannelDepartment.SETRANGE("Department Code","Department Code");
     ChannelDepartment.DELETEALL;
    END
    

    Now here in this situation during execution if i say YES then it works fine; but if i say NO then it deletes record from DEPARTMENT table. Tell me how do i resolve this issue.

    Thanks and Regards,

    That is because the Delete-trigger of the Department-table is still executing properly if you answer NO.

    Try this code:
    IF CONFIRM(ConfirmDelete,TRUE) THEN BEGIN
     ChannelDepartment.SETRANGE("Department Code","Department Code");
     ChannelDepartment.DELETEALL;
    END
    ELSE
      ERROR('')
    
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • David_CoxDavid_Cox Member Posts: 509
    Just to expand this by one level Conditional Delete:

    If you have scenario's where you do not want Lines deleted if they have been updated with some values then:

    In the lines or header OnDelete Trigger put in the test fields, like in the Sales Line and Sales Header

    Header
    TESTFIELD(Closed,TRUE);

    Lines OnDelete Trigger Code
    TESTFIELD("Payment Received",FALSE);
    TESTFIELD("Sales Invoice No.",'');

    Then as Luc wrote

    Header OnDelete trigger Code
    // This will stop the header being deleted if not Closed = True
    TESTFIELD(Closed,TRUE);

    IF CONFIRM(ConfirmDelete,TRUE) THEN BEGIN
    ChannelDepartment.SETRANGE("Department Code","Department Code");
    ChannelDepartment.DELETEALL(TRUE);
    END ELSE
    ERROR('');

    By adding the Testfield and the (TRUE) to the ChannelDepartment.DELETEALL: it will run the OnDelete Code and stop the deletion of Active records that have been actioned :)
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
  • ckndr47ckndr47 Member Posts: 100
    Hay VAN thanks a million; it worked...
    aahan that means else block will stop the tigger in case of NO to message box.

    David thanks for your idea; I think i will be coming on that task after few weeks. So by then i will have problem i will contact you up..

    Its nice to have supportive and knowledgeable people like you at this forum.

    Take Care,
Sign In or Register to comment.