Now Assist for Code Generation ‐ A Comprehensive Primer - ben-vargas/servicenow-wiki GitHub Wiki
Now Assist for Code Generation is an AI-driven feature within the ServiceNow Platform that empowers developers to write, refine, and maintain code more efficiently. By understanding natural language prompts, it generates contextually relevant JavaScript code directly within the ServiceNow script editor, streamlining your development workflow and enhancing overall code quality.
This guide serves as a one-stop reference for learning, using, and mastering Now Assist for Code Generation, incorporating best practices from official ServiceNow documentation, community insights, and other reputable sources.
Visual Demonstration:
For a quick overview and demonstration of Now Assist’s capabilities, watch the following video:
Key Features
-
Natural Language to Code:
Transform plain English requests into ready-to-use JavaScript. Instead of memorizing APIs and methods, simply describe the desired functionality, and Now Assist will produce structured code. -
Context-Aware Suggestions:
The AI engine factors in your current script context—such as table schemas, class methods, and variables already defined—ensuring generated code fits seamlessly into your existing logic. -
Real-Time Integration with Script Editor:
Embedded directly within the ServiceNow script editor, Now Assist streamlines development by eliminating the need to switch between multiple tools or documentation pages. -
Adaptive for All Skill Levels:
From newcomers who need a guided start to experienced developers seeking quick snippets, Now Assist caters to a broad range of competencies, promoting consistent, high-quality code.
Benefits
-
Accelerated Development: Spend less time writing boilerplate code and searching documentation. Quickly generate and adapt code, allowing more time for strategic problem-solving.
-
Higher Code Quality: Leverage suggestions aligned with ServiceNow’s best practices, reducing syntax errors and ensuring maintainable, standard-compliant code.
-
Simplified Onboarding: Junior developers can learn faster, as code suggestions offer real-time examples of ServiceNow coding patterns and standards.
Getting Started
-
Accessing the Script Editor:
Navigate to the desired script field—such as a Client Script, Business Rule, Script Include, or Fix Script—within your ServiceNow instance. -
Invoking Now Assist:
- Right-Click Method: Inside the script editor, right-click and select "Open Code with Now Assist".
- Keyboard Shortcut:
- Mac: Press
Cmd + Enter
- Windows: Press
Ctrl + Enter
- Mac: Press
-
Entering a Prompt:
In the Now Assist dialog, type a natural language description (up to 1,000 characters) of what you need. For example:
"Retrieve all active incidents assigned to the current user and loop through them." -
Review & Accept the Suggestion:
Now Assist will present a snippet of code that meets your prompt’s criteria. You can:- Accept as-is: Insert the code directly into your script.
- Refine further: Edit the suggested code before saving.
- Reject & Retry: If the code isn’t suitable, provide a clearer or more detailed prompt.
Example Prompts & Generated Code
1. GlideRecord Query
Prompt:
// Retrieve all active incidents assigned to the current user
Generated Code:
var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.addQuery('assigned_to', gs.getUserID());
gr.query();
while (gr.next()) {
// Process each incident
}
2. Business Rule Creation
Prompt:
// When a new incident is created, set the priority to '2 - High' if the category is 'Network'
Generated Code:
(function executeRule(current, previous /*null when async*/) {
if (current.operation() == 'insert' && current.category == 'Network') {
current.priority = '2 - High';
}
})(current, previous);
3. Fix Script for Unlocking Users
Prompt:
// Unlock all user accounts currently locked out
Generated Code:
var gr = new GlideRecord('sys_user');
gr.addQuery('locked_out', true);
gr.query();
while (gr.next()) {
gr.locked_out = false;
gr.update();
}
4. REST Integration
Prompt:
// Make a GET REST call to example.com and capture the response
Generated Code:
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://example.com');
request.setHttpMethod('GET');
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
5. Completing a Switch Statement
Prompt:
// Return the name of the day for a given number (0-6)
Generated Code:
function getDayName(dayNumber) {
switch (dayNumber) {
case 0: return 'Sunday';
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
case 6: return 'Saturday';
default: return 'Invalid day number';
}
}
Best Practices for Effective Prompts
-
Be Specific & Descriptive:
The more context you provide, the better the code suggestion. For example, specify the table name, conditions, or expected behavior. -
Iterate & Refine:
If the initial suggestion isn’t perfect, adjust your prompt. Clarify the logic, add conditions, or mention required variables. -
Validate Before Production:
Always review, test, and, if needed, refactor the generated code. While AI suggestions are often accurate, your environment’s unique conditions may require adjustments. -
Leverage the Tool as a Learning Resource:
Pay attention to the generated code’s structure, syntax, and logic. Over time, you’ll gain a deeper understanding of ServiceNow’s APIs and recommended practices.
Additional Learning Resources
-
Official ServiceNow Documentation:
Now Assist for Code Generation (Xanadu Release) -
ServiceNow Community:
ServiceNow Community Forums
Engage with fellow developers, ask questions, and share insights. -
Video Tutorials & Webinars:
The embedded video above provides a practical demonstration of using Now Assist. Seek additional videos and recorded sessions for more comprehensive walkthroughs. -
Third-Party Blogs & Articles:
Visit your GitHub ServiceNow blog and other reputable developer blogs for nuanced tips, advanced techniques, and detailed deep dives into Now Assist usage.
Conclusion
Now Assist for Code Generation represents a significant step forward in the ServiceNow development lifecycle. By turning natural language prompts into robust, context-aware JavaScript, it empowers developers to speed up coding tasks, maintain consistent standards, and focus on delivering high-quality applications. Keep experimenting, refining prompts, and leveraging this AI tool to enhance your productivity and enrich your coding experience on the Now Platform.