Overview Modal alerts and confirmations in ServiceNow are interactive user interface elements that allow users to receive important messages or confirm actions before proceeding. These modals enhance the user experience by providing clear feedback during key interactions, ensuring that users are well-informed before performing potentially critical actions, such as closing a record or saving changes.
Two key modal functions used in ServiceNow are:
g_modal.alert(): Displays an informational message to the user. g_modal.confirm(): Requests user confirmation before proceeding with an action. Key Features: Clarity: Provides clear messaging for users, helping them understand the consequences of their actions. User Control: Allows users to confirm or cancel actions, reducing mistakes. Customizable: Modal messages can be tailored to different scenarios, ensuring contextual and relevant feedback. Testing To test the modal alerts and confirmations, follow these steps:
- User Scenario Setup: Create a form with an assigned_to field and ensure that different users can access the form. Set up a button or UI action that triggers the onClick() function.
- Test Case 1: Unauthorized User Log in as a user who is not assigned to the task. Click the button to trigger the action. Expected Result: The user should receive an alert stating "Only the assigned user can perform this action." The process should stop here.
- Test Case 2: Authorized User Log in as the user who is assigned to the task. Click the button to trigger the action. Expected Result: A confirmation dialog should appear asking, "Are you sure you want to take this action?" If the user clicks "Cancel," no action should be taken. If the user clicks "OK," the task's state should be set to "Closed Complete," and the form should be saved.
- Edge Cases: Test what happens if the task has no assigned user. Test if the modals display correctly on different devices (desktop, mobile, etc.).
- Performance: Ensure that the modals load quickly and do not interfere with other form functions. How It Works The modal alert and confirmation functions are built on the ServiceNow UI framework and use JavaScript to control the interactions.
Alert Modal (g_modal.alert()): This function is used to inform the user without requiring any further input. It takes three arguments: the title of the alert, the message to display, and an optional callback function that can execute code after the alert is closed. Once triggered, the user sees a message box with only an "OK" button. Confirmation Modal (g_modal.confirm()): This function prompts the user to confirm their action with two options: "OK" or "Cancel." It takes three arguments: the title of the confirmation modal, the message, and a callback function. The callback function receives a confirmed argument that is true if the user clicks "OK" and false if they click "Cancel." This is useful in scenarios where user confirmation is critical (e.g., deleting a record, submitting a form). Process Flow: The system checks if the logged-in user has permission to perform the action (e.g., checking if the user matches the assigned_to field). If the user is not authorized, an alert modal is displayed, and the process stops. If the user is authorized, a confirmation modal is displayed to ask for final approval. If confirmed, the system proceeds with the action (e.g., changing the record state and saving the form). Benefits
- Improved User Experience: Modal alerts and confirmations provide immediate feedback to users. By using clear language in modals, users understand exactly what actions they can or cannot perform. Confirmation dialogs prevent accidental actions, which is especially useful in critical workflows (e.g., closing or deleting records).
- Reduced Errors: Alerts help users understand why an action is restricted, while confirmations ensure that actions are intentional. This reduces the risk of accidental data changes or loss.
- Increased Control: By requiring confirmation before a critical action is taken, users feel more in control of their tasks. They are prompted to reconsider their choices, which minimizes hasty decisions.
- Customization: Modal alerts and confirmations can be easily customized for various forms and records, allowing tailored feedback depending on the context of the action.
- Asynchronous Operations: Modals are asynchronous, meaning they don't block the user interface while waiting for input. This ensures that other parts of the application continue functioning smoothly.