---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# Generate scripts with AI-powered code generation

# Generate scripts with AI-powered code generation {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 minutes to read

Generate scripts from text, code, or a combination of both with AI-powered code generation.

When code generation is enabled on an instance, a Now Assist icon (![Now Assist icon.]()) appears in the script editor.

Developers must be assigned the now.assist.creator role to use code generation.

## Generate scripts from text {#ariaid-title2}

Write scripts quickly with AI-generated code by telling Now Assist what you want the script to do.

### Before you begin

Learn how to write prompts to generate better code suggestions. For more information, see [General guidelines for code generation](https://servicenow-prod.fluidtopics.net/xSHq9VFSZMvsW4~PXfCPBA "Use these general guidelines for code generation to get better code suggestions and create useful and accurate scripts.").

Role required: now.assist.creator

### Procedure

1. Navigate to a form with a script field.  
   For example, to open a script include form, navigate to AllSystem DefinitionScript Includes and select New or enter <kbd class="ph userinput">sys_script_include.do</kbd> in the navigation filter.
2. In the script editor, place your cursor where you want to add code.
3. Right-click and select Open Code with Now Assist or use one of the following keyboard shortcuts:
   * Windows: Ctrl-Enter
   * Mac: Cmd-Enter

   {#generate-scripts-from-text__choices_irg_s5p_qbc}  
   Tip:  
   Select the Help icon (![Help icon.]()) to access the list of relevant keyboard shortcuts.
4. In the Code with Now Assist dialog box, enter text that describes the desired goal of the code to generate.  
   The text you enter must be fewer than 1,000 characters.
5. Press Enter to generate a code suggestion.  
   The code suggestion appears highlighted in the script editor.

6. Review the code suggestion and complete one of the following steps:
   * To include it in your script and make any edits, select Accept.
   * To regenerate a suggestion, revise the text in the dialog box and select the arrow icon (![Arrow icon.]()).
   * To remove it from the script, select Reject.

   {#generate-scripts-from-text__choices_asf_fvp_qbc}  
   When you accept a code suggestion, a line next to the line numbers indicates which code was created by AI and hasn't been edited. If you edit AI-generated code, the line indicator doesn't appear for those lines of code.

   If the code suggestion doesn't meet your requirements, try rephrasing your prompt according to the prompt guidance and generating another code suggestion.
7. Select Submit or Update to save your changes.

## Generate scripts from code {#ariaid-title3}

Write scripts quickly with AI-generated code completion.

### Before you begin

Code completion must be turned on for the instance.

Role required: now.assist.creator

### Procedure

1. Navigate to a form with a script field.  
   For example, to open a script include form, navigate to AllSystem DefinitionScript Includes and select New or enter <kbd class="ph userinput">sys_script_include.do</kbd> in the navigation filter.
2. In a script field, enter code or a combination of text and code:  
   * Enter the beginning of a function or other code to be automatically completed. For example:

         var email = current.getValue('email');
         var regex =  

   * Enter a combination of text in a code comment that describes the desired goal of the code to generate followed by an example of how you want the code to start. For example:

         // Validate emails using regex
         var email = current.getValue('email');
         var regex =  

   {#generate-scripts-from-code__ul_tyz_h2s_mbc}
3. Right-click and select Auto-generate code completion or use one of the following keyboard shortcuts to generate a code suggestion:
   * Windows: Ctrl-Windows logo key-Enter
   * Mac: Ctrl-Cmd-Enter

   {#generate-scripts-from-code__choices_nwx_3kp_qbc}  
   Tip:  
   Select the Help icon (![Help icon.]()) to access the list of relevant keyboard shortcuts.

   The code preceding your cursor must be fewer than 1,000 characters when triggering code generation.

   You can't edit the prompt after triggering the code generation. If you need to edit your prompt before the code suggestion is returned, you can cancel the code generation by pressing the Backspace key.  
   The spinner icon (![Spinner icon.]()) appears while generating a suggestion. The code suggestion appears in the lines following your prompt but isn't added to your script until you accept it.

4. Accept the code to include it in your script or reject it to remove it from the script.
   * Accept: Press the Tab key or the right arrow key. Selecting within the suggested code also accepts the suggestion.
   * Reject: Press the Escape key, left arrow key, or up arrow key. Typing or selecting anywhere outside of the suggested code within the script also removes the suggestion.

   {#generate-scripts-from-code__choices_syf_h2v_xxb}  
   When you accept a code suggestion, a line next to the line numbers indicates which code was created by AI and hasn't been edited. If you edit AI-generated code, the line indicator doesn't appear for those lines of code.

   If the code suggestion doesn't meet your requirements, try modifying your code and generating another code suggestion.
5. Select Submit or Update to save your changes.

*[\>]: and then


