Alpha Video Training
Results 1 to 13 of 13

Thread: How to prevent "default" records from being deleted??

  1. #1
    Member
    Real Name
    Michael Scholin
    Join Date
    May 2007
    Location
    Madison, WI
    Posts
    321

    Default How to prevent "default" records from being deleted??

    I am trying to find a way using Javascript code to run when the delete key is pressed to FIRST check to see if the hidden sql field called "database_default" for that record is checked (true or false).

    If false, then it can delete the record as normal but if true, I want a message window to pop up saying something like "this record is a default record and can't be deleted".

    I see under Grid Properties, "Javascript - System Events"; there is a canDetailViewDeletRecord event but I don't know how to use it.

    Any ideas?

    Thanks!

  2. #2
    Member Bob Moore's Avatar
    Real Name
    Robert Moore
    Join Date
    Jul 2003
    Location
    Boston, MA
    Posts
    443

    Default Re: How to prevent "default" records from being deleted??

    Assuming you are trying to delete from a detail view ...
    Try this in the canDetailViewDeleteRecord event :

    Code:
    // Check value of field Database_Default and do not allow deletion of record if true
    
    var selected = {grid.Object}._getValue('D','DATABASE_DEFAULT',1);
    
    if (selected == 'True'){
       alert('This record is a default record and can not be deleted.');
       return false;
    }

  3. #3
    Member
    Real Name
    Michael Scholin
    Join Date
    May 2007
    Location
    Madison, WI
    Posts
    321

    Default Re: How to prevent "default" records from being deleted??

    Bob,

    I see what you are doing here - excellent idea.

    I must be doing something wrong like missing a comma or something. I have attached a snapshot of my screen error.

    When I run it in browser view from the development station, I get the attached error. When I run it on the website, the JavaScript is ignored and the record is deleted without any messages or errors.

    Can you see anything obvious? I am using the code you wrote.

    Thanks,

    Mike

  4. #4
    Member Bob Moore's Avatar
    Real Name
    Robert Moore
    Join Date
    Jul 2003
    Location
    Boston, MA
    Posts
    443

    Default Re: How to prevent "default" records from being deleted??

    That's a server side event handler, canDeleteRecord. That event is fired after the Javascript on the client.

    Once again I looked at the doc on the canDetailViewDeleteRecord event and I noticed it passes the rowNumber as a parameter. I hadn't thought to look for the row number because you can only show one row in the detail view however, you may need to use this syntax : e.rowNumber to pass in the row value. I'd have to test this out and dinner is on the table!

    Possible Javascript code change to my original code:

    Code:
    var selected = {grid.Object}._getValue('D','DATABASE_DEFAULT',e.rowNumber);

  5. #5
    Volunteer Moderator Peter.Greulich's Avatar
    Real Name
    Peter Greulich
    Join Date
    Apr 2000
    Location
    Boston, MA
    Posts
    11,649

    Default Re: How to prevent "default" records from being deleted??

    Try:

    var selected = {grid.Object}._getValue('D','DATABASE_DEFAULT')

    You don't need a row number in the detail section.

  6. #6
    Member
    Real Name
    Michael Scholin
    Join Date
    May 2007
    Location
    Madison, WI
    Posts
    321

    Default Re: How to prevent "default" records from being deleted??

    That worked! Very cool!

    Here is a what I used:
    HTML Code:
    // Check value of field DATABASE_DEFAULT and do not allow deletion of record if true
    
    var selected = {grid.Object}._getValue('D','DATABASE_DEFAULT');
    
    if (selected == 'True'){
       alert('This record is a default record and can not be deleted.');
       return false;
    }
    Also, for anyone else reading this, the field name needs to be in UPPERCASE - a mistake I made originally until I saw a comment elsewhere in another post (even though Bob clearly had his example in uppercase, I did not notice that!)

    Thank you Bob and Peter - this is an excellent way to test values before a delete!

  7. #7
    Member Bob Moore's Avatar
    Real Name
    Robert Moore
    Join Date
    Jul 2003
    Location
    Boston, MA
    Posts
    443

    Default Re: How to prevent "default" records from being deleted??

    Everyone remember ...

    JavaScript is case sensitive and A5v10 uses UPPER_CASE to reference field names in Javascript!

  8. #8
    Member
    Real Name
    Michael Scholin
    Join Date
    May 2007
    Location
    Madison, WI
    Posts
    321

    Default Re: How to prevent "default" records from being deleted??

    Bob and/or Peter,

    Here is a curious thing - the js is working fine. I went in to change the alert message just a little but no mater what I do, the old message continues to pop up - even if I delete the entire js from the system events.

    I removed all the xx.js.gzcache files in the a5webroot folders, even deleted the a5wcmp and re-published it - but still the old js alert will pop up. I went to several different computers thinking that maybe it is stuck in the cookies or session variables but the same old message on other computers as well.

    Why do you think this is not changing? Or where is this js code going that I can't change it? Very interesting!

  9. #9
    Member Bob Moore's Avatar
    Real Name
    Robert Moore
    Join Date
    Jul 2003
    Location
    Boston, MA
    Posts
    443

    Default Re: How to prevent "default" records from being deleted??

    It's the phantom magical persistent Javascript cache.

    Only kidding ... You either need to republish the grid component or flush the browser cache.

  10. #10
    Member
    Real Name
    Michael Scholin
    Join Date
    May 2007
    Location
    Madison, WI
    Posts
    321

    Talking Re: How to prevent "default" records from being deleted??

    Stupid, Stupid, Stupid! User error . . . of course!!

    OK, I am embarrassed to admit this but hopefully others may learn from this.

    In the beautifully designed dialog that Selwyn created for us to have access to to these internal functions, "I" did not grasp that the Event itself needs to be highlighted first (canDetailViewDeleteRecord in this case) and then type in the JS code . . . for that Event (of course).

    I had done this correctly at first but then tried to experiment with copy and pasting code. However when I pasted it back, I neglected to notice which Event I was on - which explains why I could not effect changes to the "other" correct event, no matter what I did - I was on the wrong Event.

    Anyway, just make sure you have the right Event highlighted on the left column of Events before you add or change JS code!

  11. #11
    "Certified" Alphaholic kingcarol's Avatar
    Real Name
    Carol King
    Join Date
    Jun 2010
    Posts
    1,531

    Default Re: How to prevent "default" records from being deleted??

    OK, now that you all have mastered preventing a deletion based on a value in a field, can you help this newbie write mine? I have a field in the grid named PROD_CODE. I don't want the user to delete the row (or record) if the character value in the field is equal to CHANGE ORDER. I think I have to write this in CanRowDelete system event.

    That's a simple one, I know, but I'm so new I just need to understand the syntax.

    I also have a tougher thing I need to do with that grid, and that is to prevent deletion if the PROD_CODE has been used in another table that does NOT have a relationship (cuz it was used in too many relationships and Access would not let me make a relationship for the particular 'child' table). So, in Access I had a lookup query to check to see if the PROD_CODE value was used in that table and if so, I disabled the delete. I am clueless in Alpha as to how to accomplish this.... but, one step at a time.

  12. #12
    Member
    Real Name
    Wayne
    Join Date
    Aug 2009
    Posts
    129

    Default Re: How to prevent "default" records from being deleted??

    Carol,

    Here is the simplest way to handle that I have found. There are some side benefits as well.

    First, add a button to your row. Call it delete.

    Under client side properties \ show hide, enter the following code:

    alltrim(YourFieldHere)="value to be true"

    Conversely, you can modify above to be the not true value, should be straight forward. Enter the same code in the field below for enable expression

    Go to the update settings pane for your grid and set Delete Record Control to none. You need to do this to take the A5 standard button off, so it now uses the custom one you added.

    Next, go add code to bring the new button to life. In-line javascript, onclick row event. Add the following code:

    {grid.Object}.deleteRow({Grid.RowNumber});

    Now for side benefits.... if you have a field value that when true you want to prevent deleting. Well, its probably safe to say you dont want users editing either. I have used the same on true code added to each columns enable expression. that way if a certain value is true, it essentially locks the record and prevents deletion.

    I am sure some of the A5 code jockies have a more code focused way to do what I have done above, but its simple and straightforward. Hope it works for you.

  13. #13
    "Certified" Alphaholic kingcarol's Avatar
    Real Name
    Carol King
    Join Date
    Jun 2010
    Posts
    1,531

    Default Re: How to prevent "default" records from being deleted??

    That worked beautifully, Wayne! Thank you, thank you, thank you. Using the in line javascript on a row event is much more intuitive to me. Biggest problem I'm having is knowing where to put code for things.

    Only bugaboo I have is that I wanted the grid properties Row Edit Style to be RowOnDemand. But that causes the Delete Record Control to be unavailable in grid properties.... which also causes the A5 standard delete button to show up. I want users to edit rows one at a time because I'm hoping to capture audit info and perhaps give messages particular to values in the row they are editing. HOWEVER, you have made me think and I bet I can create my own edit and submit buttons, too...which means they only work on the row they are on..essentially creating my own RowOnDemand effect. I'm thinking that creating my own edit button will make it easier to look up a value in the line in another table to determine whether it can be edited, OR submitted (same would probably be true with the self-made delete button). more work... off I go.

    Again, thank you for sending me in the right direction!

Similar Threads

  1. A5 Crashing "Default form full. Not all fields will be displayed."
    By fsi in forum Alpha Five Version 9 - Desktop Applications
    Replies: 4
    Last Post: 10-12-2008, 08:56 AM
  2. Prevent multiple sessions (mini-"mizers")
    By John Lemisch in forum Code Archive
    Replies: 1
    Last Post: 08-21-2008, 05:24 PM
  3. Setting Default Values In "New Record" web page
    By perry9999 in forum Application Server Version 8
    Replies: 2
    Last Post: 07-21-2008, 03:47 PM
  4. "Add" and "Save" buttons don't stay on "Self" page
    By Scholin in forum Application Server Version 8
    Replies: 2
    Last Post: 02-05-2008, 08:50 AM
  5. off topic- set default view in "Select Files" dialog box
    By Jay Talbott in forum Alpha Five Version 8
    Replies: 0
    Last Post: 12-01-2007, 12:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •