If your intent is to display a field-level message near the "Start Date" field when a user selects a date, you can achieve this through the use of Client Scripts in ServiceNow, specifically focusing on the onChange script type. Unlike displaying a message at the top of the form, showing a message close to a specific field requires a slightly different approach, namely using the g_form.showFieldMsg() method.
Here's how you can create a Client Script to display a field message when a user selects a "Start Date" in a Change Request form:
1. Navigate to Client Scripts
First, navigate to System Definition > Client Scripts in the ServiceNow navigator/sidebar.
2. Create a New Client Script
Click on the "New" button to create a new Client Script.
3. Configure Your Client Script
Fill out the form with the appropriate details:
- Name: Give your Client Script a descriptive name, e.g., "Show Message on Start Date Selection".
- Table: Choose "Change Request" (usually
change_request), which indicates the table where this script will be applied.
- Type: Select "onChange". This type executes the script when a field value changes on the form.
- Field name: Select the "Start Date" field. This is the field that triggers your script when changed. The internal field name might be something like
start_date (ensure you select the correct field name from your form).
- Script: Here you will write your actual JavaScript code. Below is an example script that displays a message near the "Start Date" field when a user selects a date:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Customize your message
var message = "Please ensure the start date is accurate and double-check any dependencies.";
// This shows an informational message directly below the "Start Date" field
g_form.showFieldMsg('start_date', message, 'info'); // You can use 'info', 'error', or 'warning' as the last parameter
}
- Active: Make sure this option is selected to enable your Client Script.
4. Save
After completing the form and script, click the "Submit" or "Save" button to save your Client Script.
5. Test Your Script
Now, navigate to a Change Request form in your ServiceNow instance and try changing the "Start Date". If everything is set up correctly, you should see your custom message appear right below the "Start Date" field.
Notes
- Ensure that you're referencing the correct field name in
g_form.showFieldMsg(). Replace 'start_date' with the actual field name if different.
- You can tweak the message and the type of message (
'info', 'error', 'warning') according to your requirements.
- Always test your Client Scripts in a sub-production instance before deploying them in production to avoid unexpected behaviors.
This approach helps to guide users at the field level, providing immediate and contextually relevant feedback to ensure data is entered correctly into the form.