Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,114 @@
# Field Color-Coding Based on Choice Values
# 🎨 Dynamic Field Background Color Based on Choice Value

## Purpose
Dynamically change the background color of any choice field on a form based on the selected backend value.
## 📁 Location
**Category:** `Client-Side Components`
**Subcategory:** `Client Scripts`
**Snippet Folder:** `Field Background Color OnChange`

## How to Use
1. Create an OnChange client script on the desired choice field.
2. Replace `'your_field_name'` in the script with your actual field name.
3. Update the `colorMap` with relevant backend choice values and colors.
4. Save and test on the form.
---

## Key Points
- Works with any choice field
- Uses backend values of choices for mapping colors.
## 📌 Description

## Demo
This client-side `onChange` script dynamically updates the **background color** of a **choice field** based on the selected **backend value**.

<img width="1710" height="557" alt="image" src="https://github.com/user-attachments/assets/9fb9e68a-1ade-4eb5-81cc-c947c970bd6f" />
This visual enhancement improves form usability by making key field states (like priority or status) more immediately recognizable, reducing user error and boosting efficiency.

---

## 🚀 Features

- ✅ Uses `g_form.getControl()` to access the field’s DOM element
- ✅ Color-codes based on backend values, not display labels
- ✅ Easy to customize — works with any choice field
- ✅ Executes only on field value change (not on form load)
- ✅ Improves UX with real-time visual feedback

---

## 📄 Script: `setColor.js`

```javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;

// Map backend choice values to colors
var colorMap = {
'1': '#e74c3c', // Critical - Red
'2': '#e67e22', // High - Orange
'3': '#f1c40f', // Moderate - Yellow
'4': '#3498db', // Low - Blue
'5': '#27ae60' // Planning - Green
};

// Replace 'priority' with your actual field name
var fieldControl = g_form.getControl('priority');
if (!fieldControl) return;

fieldControl.style.backgroundColor = colorMap[newValue] || '';
}

🛠️ How to Use

1) Create a Client Script in ServiceNow:

Type: onChange
Field: e.g., priority
Table: Your target table (e.g., incident or task)
Script: Use the code provided in setColor.js

2) Customize if needed:

Change 'priority' to the name of your field
Modify the colorMap values to match your field’s backend values and desired colors.


📸 Example Use Case

You're building a form where priority is a required field. You want high-priority issues to stand out visually:

1) Critical (value: 1) turns the field red
2) High (value: 2) turns it orange
3) Low or Planning priorities show in blue or green

This helps agents recognize and prioritize tasks more quickly.

📂 File Structure

Client-Side Components/
└── Client Scripts/
└── Field Background Color OnChange/
├── README.md
└── setColor.js

✅ Requirements Checklist

✔️ Script is in a properly named snippet folder
✔️ Code is relevant and useful to ServiceNow developers
✔️ No XML exports or platform-specific data
✔️ README.md included with:

Description
Usage instructions
Example code
Folder structure

✔️ No use of sensitive data or global variables
✔️ Works with standard choice fields in the platform


👨‍💻 Author

Contributor: @Shweyy123
Pull Request: #1845
Script Name: setColor.js
Compatibility: Works with most ServiceNow instances (Orlando and later)

📘 License

This code is provided under the MIT License. Use at your own discretion in production environments. Always test in a sub-production instance first.

🧩 Additional Ideas (Optional Enhancements)

1) Show display values alongside color mapping
2) Add a tooltip with the priority name
3) Extend support for multiple fields (e.g., priority + state)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return; // Skip when loading the form

// Map backend choice values to colors
var colorMap = {
'1': '#e74c3c', // Critical - strong red
'2': '#e67e22', // High - bright orange
Expand All @@ -8,8 +10,10 @@ function onChange(control, oldValue, newValue, isLoading) {
'5': '#27ae60' // Planning - green
};

var priorityField = g_form.getControl('priority');
if (!priorityField) return;
// Replace 'priority' with your field name
var fieldControl = g_form.getControl('priority');
if (!fieldControl) return;

priorityField.style.backgroundColor = colorMap[newValue] || '';
// Apply background color based on selected value
fieldControl.style.backgroundColor = colorMap[newValue] || '';
}
Loading