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.
DATA: v_expression TYPE string,
v_var1 TYPE char10,
v_var2 TYPE char10.
DATA: js_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->evaluate( js_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:
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
Excellent information. Thank you.
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
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
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 […]
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