Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Using CL_JAVA_SCRIPT to execute JS files

Former Member
0 Likes
4,129

Hello,

I want to use CL_JAVA_SCRIPT in abap to execute a javascript file which i get from url.

And this is my code :


*

&---------------------------------------------------------------------*

*& Report  ZCLIENT

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT ZCLIENT.

DATA: LV_CLIENT TYPE REF TO IF_HTTP_CLIENT.

DATA:lv_URL TYPE STRING .

lv_URL  = 'https://resources/sap-ui-core.js'.

* Create client

   CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL

     EXPORTING

       URL                = lv_URL

     IMPORTING

       CLIENT             = LV_CLIENT

     EXCEPTIONS

       ARGUMENT_NOT_FOUND = 1

       PLUGIN_NOT_ACTIVE  = 2

       INTERNAL_ERROR     = 3

       OTHERS             = 4.

   IF SY-SUBRC NE 0.

     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

       WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

   ENDIF.

*   LV_CLIENT->REQUEST->SET_CONTENT_TYPE(

*       EXPORTING

*         CONTENT_TYPE = 'application/javascript; charset=UTF-8'

*         ).

   CALL METHOD LV_CLIENT->REQUEST->SET_FORMFIELD_ENCODING

     EXPORTING

       FORMFIELD_ENCODING = LV_CLIENT->REQUEST->CO_ENCODING_URL.

* Get request:

   CALL METHOD LV_CLIENT->SEND

     EXCEPTIONS

       HTTP_COMMUNICATION_FAILURE = 1

       HTTP_INVALID_STATE         = 2

       HTTP_PROCESSING_FAILED     = 3

       OTHERS                     = 4.

   IF SY-SUBRC NE 0.

     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

       WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

   ENDIF.

* Prepare client-receive:

   CALL METHOD LV_CLIENT->RECEIVE

     EXCEPTIONS

       HTTP_COMMUNICATION_FAILURE = 1

       HTTP_INVALID_STATE         = 2

       HTTP_PROCESSING_FAILED     = 3

       OTHERS                     = 4.

* Get HTML:

DATA EV_DATA TYPE STRING.

CONSTANTS co_line_size TYPE i VALUE 150.

TYPES: t_line TYPE c LENGTH co_line_size.

DATA: l_string TYPE string,

      lt_lines TYPE TABLE OF t_line.

FIELD-SYMBOLS: <l_line> TYPE t_line.

   EV_DATA = LV_CLIENT->RESPONSE->GET_CDATA( ).

"   compile js

data GET_VALUE type STRING.

data JS_PROCESSOR type ref to CL_JAVA_SCRIPT.

data SOURCE type STRING.

concatenate  EV_DATA SOURCE into SOURCE SEPARATED BY cl_abap_char_utilities=>cr_lf.

JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).

JS_PROCESSOR->COMPILE( SCRIPT_NAME = 'TEST_GET.JS'

  SCRIPT =  EV_DATA ).

if JS_PROCESSOR->LAST_CONDITION_CODE <> 0.

write: / 'Error in COMPILE',

  JS_PROCESSOR->LAST_ERROR_MESSAGE.

else.

write / 'JavaScript was compiled'.

endif.

skip.

JS_PROCESSOR->EXECUTE(

exporting SCRIPT_NAME = 'TEST_GET.JS' ).

if JS_PROCESSOR->LAST_CONDITION_CODE <> 0.

write: / 'Error in EXECUTE',

  JS_PROCESSOR->LAST_ERROR_MESSAGE.

else.

write / 'JavaScript was executed'.

endif.

skip.

When executing my code i'm getting some errors like : "missing ; at line 16 " however my  js file doesn't contain any errors.

Thank you for help

Ameni,

1 ACCEPTED SOLUTION
Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
1,831

Hi Ameni

The error message says there is a missing ";" at line 16. Either the kernel JS processor may have a more strict check for this where it may not be an "error" in other environments; or, there is really a crucial semi-colon missing which you might have overlooked.

BTW, the class name says "obsolete". So, you may want to check alternative solutions.

Hope this helps.

*-Serdar

4 REPLIES 4
Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
1,832

Hi Ameni

The error message says there is a missing ";" at line 16. Either the kernel JS processor may have a more strict check for this where it may not be an "error" in other environments; or, there is really a crucial semi-colon missing which you might have overlooked.

BTW, the class name says "obsolete". So, you may want to check alternative solutions.

Hope this helps.

*-Serdar

Read only

0 Likes
1,831

Hi Serdar,

Thank you for your reply

have you any alternatives to complie and execute a js file from ABAP ?

Thank you,

Ameni,

Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
1,831

Hi Ameni

Here's the official page advising the support for this class to be ceased:

The Class CL_JAVA_SCRIPT - BC - ABAP und JavaScript - SAP Library

I believe you should check why you need to execute JS in ABAP. If it is related to authoring a web page, you should consider redesigning to properly leverage JS in the page.

If it is for the sake of reusing an existing capability, it seems you should think of accessing it via a different channel or re-implement the capability in ABAP.

Looks like SAP does not want to take the burden of supporting this. And I believe this looks totally reasonable considering its limited use cases.

Hope this helps...

*--Serdar

Read only

Former Member
0 Likes
1,831

In my experience the COMPILE method accepts only source lines with a limited length. Please check, wether your TEST_GET.JS source has a long line at row 16. I don't know the exact limitation, but when i used only source lines with less then 150 characters, it worked fine.

Please also consider, that CL_JAVA_SCRIPT does not support the newest ECMAscript standard.