JavaScript in ABAP

By | October 14, 2011 | JavaScript | 10,124 | 7

With introduction to CL_JAVA_SCRIPT, we can now use the Javascript (JS) within ABAP.

Javascript supports dynamic typing in contrast to ABAP. ABAP has variables defined with the TYPE rather than the Value of the variable. In Javascript, we can eveluate any valid statement by passing it as a STRING using the eval function.

How to use Javascript in ABAP:

  • CREATE – This is the factory method of the class CL_JAVA_SCRIPT to generate the instance.
  • EVALUATE – This method is to evalute the supplied SOURCE. This is similar to eval in JS. This method except the JS Source as argument and provides back the return result.
  • LAST_ERROR_MESSAGE & LAST_CONDITION_CODE – These attributes provides us the error message and code, if there is any.

Lets see a sample code:

PROGRAM z_javascript_in_abap.

DATAv_expression TYPE string,
      v_var1 TYPE char10,
      v_var2 TYPE char10.

DATAjs_processor TYPE REF TO cl_java_script,
      js_source TYPE string,
      return_value TYPE string.

* Dynamic Expression
v_var1 1.
v_var2 2.
CONCATENATE v_var1 '+' v_var2 INTO v_expression.
CONDENSE v_expression.

* JS Object
js_processor cl_java_script=>create).

* JS Source
CONCATENATE
  'var string = ' v_expression ';'
  'function evaluate_expression()                 '
  '  { string = eval(string);                     '
  '  }                                            '
  'evaluate_expression();                         '
  'string;                                        '
    INTO js_source SEPARATED BY cl_abap_char_utilities=>cr_lf.

* return after evaluation
return_value js_processor->evaluatejs_source ).

* Error
IF js_processor->last_condition_code IS NOT INITIAL.
  WRITE/'Error message'js_processor->last_error_message,
         /'Error Code'js_processor->last_condition_code.
* Result
ELSE.
  WRITE/'Result of'v_expression ,' is: 'return_value.
ENDIF.

In the JS, we created a Function evaluate_expression. We instantiate the object and call the method EVAL, which inturn calls the JS function and gives back us the result.

The output of this would be like this:

If we make any typos – that we generally do – JS complier will generate the error. Like this:
Code with Typo:

Output of error:

In next post of JS in ABAP, we’ll see Real time usage of JS.

Read More on Javascript in ABAP at SAP Help: ABAP and JAVAScript

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

7 Comments

  • Arun

    Excellent information. Thank you.

  • Shahrin

    Hi Namesh.

    “In next post of JS in ABAP, we’ll see Real time usage of JS.”

    Have you gotten to that yet? I am very interested in the real time capabilities of JS + ABAP

  • Naimesh Patel

    Hello Shahrin,

    No, I haven’t forgotten about that but got busy with some other things. Will post about the same very soon. Stay tuned. If you haven’t subscribed to emails notification, please do that by visiting Subscribe ABAP Help.

    Regards,
    Naimesh Patel

  • Naimesh Patel

    Hello Shahrin,

    I have posted the Case Study on JavaScript usage in ABAP.

    Regards,
    Naimesh Patel

  • […] library in ABAP stack, which can be used to evaluate this. I tried the simple example as posted in JavaScript in ABAP. Finally, I was able to make it […]

  • marcin

    Cool stuff, we just we need that for dynamic expression evaluation. It’s perfectly suited for such occasions.

    Thanks Naimesh,
    marcin

  • Hello Marcin,

    Yes, there are lot of opportunities to use JS for calculations .. But it seems SAP is going to remove CL_JAVA_SCRIPT as per the tweet from Uwe Fetzer. Only the future releases can confirm this….

    Regards,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.