Of course, sometimes you’ll want the ability to get rid of a car from your Car List - for those following along, this would give Valerie the flexibility to remove records, which she’ll definitely need as a small business owner. Let’s add that functionality now.
Customize the New / Edit Car screen.
Drag a button object into secButtons and put it to the left of the Save button.
Change the new button's text to “Delete”.
Change the button Background Color to Secondary, then Save the screen.
Now, select down arrow on the Delete button and choose the Click event.
Deleting a record from a table can be tricky, and it’s an action you want to make sure you do correctly. You see, the action you’re about to use is called Delete Records from Table - note the plural on Records . That’s right - this action has the power to delete all the records from your table - which you definitely don’t want to do by accident! So let’s specify what we want to delete.
Search for and add the action Delete Records from Table.
For Select a Table Name, choose for Table Cars. We’ve now specified which table we want to delete from.
Beside Delete Records Where, click the Open Criteria Builder button.
The Criteria Builder is used to help pinpoint what you specifically want to delete in a given table.
Click Add a condition.
In the first dropdown on the first line, choose RecordID. This is the field in the Cars Table.This action will tell Method to cycle through the entire Cars Table looking ONLY at the RecordID field. But what is it looking for? To answer that question, we’ll have to set up a Comparison and second Value. We want the app to find the record in the table that has the same RecordID as the one chosen in the grid.
For the next dropdown, keep the default of Is Equal.
For the third dropdown, search for and select Screen Active Record ID.
A Screen Active Record ID is best explained by thinking about it in terms of caching. If you’ve filled in a form on a website and submitted it, then hit the back button, you see how the fields are still populated by the information you wrote down? That’s sort of how a Screen Active Record ID works. When you select a Record ID in the grid, Method saves that information in memory – the place it’s saved is considered the Screen Active Record ID. This means the 1st value will now be searching for the Active Record ID (which is the one you just clicked!)
Since the RecordID is unique, this action allows Method to isolate the individual record for deletion, rather than deleting every record in the table.
Select the Back link to exit the Criteria builder. The action will look like this:
Include an Internal Note: “Delete car”, but don't close the Action Editor yet.
We’re done with the functionality of the delete button, but since deleting can’t be undone, we might want to put one more step in: a confirmation message asking the user if they really meant to delete this record. You see this a lot in software packages - like when your word processor asks you if you’re sure you don’t want to save before closing. Let’s put one in here.
Click Add Action and search for and select Show Message.
For the Title, type in “Confirm Delete”.
For the Message: “Are you sure you want to delete this car?”
For the Button(s), choose OK/Cancel (OK will compete the deletion, Cancel will close the message and ignore all subsequent actions).
Include an Internal Note: “Confirm Delete”.
Now, as we know, actions are run in order - and we certainly don’t want to delete the record before asking for confirmation!
Drag “Show Message” action, by its line number, above “Delete Records From Table
There is one final thing we need to do. The Delete button shouldn't appear if the page is being used to add a new car! It should only appear if the screen is being used to Edit a car.
This is where we can use the Screen Active Record ID. Remember, when we are editing a car, the Screen Active Record ID will hold the Record ID of the car we're editing. However, if it's 0, then we know the user is not editing a car and we can make the Delete button invisible!
The tricky part is: what Event should this go in? The Screen itself has events, and the event to use is called Active Record Change. This event triggers when the Active Record changes, and it gets changed when it is assigned a Record ID!
First, let's make the Delete Button invisible
Select the Delete Button.
Under Settings, change the Load State to Hidden.
And now the button will no longer be visible. However, we want to make it visible when the user is editing a car!
Select the Page Icon in the top menu to go to the Screen Properties.
Under Actions, select the event Active Record Change.
Add the action, Conditional Statement. This action should be famliar to you, because we used it previously to check Required Fields.
Click the Pencil icon beside the IF statement and fill in:
1st Value: Session - Screen Active Record ID
Comparison: Greater Than
2nd Value: Number - 0
For Internal Note: "Active Record ID > 0 (User editing a car)"
Click Save & Back
If the Active Record ID is greater than zero, this means the user is editing a car.
Select the down arrow beside IF and choose Add Action Below. Find and add Show/Hide Controls action and use these parameters: