Working in the SAP Analytics Cloud Script Editor

Objective

After completing this lesson, you will be able to access and perform basic tasks in the Script Editor in Advanced Mode.

Introduction to the Script Editor

Scripting Language

SAP Analytics Cloud uses a subset of JavaScript for enhancing stories using scripting.

Why is JavaScript used?

  • It can be executed on browser and server
  • It’s relatively easy to learn
  • It’s sufficiently powerful

Why does SAP Analytics Cloud use a subset of JavaScript?

  • Full JavaScript is quite complex
  • JavaScript does not offer type support
  • Some constructs in JavaScript can be dangerous (for example endless loops)
  • SAP Analytics Cloud can validate all scripts

How does scripting in SAP Analytics Cloud differ from using JavaScript in general?

  • Script must be strongly typed
  • Automatic type casting is not offered

Due to the rich code completion capabilities available in SAP Analytics Cloud, the JavaScript subset must be strongly typed. If you want to use an integer as a string, you must explicitly cast it. What this means is, once you have a duck, it remains a duck. A consequence of strong typing is that you can't expect automatic conversions. With strong typing, you can't recycle variable names as new types. So, if you want something else, you need another variable.

In general, using JavaScript offers weak typing and dynamic typing:

  • With weak typing, the script writer can implicitly coerce variables to act like different types. For example, you can have an integer value and treat it as if it's a string: "Dear integer, you are a string". For more information on weak typing, you can go to Code Basics
  • With dynamic typing, the runtime tries to guess the type from the context at that moment. You can even change the type after the variable is already in use. For example, you can change the value of the previously mentioned integer to another type of object: "Dear integer, you're now a duck".

Even though SAP Analytics Cloud uses as subset of JavaScript as its scripting language, it is still JavaScript. You can write valid JavaScript while treating the language as if it's strongly and statically typed.

Knowing that some more advanced JavaScript features are disabled in SAP Analytics Cloud, being a true JavaScript subset still allows for native execution in a browser. Scripts used in SAP Analytics Cloud’s subset of JavaScript are either tied to events or global script objects and are run and validated against "strict mode".

For more information on strict mode, you can go to: Strict mode - JavaScript

Data Types in SAP Analytics Cloud

All programming languages have the concept of Data Types.

In SAP Analytics Cloud, we have:

  1. Primitive data types (like in JavaScript)
    • Integer: 2, 8, 6+4
    • String: "Hello", "I’m" + "here"
    • Boolean: true, false, true && false, "a" == "a", 1 !=2
  2. BI-specific data types:
    • Widget
    • Dimension
    • Member
    • Filter, etc.

There is no automatic type casting.

The Script Editor

The SAP Analytics Cloud Script Editor lets you write scripts for each widget, creating interactive and highly custom-defined stories.

Scripting offers several options for writing code to enhance a story in SAP Analytics Cloud. For example, you can:

  • Place a Button widget in the story and assign a script to the button’s onClick event,
  • Assign script to a chart using its onSelect event,
  • Write scripts based on other system events such as the onInitialization event of the story, or
  • Add scripts that execute whenever data is changed using onResultChanged.
Script Editor open for Button_2018.

The code completion and value help functionality in SAP Analytics Cloud allows for:

  • Code completion in the traditional sense like completing local or global identifiers
  • Semantic code completion by suggesting member functions or similar
  • Value help in the form of context-aware value proposals like measures of a data source for function parameters

For example, when calling an API method on an SAP Business Warehouse query, the code completion can propose measures as code completion options or values to specify a filter.

To open code completion in the script editor, use the keyboard shortcut Crtl + Space.

Code completion in SAP Analytics Cloud script editor. Remember, the most Important Shortcut on the Keyboard is Crtl + Space.

Watch this short video to find out more about the script editor, automatic syntax check, code completion, and value help.

Comment Types

Examples of Single Line Multi Line comments. Examples of both types of comments are provided below.

You can enhance your script with comments. Comments help you or others to create documentation about the script or single parts of the script. JavaScript syntax offers the following two kinds of comments:

  1. Single-line comments
  2. Multi line comments

Single-line comments start with//.

Any text between // and the end of the code line is interpreted as a comment and isn’t executed as a command. You can either use it to generate a whole line as a comment but you can also add it to an existing line of code.

For example:

var counter = 1; // declare counter and assign value 1

or

var counter = 1;

// declare counter and assign value 1

Multi line comments start with/*and end with*/.

Text between /* and */ is interpreted as a comment and isn't executed as a command.

For example:

var counter = 1;

/* declare counter and

assign value 1 */

Statement Types

Diagram with Statement Types on top with arrows leading to each of the boxes below. From left to right: Call Statements, Conditional Execution Statements, Loops, and Assignment Statements. Examples of each of the lower boxes are provided in the table below.
Call StatementsConditional Execution StatementsLoopsAssignment Statements
For example, change the Chart Type of a chart or set a filter for a dimensionIF xxx THEN xxx ELSEFOR…Define a new variable or assign a value to an existing one.
Basic syntax: <Widget>.<Method>(<Arguments>);SWITCH CASEWHILE…var <variable> = <expression>;
For/IN...or

