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.
1. Customize the New / Edit Car screen.
2. Drag a button object into secButtons and put it to the left of the Save button.
3. Change the new button's text to “Delete”.
4. Change the button Background Color to Secondary, then Save the screen.
5. Now, select down arrow on the Delete button and choose the Click event.
Note: 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.
6. Search for and add the action Delete Records from Table.
7. For Select a Table Name, choose for Table Cars. We’ve now specified which table we want to delete from.
8. Beside Delete Records Where, click the Open Criteria Builder button.
Note: The Criteria Builder is used to help pinpoint what you specifically want to delete in a given table.
9. Click Add a condition.
10. 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.
11. For the next dropdown, keep the default of Is Equal.
12. For the third dropdown, search for and select Screen Active Record ID.
Note: 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.
13. Select the Back link to exit the Criteria builder. The action will look like this:
14. 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.
1. Click Add Action and search for and select Show Message.
2. For the Title, type in “Confirm Delete”.
3. For the Message: “Are you sure you want to delete this car?”
4. For the Button(s), choose OK/Cancel (OK will compete the deletion, Cancel will close the message and ignore all subsequent actions).
5. Include an Internal Note: “Confirm Delete”.
Note: Now, as we know, actions are run in order - and we certainly don’t want to delete the record before asking for confirmation!
6. Drag “Show Message” action, by its line number, above “Delete Records From Table.
7. Close the Action Editor.
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!
1. Select the Page Icon in the top menu to go to the Screen Properties.
2. Under Actions, select the event Active Record Change.
3. Add the action, Conditional Statement. This action should be famliar to you, because we used it previously to check Required Fields.
4. 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
5. If the Active Record ID is greater than zero, this means the user is editing a car.
6. Select the down arrow beside IF and choose Add Action Below. Find and add Show/Hide Controlsaction and use these parameters:
- Control*: Delete
- Option: Show
- Internal Note: "Show Delete Button"
- Click Save & Back
7. We're done! Click Save & Close > Save > Close
And we now have a functional delete button!