Skip to Content

This Blog explains how to execute the JavaScript programs in the ABAP editor. In executing the JavaScript programs that have been loaded from external programs or processed in an editor you have written yourself, following are the steps you use in sequence:

  • Creating the JavaScript context.
  • Compiling the JavaScript source.
  • Executing the compiled JavaScript source.

Creating the JavaScript context:

The SAP Basis System contains a class called CL_JAVA_SCRIPT, which implements an API that can be used in ABAP programs as a JavaScript (JS) engine implemented in the SAP kernel. The creation of JavaScript context in ABAP involves below steps:

  1. Declare a reference variable.
  2. Call the CREATE method.

Example:
data JS_PROC type ref to CL_JAVA_SCRIPT.
JS_PROC = CL_JAVA_SCRIPT=>CREATE( ).
….

Note: The CREATE method Creates an object of the class CL_JAVA_SCRIPT and simultaneously creates a context for the JavaScript Engine, which you can use to access the object. You can create any number of contexts in your ABAP program.

Compiling the JavaScript source:

After creation of JavaScript context the next step is to compile the JavaScript source text. The compiling of JavaScript source consists of below steps:

  1. Pass the JavaScript source text in to a string.
  2. Call the COMPILE method.

Example:
data JS_CODE type STRING.
….
concatenate
‘var string = “Hello SDN”;            ‘
‘function Set_String()                  ‘
‘  { string += “, this is a Blog!”; ‘
‘  }                                    ‘
‘Set_String();                          ‘
‘string;                                ‘
into JS_CODE separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.
JS_PROC->COMPILE( SCRIPT_NAME = ‘HELLO_SDN.JS’
                     SCRIPT      = JS_CODE ).
….

Note

  • The Compiled script is stored under the name HELLO_SDN.JS in the current context.
  • The JavaScript individual source text lines in a string are separated from one another by the static constant CL_ABAP_CHAR_UTILITIES=>CR_LF.

Executing the compiled JavaScript source:

The compiled script is executed by calling the method EXECUTE as shown below,

Example:
data RETURN_JS_VAR type STRING.

RETURN_JS_VAR = JS_PROC->EXECUTE( ‘HELLO_SDN.JS’ ).

Here is the complete code,

REPORT ZAZAZ_JAVA_SCRIPT.
data JS_CODE type STRING.
data RETURN_JS_VAR type STRING.
data JS_PROC type ref to CL_JAVA_SCRIPT.
JS_PROC = CL_JAVA_SCRIPT=>CREATE( ).
concatenate
  ‘var string = “Hello SDN”;            ‘
‘function Set_String()                  ‘
  ‘  { string += “, this is a Blog!”; ‘
  ‘  }                                    ‘
  ‘Set_String();                          ‘
  ‘string;                                ‘
into JS_CODE separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.
JS_PROC->COMPILE( SCRIPT_NAME = ‘HELLO_SDN.JS’
                     SCRIPT      = JS_CODE ).
RETURN_JS_VAR = JS_PROC->EXECUTE( ‘HELLO_SDN.JS’ ).
write RETURN_JS_VAR.

When you run the program, the following is displayed:

Hello SDN, this is a Blog!

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

    1. Peter Inotai
      Hi Puru,

      It’s used for the BSP framework, check for example class CL_BSP_PAGE methods like %_JAVASCRIPTINIT or %_JAVASCRIPTRUN.
      CL_JAVA_SCRIPT is just an API for the Javascript engine.

      Best regards,
      Peter

      (0) 
      1. Klaus Ziegler
        Yes, but only for scripted BSP (release 6.10) not using the more modern techniques. In fact the java script engine is more or less obsolete from BSP point of view.
        In general most often the drawback of missing safety at compile time, weak debugging support, etc. will offset the advantages you might get elsewhere.
        Best Regards,
          Klaus
        (0) 

Leave a Reply