<variable> = <expression>;

SAP Analytics Cloud can use delivered APIs and common JavaScript options for different statement types. These statement types are covered in more detail in the following lessons of this course.

AI-Assisted Story Code Generation

Introduction to AI-Assisted Story Code Generation

SAP Analytics Cloud offers an innovative feature called AI-assisted story code generation, designed to empower story developers. This functionality allows you to generate scripts within the script editor by simply providing a natural language query. Instead of writing complex code from scratch, you can describe the desired script behavior, and the AI will produce the corresponding code. This capability significantly speeds up the development process, reduces the need for extensive scripting knowledge, and helps in quickly prototyping and implementing interactive elements in your stories.

Step-by-Step Guide: Generating Story Scripts with AI

Generating story scripts using AI assistance in SAP Analytics Cloud is a straightforward process integrated directly into your development workflow. Follow these steps to leverage this powerful feature:

  1. Access the Script Editor: Begin by opening the script editor of the specific object within your story to which you intend to assign a script. This is where you will interact with the AI assistant.

    The Script editor is open and ready for entering or modifying script code
  2. Initiate Inline Chat: Once in the script editor, locate and select the Inline Chat icon. This action activates the AI-assisted code generation interface.

    Script Editor showing the Inline Chat button in the upper-right corner of the editor.
  3. Enter Your Query: In the banner that appears, type your request or query in natural language, clearly describing the script you want the AI to generate. Be as specific as possible to get the most accurate results.

    Script Editor with the Inline Chat input field displaying a script generation request.
  4. Send Your Query: After entering your query, select the Send icon to submit your request to the AI. The script generation process will then begin.

    Script Editor with the Inline Chat input field displaying a script generation request and the Send button
  5. Review and Act on Generated Script: Once the AI completes the generation, the proposed script will appear highlighted within your script editor. An AI notice text indicating "Created with AI." will be visible, along with Accept and Reject buttons. Carefully review the generated script to ensure it meets your requirements and functions as intended.

    Script Editor displaying AI-generated code highlighted in the editor, with a “Created with AI” indicator and Accept and Reject buttons. Users can review the generated script and choose whether to apply or discard it.
  6. Accept or Reject the Script:
    1. If the generated script is satisfactory, select Accept. The script will then become a permanent part of your overall script and will no longer be highlighted.

    2. If the script does not meet your needs, select Reject. You can then refine your query and try generating the script again.

  7. Continue or Exit: You can continue to ask the AI to generate additional scripts within the same session. To exit the AI-assisted story code generation interface, simply select the Close icon in the banner.

    Script Editor displaying an AI-generated script and Inline Chat panel, with the Close icon

Prerequisites: Permissions for AI-Assisted Scripting

To effectively utilize the AI-assisted story code generation feature, specific permissions must be granted to your user account. It is crucial to have these in place to ensure you can access and use the functionality without interruption.

  • Execute Permission for Generative AI: You must possess the Execute permission for the Generative AI object type. This permission is fundamental for enabling the AI-assisted story code generation capability itself. If you lack this permission, you will not be able to initiate or use the AI feature.

  • Application Creator Role or Create Permission for Applications: To edit scripts within the script editor, which is where the AI generation takes place, you need either the Application Creator role or the Create permission for the Applications object type. These permissions allow you to modify and save the generated scripts within your story.

If you encounter issues accessing these features, contact your administrator to ensure you have the necessary roles and permissions assigned.

Key Considerations and Restrictions

While AI-assisted story code generation is a powerful tool, it's important to be aware of its current limitations and specific considerations:

  • ODataService Not Supported: The script generation feature does not support generating code for ODataService objects. If your scripting needs involve ODataService, manual coding will still be required.

  • Placeholder Values for Identifiers: Generated code often includes placeholder values for dimension and member identifiers. It is essential that you replace these placeholders with the actual technical IDs from your specific data model before attempting to run the script. Failing to do so will result in errors.

  • No New Object Creation: The AI is designed to generate script code, but it does not support creating new objects within your SAP Analytics Cloud environment. For example, if you ask the AI to create a new script variable, this request will be rejected. The AI focuses on generating logic for existing structures.

  • Limited Access to Event Handler Parameters: Script generation does not have direct access to event handler parameters. This means that generating code that references parameters, such as status in an onExecutionStatusUpdate event, may not function correctly or might require manual adjustment after generation.

Summary

  • AI-assisted story code generation streamlines script creation in SAP Analytics Cloud's script editor.

  • The process involves querying an AI with natural language and reviewing the generated code before acceptance.

  • Specific permissions, including Execute for the Generative AI object type, are required to use this feature.

  • Limitations exist, such as using placeholder values for identifiers and restrictions on creating new objects or accessing event handler parameters.