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!

New NetWeaver Information at SAP.com

Very Helpfull

 

 

User Rating: Be the first one !