ServiceNow Developer Program Code Snippets
repository·main·Indexed 19 days ago
https://github.com/servicenowdevprogram/code-snippetsA community-driven repository of ServiceNow code snippets covering core APIs, modern development, and integrations. It provides reference implementations for developers, including Catalog Client Scripts for auto-saving drafts, autopopulating fields from URL parameters or user records, managing Multi-Row Variable Sets (MRVS), and implementing dynamic approval chain previews.
What's inside servicenowdevprogram-code-snippets
- The Similarity Calculator is a utility designed to perform manual similarity scoring between ServiceNow incident records using text analysis. It allows developers and admins to programmatically compare incident descriptions and calculate similarity scores (0-100%) based on keyword overlap. This utility is useful for finding related tickets without the need for Machine Learning (ML) or the ServiceNow Predictive Intelligence engine.
Overview of the Cross-Table Dependency Analyzer
mainThe Cross-Table Dependency Analyzer is a custom ServiceNow solution used to dynamically detect and analyze dependencies across multiple tables (e.g., Incidents, Problems, Changes, and Configuration Items).
It is designed to provide real-time, developer-driven visibility into how updates to a single record might impact related records across the system. This helps in risk mitigation and proactive management of complex IT processes, offering more customization than standard out-of-the-box (OOB) impact analysis tools.
Overview of Dynamic Field Population from CMDB
mainDynamic Field Population from CMDB is a reusable ServiceNow solution designed to automatically populate key fields on ITSM records (such as Incidents, Tasks, or Change Requests) based on the selected Configuration Item (CI).
By fetching data like Owner, Assignment Group, Location, Department, and Business Service directly from the CMDB, the solution reduces manual data entry, ensures data consistency, and improves ITSM efficiency. It is designed to be extensible across multiple tables and can be integrated with the Service Portal using
GlideAjax.Overview of ServiceNow Change Schedule Enhancement
mainThis customization extends the standard ServiceNow Change Schedule (Change Calendar) to provide increased visibility and interactivity. It is designed for Change Managers, CAB members, and ITSM users to better plan and view changes.
Key features include:
- Short Description Column: Adds visibility to the change's purpose directly in the schedule view.
- Configurable UI: Allows users to toggle the visibility of specific columns, including Configuration Item, Short Description, and Duration.
- Dynamic Refresh: Includes a Change Schedule button to refresh and display changes dynamically.
- Enhanced Data Services: Integrates additional services to fetch and render change records with more detail.
Overview of the Emotion-Aware Ticket Prioritizer
mainThe Emotion-Aware Ticket Prioritizer is an AI-driven solution for ServiceNow designed to analyze the tone and emotion of user-submitted tickets (such as Incidents or HR Cases). It detects the emotional state of the user to dynamically adjust ticket priority, append contextual work notes, and optimize routing to ensure urgent or frustrated users receive faster resolution.Overview of Catalog Item Explorer Widget
mainThe Catalog Item Explorer is a ServiceNow Service Portal widget designed to improve catalog navigation. It allows users to browse catalog items alphabetically, use pagination for large datasets, and perform quick searches via keywords. Beyond its functional use, it is intended as a learning resource for developers, containing best practices and extensive comments to demonstrate advanced widget development techniques.Overview of the SmartData utility
mainSmartData is a lightweight, reusable ServiceNow Script Include designed to automatically select between
GlideAggregateandGlideRecordbased on the developer's intent. This abstraction allows you to use a single API to perform various data operations efficiently, using the most performant engine for each task.Key features include:
- Automatic Engine Selection: Switches between
GlideAggregate(for counts, stats, and distinct values) andGlideRecord(for retrieving specific records or lists). - Helper Methods: Includes
describe(table)for schema inspection andpreview(table, query)for quick data peeks. - Client-Callable Support: Includes a
SmartDataAjaxwrapper for use in Client Scripts and UI Actions. - Security: Uses
GlideStringUtil.escapeQueryTermSeparatorto sanitize queries and prevent malformed or injected encoded queries.
- Automatic Engine Selection: Switches between
Use the MultiSelect widget in Service Portal
mainThe MultiSelect widget allows users to select multiple incident records directly within a Service Portal page. This is a custom widget implementation designed for portal-based incident management.
/* This widget is used to enable multiple incident selection in the Service Portal. */Cleanup Orphaned and Stale Workflow Contexts
mainThis maintenance script identifies and cancels stale or orphaned workflow contexts in thewf_contexttable. It is designed to resolve issues where workflows remain in an 'Executing' state indefinitely due to deleted parent records, design flaws, or system errors, which can otherwise impact performance and reporting accuracy.Implement an FAQ Dropdown Widget in ServiceNow Portal
mainThe FAQ Dropdown Widget is a ServiceNow Portal component that provides an interactive accordion-style interface. Users can click on questions to toggle the visibility of their corresponding answers. It features animated arrow indicators to provide visual feedback on the state of the dropdown.Use Performance Analytics Utils for custom PA aggregations
mainThePerformance Analytics UtilsScript Include provides methods for handling Performance Analytics subjects. It is designed to be invoked by PA Scripts, such as those used for custom aggregations or breakdown mappings.Understand the Sample Incident GraphQL Implementation
mainThis snippet provides a basic implementation of an incident details schema using the ServiceNow GraphQL API. It is designed as a learning resource to demonstrate how to structure a GraphQL implementation within ServiceNow. The implementation consists of three core components:
- Schema (
schema.gql): Defines the GraphQL types, queries, and structure for incident data. - Scripted Resolvers (
.js): Contains the JavaScript logic used to fetch and resolve the data requested in the schema. - Resolver Mappings (JSON): A configuration file that maps GraphQL paths to their corresponding scripted resolvers.
1. schema (schema.gql) 2. scripted resolvers (.js extension) 3. json file representing the resolver mappings (key is Path, value is Resolver)- Schema (