Skip to Content

Binding between JavaScript and the ABAP program

In the weblog ABAP and JavaScript we have seen how to execute the JavaScript programs in the ABAP editor. In this weblog we will look at how binding is done between JavaScript and the ABAP program. I have put together these two blogs as a quick guide to using JavaScript in ABAP, and the SAP standard Help library has much detailed info in this regard.

Binding allows you to link variables and objects in JavaScript with data objects and references to objects in ABAP.The complex ABAP types (structures, internal tables, and references) have to be mapped in JavaScript as a proxy object of the predefined proxy class SAPAbapStructClass.

Different data types in ABAP are mapped to JavaScript types as described below,

  • Binding to ABAP Elementary Types

  • Binding to ABAP Structures

  • Binding to ABAP Internal Tables

  • Binding to Data References

  • Binding to Object References

Binding to ABAP Elementary Types

The elementary ABAP types can be mapped to elementary JavaScript types.
Example:
DATA INT   TYPE I VALUE 3.

JS_PROCESSOR->BIND( EXPORTING NAME_OBJ  = ‘abap’
                                                          NAME_PROP  = ‘pro’
                                                  CHANGING  DATA  = INT ).
SOURCE = ‘result = abap.pro + 1;’.

Binding to ABAP Structures

The components of the structure form the properties of the proxy object and the Substructures are mapped as separate proxy objects.

Example:
DATA: BEGIN OF ADDRESS,
               FIRST_NAME TYPE STRING,
               SECOND_NAME TYPE STRING,
               BEGIN OF STREET,
                     NAME TYPE STRING,
                     NUMBER(4) TYPE C,
               END OF STREET,
               BEGIN OF CITY,
                     ZIPCODE(5) TYPE N,
                     NAME  TYPE STRING,
               END OF CITY,
           END OF ADDRESS.


JS_PROCESSOR->BIND( EXPORTING NAME_OBJ  = ‘abap’
                                                          NAME_PROP = ‘Adr’
                                                 CHANGING  DATA  = ADDRESS ).
CONCATENATE       ‘abap.Adr.first_name=   “Han”;       ‘
                                ‘abap.Adr.second_name=  “Solo”;      ‘
                                ‘abap.Adr.street.name=  “Woodway”;   ‘
                                ‘abap.Adr.street.number=”666″;       ‘
                                ‘abap.Adr.city.zipcode= “64283”;     ‘
                                ‘abap.Adr.city.name=    “Horse Town”;’
            INTO SOURCE SEPARATED BY CL_ABAP_CHAR_UTILITIES=>CR_LF.

Note: You can specify a case-sensitive name for the JavaScript reference using the NAME_PROP parameter of the BIND method.

Binding to ABAP Internal Tables

The proxy class implements an array with the array elements beginning with the index 0.

When binding to Internal tables the proxy object has:

  • A property ‘length’ containing the current number of lines in the internal table

  • A method ‘appendLine’ allowing you to add new blank lines to the internal table

  • A method ‘deleteLines(idx,count)’ allowing you to delete ‘count’ lines, starting with the line ‘idx’.

Example:
DATA: BEGIN OF LINE,
                COMP TYPE I,
            END OF LINE.
DATA ITAB LIKE STANDARD TABLE OF LINE.

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.

Note: 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.If the internal table in ABAP is reorganized, the binding between these references in JavaScript and ABAP may be lost and an exception raised.

Binding to Data References

The proxy object has a method, ‘notInitial’, with no input parameters, that returns a Boolean value. The return value is true if the ABAP data reference points to a data object, and false if the ABAP data reference is blank.

Example:
TYPES: BEGIN OF T_STRUCT,
               TEXT TYPE STRING,
             END OF T_STRUCT.
DATA DREF TYPE REF TO T_STRUCT.
DATA STRUCT TYPE T_STRUCT.
DATA SOURCE TYPE STRING.

STRUCT-TEXT = ‘Hello’.
GET REFERENCE OF STRUCT INTO DREF.
….
JS_PROCESSOR->BIND( EXPORTING NAME_OBJ  = ‘abap’
                                                           NAME_PROP = ‘Dref’
                                                   CHANGING  DATA = DREF ).
CONCATENATE    ‘if ( abap.Dref.notInitial() )        ‘
                             ‘{ abap.Dref.data.text += ” World!”; }’
                INTO SOURCE.

Binding to Object References

The components of the proxy object are:

  • A predefined property,‘Class’, based on the proxy class SAPAbapCrefClass and implicitly bound to the class of the object.
  • The public instance attributes of the referenced ABAP object as properties (in lower case).
  • A predefined method, ‘invoke’, which allows you to call the public instance methods of the referenced ABAP object.
  • A predefined method, ‘notInitial’, with no input parameters, that returns a Boolean value. The return value is true if the ABAP object reference points to an instance, and false if the ABAP object reference is blank.

Example:
*———————————————————————*
*       CLASS C1 DEFINITION
*———————————————————————*
CLASS C1 DEFINITION.
  PUBLIC SECTION.
    DATA ATTR TYPE STRING.
    METHODS CONSTRUCTOR.
ENDCLASS.                    “C1 DEFINITION
*———————————————————————*
*       CLASS C1 IMPLEMENTATION
*———————————————————————*
CLASS C1 IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    ATTR = ‘Hello ‘.
  ENDMETHOD.                    “CONSTRUCTOR
ENDCLASS.                    “C1 IMPLEMENTATION
DATA OREF TYPE REF TO C1.
DATA RETURN_VALUE TYPE STRING.
DATA SOURCE TYPE STRING.

JS_PROCESSOR->BIND(  EXPORTING
                                                NAME_OBJ  = ‘abap’
                                                NAME_PROP = ‘Ref’
                                         CHANGING
                                                DATA  = OREF ).
CREATE OBJECT OREF.
CONCATENATE  ‘if ( abap.Ref.notInitial() )  ‘
                           ‘{ abap.Ref.attr += ” World!”; }’
               INTO SOURCE.

To report this post you need to login first.

2 Comments

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

  1. David Halitsky
    but not to me … so thanks very much for taking the time to lay it out so clearly.

    If only there were as many JAVA developers fluent in ABAP as there are ABAP developers fluent in JAVA, the world would be a beautiful place.

    Regards
    djh

    (0) 

Leave a Reply