- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on
06-26-2025
02:13 AM
- edited on
07-14-2025
11:44 PM
by
Ashwini Sawanth
ServiceNow® Now Assist for Code is an exciting generative AI tool that transforms the coding experience! By streamlining common coding tasks and embracing best practices, it significantly boosts developer productivity. With its latest updates, developers can generate, edit, explain, and autocomplete code more efficiently than ever, unlocking new possibilities for creativity and innovation!
Install and activate Code Assist skills
- Install or upgrade the Now Assist for Creator parent application.
Now Assist for Code will be installed as a dependency. - Activate the following Now Assist for Code skills in Now Assist Admin:
- Code Generation
- Code AutoComplete
- Code Edit
- Code Explain and Summarize
- (Optional) Choose an LLM:
- Go to Now Assist Admin > Settings > Manage LLMs.
- Scroll to Code Assist and choose a Provider.
Remember that to unlock the powerful skills, you need to be assigned the now.assist.creator role.
When Now Assist for Code is installed and the skills are activated, you'll notice the handy sparkle icon (✨) pop up at the bottom of the script editor. Happy coding!
Code Generation
Supercharge your coding experience with the exciting Code Generation skill! This feature creates new code right where your cursor is, all while expertly respecting the surrounding code. It intelligently takes into account the context of what's already there, both before and after your cursor.
Simply type what kind of code you need, and the tool will provide you with tailored code suggestions.
The best part! Developers of any skill level can use this feature to kickstart their custom code projects or enhance existing scripts more efficiently.
Effective Prompting for Code Generation
The quality of AI-generated code depends on how well the prompt is structured. Clear, specific prompts produce the best results, while vague or overly complex prompts may result in suboptimal suggestions.
Example of a poor prompt:
🧑💻 Return the date and time
This prompt lacks specificity, forcing the model to make assumptions about format, time zone, and user context.
Example of an improved prompt:
🧑💻 Write a function that returns the current date and time of a given user in the format YYYY-MM-DD HH:MM AM/PM specific to the user’s time zone.
This function correctly retrieves the user’s time zone and returns the localized date and time.
Avoiding Overly Complex Chained Prompts
A single prompt that chains multiple tasks may introduce errors or inconsistencies.
For example:
🧑💻 Depending on whether a user is a VIP, set the incident priority to 1 automatically and send an email to "vip@concierge.com" with the details of the incident. Also, send an email to the user who created the incident with the subject "We're on it!"
A better approach is to break it down into sequential tasks, ensuring each part is correctly handled. This way, if one part of the result is incorrect, it doesn't cascade to subsequent results.
Code AutoComplete
The Now Assist for Code (v27.1.2), launched in March 2025, introduces an inline autocompletion feature designed to enhance the coding experience. As you type, the tool predicts and suggests the next logical code snippets.
Utilizing Retrieval Augmented Generation (RAG), the autocompletion feature offers intelligent suggestions based on the user's scripts and tables. Users are encouraged to pause briefly after typing to see the autocompletion feature in action, ultimately making the coding process more efficient.
Example: Code Autocompletion in action
When you begin coding, autocompletion keeps your momentum high by suggesting next steps, streamlining coding tasks, and boosting speed, accuracy, and adherence to best practices.
Code Edit
Introduced in November 2024 with version 26.1.7, Code Edit is a game-changer for developers! This exciting feature empowers you to refine and optimize your ServiceNow scripts with structured improvements, boosting performance, validation, logging, and overall maintainability.
Code Edit can be used to:
- Improve validation by enhancing input checks and adding specificity to regular expression (regex) validation.
- Enhance error handling by providing more descriptive error messages.
- Refactor variable names to ensure consistency, improve meaning, and enhance readability.
- Optimize database performance by refining queries, minimizing inefficient loops, and reducing redundant operations.
- Improve logging and debugging by implementing structured logging to capture errors, execution details, and key outputs.
- Increase maintainability by adding detailed inline comments to facilitate easier collaboration.
Improved validation and error handling
Now Assist for Code helps enforce stricter input validation, regex checks, and error handling to ensure robustness in ServiceNow applications.
Example prompts
🧑💻Add input validation to ensure the user’s email is not empty and follows a valid format.
✨
if (!email || !email. Match(/^[^\s@]+@[^\s@]+\.[^\s@]+$$/)) {
gs.addErrorMessage("Please enter a valid email address.");
return;
}
🧑💻Wrap this code in a try/catch block and verify that user_id and task_id are included in the request body.
var requestBody = JSON.parse(request.body.data);
var taskGr = new GlideRecord('task');
taskGr.get(requestBody.task_id);
// Do something with the task
✨
try {
var requestBody = JSON.parse(request.body.data);
if (!requestBody.user_id || !requestBody.task_id) {
throw new Error("Invalid input: user_id and task_id are required.");
}
// Proceed with logic
var taskGr = new GlideRecord('task');
if (!taskGr.get(requestBody.task_id)) {
throw new Error("Task not found.");
}
// Do something with the task
} catch (e) {
response.setStatus(400);
Refactor variable names
Code Edit can rename variables for better clarity or apply a specific naming convention to enhance readability and maintainability.
Rename Variables for Clarity
🧑💻Improve variable names for readability in this loop.
var gr = new GlideRecord('task');
gr.query();
while (gr.next()) {
var n = gr.number;
gs.info(n);
}
✨
var taskRecord = new GlideRecord('task');
taskRecord.query();
while (taskRecord.next()) {
var taskNumber = taskRecord.number;
gs.info(taskNumber);
}
Apply a specific naming convention
🧑💻Standardize variable names to camelCase.
var UserID = gs.getUserID();
var user_name = gs.getUserName();
✨
var userId = gs.getUserID();
var userName = gs.getUserName();
Optimize database performance
Inefficient database queries can significantly affect performance. Code Edit assists developers in optimizing GlideRecord queries, replacing inefficient loops, and enhancing indexing strategies.
Replace Inefficient Queries with GlideAggregate
A common need is to determine the number of records in a table based on certain criteria. The GlideRecord getRowCount method is widely used, even in out-of-the-box code, but it is inefficient because it must query the database, retrieve all matching records, and then loop through each of them in the code, despite not using the values of those records.
🧑💻 Optimize this code with GlideAggregate
function getCountOfIncidents(state) {
var gr = new GlideRecord('incident');
gr.addQuery('state', state);
gr.query();
return gr.getRowCount();
}
✨
function getCountOfIncidents(state) {
var ga = new GlideAggregate('incident');
ga.addQuery('state', state);
ga.addAggregate('COUNT');
ga.query();
return ga.getAggregate('COUNT');
}
Savvy ServiceNow developers know that a GlideAggregate query is more efficient, but in more complex scenarios, the syntax can be a bit dense and may not be natural for every developer to use frequently. Luckily, Code Edit makes it simple to replace the existing logic of looping through each record with a database aggregate query that returns the count in one efficient step.
This is a highly simplified example, although common, but the performance benefits can be significant. Check out Andrew Barnes' excellent deep dive on the way GlideAggregate works and examples of how it enhances performance.
Extract repeated code into a reusable function
🧑💻 Refactor this date formatting logic into a reusable function
var now = new GlideDateTime();
var formattedNow = now.getDisplayValue();
var tomorrow = new GlideDateTime();
tomorrow.addDaysLocalTime(1);
var formattedTomorrow = tomorrow.getDisplayValue();
✨
function getFormattedDate(offsetDays) {
var dt = new GlideDateTime();
dt.addDaysLocalTime(offsetDays);
return dt.getDisplayValue();
}
var formattedNow = getFormattedDate(0);
var formattedTomorrow = getFormattedDate(1);
Improve logging and debugging
Debugging is faster with structured logging that captures key execution details.
Script execution timing
🧑💻 Add logging to measure the execution time of this script
var gr = new GlideRecord('problem');
gr.query();
while (gr.next()) {
// process problem
}
✨
var start = new Date().getTime();
var gr = new GlideRecord('problem');
gr.query();
while (gr.next()) {
// process problem
}
var end = new Date().getTime();
gs.info("[Execution Time] Problem processing took " + (end - start) + " ms");
Log errors with context
🧑💻 Wrap the loop code in a try/catch and log errors with record context
var gr = new GlideRecord('change_request');
gr.query();
while (gr.next()) {
// risky operation
}
✨
var gr = new GlideRecord('change_request');
gr.query();
while (gr.next()) {
try {
// risky operation
} catch (e) {
gs.error("[Change Error] ID: " + gr.sys_id + ", Number: " + gr.number + ", Error: " + e.message);
}
}
Code Explain and Summarize
With the release of version 27.0.0 in February 2025, Now Assist for Code has introduced two fantastic features: "Summarize Code" and "Explain code in detail"! These tools empower developers to swiftly grasp the essence of scripts, making them especially efficient for tackling complex, large, or legacy code.
"Summarize Code" delivers a concise overview of a script's functionality, while "Explain code in detail" offers an insightful step-by-step breakdown for a deeper understanding.
To use these features, simply highlight the code you need help with, click the Quick Action button, and choose either option to get started!
Enhance code readability with comments
Well-commented code improves maintainability, especially in large projects.
Add clarity to a Scripted REST API
🧑💻 Add comments to explain this Scripted REST API.
var data = JSON.parse(request.body.data);
var user = new GlideRecord('sys_user');
user.get(data.user_id);
response.setBody(user.name);
✨
// Parse the incoming request body to extract user_id
var data = JSON.parse(request.body.data);
// Retrieve the user record from the sys_user table
var user = new GlideRecord('sys_user');
user.get(data.user_id);
// Return the user's name in the response
response.setBody(user.name);
Explain business rule conditions
🧑💻 Add comments to explain the logic in this Business Rule
if (current.state == 3 && current.priority == 1) {
current.escalated = true;
}
✨
// If the incident is in 'On Hold' state and has a high priority,
// mark it as escalated
if (current.state == 3 && current.priority == 1) {
current.escalated = true;
// Code Assist is trained to know what 3 and 1 mean with respect to state and priority
Enjoy coding with confidence!
Additional Resources
Now Assist for Code generation documentation
Share your prompt tips below
If you've discovered other effective ways to achieve a specific goal with your prompts, share them in the comments. We'd love to hear how you're structuring your prompts to maximize the benefits of Generative AI.
- 2,111 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
The way to change the models has been modified. The new instructions are:
1. Now Assist Admin > Settings > Manage Model Providers
2. Select “Model providers” tab > “Edit Model Providers” button
3. Choose “Customize”
1. “Edit provider for skill” and search by “Code” (granular)
2. “Edit provider for skill group” and select “Code Assist”