Upon delete do not want to delete

jwilder
Member Posts: 263
I have a custom table that has a status field on it (Open,Closed,Deleted).
When someone deletes one of these records I want to actually change the Status field to Deleted and not actually delete the record. I put my code on the OnDelete to modify the record but the OnDelete trigger always wants to delete the record. So I can modify it but then it just gets deleted anyway. The EXIT statement does not work on the OnDelete trigger of a table. Thats what I need.
Any thoughts on how to do this?
When someone deletes one of these records I want to actually change the Status field to Deleted and not actually delete the record. I put my code on the OnDelete to modify the record but the OnDelete trigger always wants to delete the record. So I can modify it but then it just gets deleted anyway. The EXIT statement does not work on the OnDelete trigger of a table. Thats what I need.
Any thoughts on how to do this?
Jason Wilder
jwilder@stonewallkitchen.com
jwilder@stonewallkitchen.com
0
Answers
-
Don't think you can do that directly, but you can be sneaky about it:
- Show the user a temporary version of the table
- Override the triggers in the table to test whether it's the temp table or not:
- OnModify, get the real record and update it
- OnDelete, get the real record and mark it as deleted
- OnInsert, simply insert a new record
Here's an example using the Item table. In OnDelete (first code to run):rrRecRef.GETTABLE(Rec); IF rrRecRef.ISTEMPORARY THEN BEGIN recItem.GET("No."); recItem.Description := 'DELETED'; recItem.Blocked := TRUE; recItem.MODIFY; EXIT; END;//if
In OnModify:rrRecRef.GETTABLE(Rec); IF rrRecRef.ISTEMPORARY THEN BEGIN recItem := Rec; recItem.MODIFY; EXIT; END;//if
Etc. To test, just create a form with a temp record for Item and delete a few.0 -
No need to do all this . If its a customised table and DELETE is not called from out side simply put code like this On DELETE
COMMIT will save your change of status and ERROR will stop its deletion
Deleted := TRUE;
MODIFY;
COMMIT;
IF Deleted THEN
ERROR('ok');0 -
Yup that works. I could even change the Error('ok') to Error('') so there is no message to to the user.Jason Wilder
jwilder@stonewallkitchen.com0 -
Yep - that's a way better (and more obvious) solution.0
-
Why do you want to do this? Who approved this design?
A status "Deleted"?
Imagine someone else but you having to do maintenance on this code.
Please reconsider this design and go to something more NAV-like.0 -
Dev wrote:No need to do all this . If its a customised table and DELETE is not called from out side simply put code like this On DELETE
COMMIT will save your change of status and ERROR will stop its deletion
Deleted := TRUE;
MODIFY;
COMMIT;
IF Deleted THEN
ERROR('ok');0 -
This is (as everyone said) a very very dangerous thing to do. Just as an alternative, you can create another table with a suffix or prefix as archive, like NAV has, Sales Header Archive, Sales Line Archive, etc. and put the deleted record into this when it gets deleted from the original table.0
-
Yup, Design looks unhealty , but it depends upon requirement, if such requiremnt comes on mibuso that means the developer had already purt a lot of stuff from his side and even convencing the client. If not succedded then it comes to mibuso. If the developer still changes the design than it goods else pick one of the solution.
Rest if Rec is deleted then it is stoped to actualy get delete. I dont think it can mess some thing as it it on top of the delete triger.
Dev0 -
You could do this easy at the form level, as the form delete accepts a boolean, it is safe and any code calls on the table will not cause problems?
Option 1:Form - OnDeleteRecord() : Boolean Status = Status::Deleted; MODIFY; EXIT(false);
Option 2
Another way at form level which makes it hard for the user to delete by mistake.
Set the form Property DeleteAllowed = No
Add a Button [ Delete ]IF CONFIRM('Mark this record as deleted?',FALSE)THEN BEGIN //Close the Record Status = Status::Deleted; MODIFY; END;
Option 3:
A bit of both confirm or exit!Form - OnDeleteRecord() : Boolean IF CONFIRM('Mark this record as deleted?',FALSE)THEN BEGIN Status = Status::Deleted; MODIFY; END; EXIT(false);
Then you can write a report to delete records at table level, I would also change the option Deleted to Closed!
HTH
DavidAnalyst 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.com0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions