Entering content frameThis graphic is explained in the accompanying text Binding to Internal Tables Locate the document in its SAP Library structure

An internal standard table is mapped in JavaScript as a proxy object of the predefined proxy class SAPAbapItabClass. The proxy class implements an array. You can specify a – case-sensitive – name (proxy) for the JavaScript reference using the NAME_PROP parameter of the BIND method. Counting the array elements begins with the index 0.

You can access a line of the internal table that has the index ‘index’ using ‘proxy[index]’.

The proxy object has:

If the line type of the internal table is structured, the components of the structure form the properties of the proxy object, whereby the ABAP name is passed in lower case.

You cannot use the other possibility in JavaScript, where you create new array elements by addressing entries that are not available. As usual in JavaScript, counting the elements of the array begins with 0. You cannot add other properties or methods to the proxy object in JavaScript. As with structures, you cannot change an internal table as a whole in JavaScript. If the key fields of a table with a unique key are modifed in JavaScript and the uniqueness of the key is violated, a runtime error may occur.

Example

report DEMO_JAVA_SCRIPT_BIND_ITAB.

data: begin of LINE,
        COMP type I,
      end of LINE.

data ITAB like standard table of LINE.

data SOURCE type STRING.
data JS_PROCESSOR type ref to CL_JAVA_SCRIPT.

JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).

JS_PROCESSOR->BIND( exporting NAME_OBJ  = 'abap'
                              NAME_PROP = 'Itab'
                    changing  DATA      = ITAB ).

do 5 times.
  LINE-COMP = SY-INDEX.
  append LINE to ITAB.
enddo.

concatenate 'var len = abap.Itab.length;           '
            'var sum = 0;                          '
            'for (var i = 0; i < len; i++)         '
            '{                                     '
            '  sum += Number(abap.Itab[i].comp)    '
            '}                                     '
            'abap.Itab.appendLine();                '
            'len++;                                '
            'abap.Itab.appendLine();                '
            'len++;                                '
            'abap.Itab[len-1].comp=sum;             '
   into SOURCE separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.

JS_PROCESSOR->EVALUATE( JAVA_SCRIPT = SOURCE ).

loop at ITAB into LINE.
  write / LINE-COMP.
endloop.

This example shows how you can access a structured internal table in JavaScript. It would also be possible to access an internal table with an elementary ABAP data type here, whereby the ‘comp’ specification would simply be omitted in JavaScript. The JavaScript program calculates the total number of entries, appends two blank lines to the internal table and fills the last entry with this total. After the EVALUATE method has been called, the internal table in the ABAP program contains seven lines. Note also that the internal table in the ABAP program is filled after the BIND method has been called. JavaScript always works with the data that is current in ABAP.

Process with care, if you want to assign elementary properties of the array object in JavaScript to other JavaScript variables. If the internal table in ABAP is reorganized, the binding between these references in JavaScript and ABAP may be lost and an exception raised. Example:

Example

report DEMO_JAVA_SCRIPT_BIND_ITAB.

data: begin of LINE,
        COMP type I,
      end of LINE.

data ITAB like standard table of LINE.

data SOURCE type STRING.
data RETURN_VALUE type STRING.

data JS_PROCESSOR type ref to CL_JAVA_SCRIPT.
JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).

JS_PROCESSOR->BIND( DATA      = ITAB
                    NAME_OBJ  = 'abap'
                    NAME_PROP = 'Itab' ).

do 5 times.
  LINE-COMP = SY-INDEX.
  append LINE to ITAB.
enddo.

concatenate 'var pointer = abap.Itab[4];          '
            'pointer.comp;                        '
  into SOURCE separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.

RETURN_VALUE = JS_PROCESSOR->EVALUATE( JAVA_SCRIPT = SOURCE ).
message return_value TYPE 'I'.

clear ITAB.

do 5 times.
  LINE-COMP = SY-INDEX ** 2.
  append LINE to ITAB.
enddo.

SOURCE = 'pointer.comp;'.

RETURN_VALUE = JS_PROCESSOR->EVALUATE( JAVA_SCRIPT = SOURCE ).

The first time the method is called, it returns the value ‘5’. However, the second time, it causes an exception that cannot be handled. This exception has the runtime error SYSTEM_DATA_ALREADY_FREE assigned to it, since the JavaScript reference ‘pointer’ points to an undefined memory area.

 

 

Leaving content frame