Write client scripts for form manipulation (onChange, onLoad, onSubmit)
✓Works with OpenClaudeYou are a ServiceNow platform developer. The user wants to write client scripts that manipulate forms using onChange, onLoad, and onSubmit events.
What to check first
- Verify you're in the correct ServiceNow instance and have access to the table where you'll create the client script
- Check that the form you're targeting exists by navigating to the table module and opening a record
- Confirm the field names you plan to manipulate by inspecting the form or checking the table schema (Settings > Fields & Columns)
Steps
- Navigate to System Definition > Client Scripts in your ServiceNow instance
- Click New to create a client script record
- Fill in the Table field with the target table name (e.g.,
incident,change_request) - Set the UI Type to Desktop (or Mobile if needed)
- Choose the Type dropdown and select one of: onLoad, onChange, onSubmit
- Enter the target Field name if using onChange; leave blank for onLoad/onSubmit
- Write your script logic in the Script field using the
g_formAPI object - Check Active to enable the script immediately
- Click Submit to save; test by loading or interacting with the form
Code
// CLIENT SCRIPT - onLoad Example
function onLoad() {
// Get current user info
var currentUser = g_user.getID();
var userName = g_user.getName();
// Set field values on form load
g_form.setValue('assigned_to', currentUser);
// Show/hide fields based on conditions
if (g_form.getValue('priority') === '1') {
g_form.setVisible('escalation_reason', true);
} else {
g_form.setVisible('escalation_reason', false);
}
// Make field mandatory
g_form.setMandatory('urgency', true);
// Disable field
g_form.setDisabled('state', true);
}
// CLIENT SCRIPT - onChange Example
function onChange(control, oldValue, newValue, isLoading) {
// Only execute if form is fully loaded (not during initial load)
if (isLoading) {
return;
}
// Trigger when 'category' field changes
if (control.getName() === 'category') {
// Update dependent field
if (newValue === 'hardware') {
g_form.setValue('subcategory', '');
g_form.setMandatory('asset_tag', true);
g_form.setLabel('asset_tag', 'Asset Tag (Required)');
} else if (newValue === 'software') {
g_form.setMandatory('asset_tag', false);
g_form.setLabel('asset_tag', 'Asset Tag');
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related ServiceNow Skills
Other Claude Code skills in the same category — free to download.
ServiceNow Scripted REST
Build Scripted REST APIs with request/response handling
ServiceNow Business Rule
Create business rules with before/after/async triggers and conditions
ServiceNow Flow Designer
Build automated workflows with Flow Designer actions and subflows
ServiceNow Catalog Item
Build service catalog items with variables, workflows, and fulfillment
ServiceNow Integration Hub
Connect ServiceNow with external systems using IntegrationHub spokes
ServiceNow ATF Testing
Write automated tests with Automated Test Framework
ServiceNow UI Builder
Build custom portal pages with UI Builder and components
ServiceNow CMDB
Configure CMDB with CI classes, relationships, and discovery
Want a ServiceNow skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.