Write scripts quickly with AI-generated code by providing a prompt with text, code, or a combination of both.

Before you begin

Learn how to write prompts to generate better code suggestions. For more information, see Writing prompts and reviewing code.

Role required: now.assist.creator

About this task

When code generation is enabled on an instance, a Now Assist icon (Now Assist icon.) appears at the top of the script editor.

Procedure

  1. Navigate to a form with a script field.
    For example, to open a script include form, navigate to All > System Definition > Script Includes and select New or enter sys_script_include.do in the navigation filter.
  2. In a script field, enter one of the following types of prompts.
    Table 1. Prompts for code generation
    Prompt type Example
    Text-to-code
    Text in a code comment that describes the goal of the code.

    Code suggestion for the prompt "Validate emails using regex."

    In this example, the prompt is: // Validate emails using regex.

    Code complete
    The beginning of a function to be automatically completed.

    Code completion for the beginning of a function to validate emails using regex.

    In this example, the prompt is:
    var email = current.getValue('email');
    var regex =  
    
    Single-shot (combination of text and code)
    A combination of text in a code comment that describes the goal of the code followed by an example of how you want the code to start.

    Code suggestion for a prompt with both a text comment and the beginning of a function to validate emails using regex.

    In this example, the prompt is:
    // Validate emails using regex
    var email = current.getValue('email');
    var regex =  
    

    In these examples, a developer is looking for help with writing a script that checks whether email addresses are valid using regular expressions before inserting records with email addresses.

  3. Trigger generating a code suggestion using one of the following keyboard shortcuts.

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

    • Windows: Press Control-Enter
    • Mac: Press Command-Enter
    Tip: Select the Help icon (Help icon.) to access the list of relevant keyboard shortcuts.

    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 is not added to your script until you accept it.

    Prompt and code suggestion in the script editor.

  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.
    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.

    Line indicating which lines of code are AI-generated.

    If the code suggestion doesn’t meet your requirements, try rephrasing your prompt according to the prompt guidance and generating a new suggestion.
  5. Select Submit or Update to save your changes.