Here’s the sample code on using JavaScript in ABAP.
ZREPORT_JSCRIPT_ABAP.
data: l_JS_RESULT type ref to CL_JAVA_SCRIPT,
l_RET_VAL type STRING,
l_SRC type STRING.
* Create new JavaScript Instance
l_JS_RESULT = CL_JAVA_SCRIPT=>CREATE( ).
* Put Your JavaScript here
concatenate
'var l_SRC = "Hello World,"; '
'l_SRC += " I''m"; '
'l_SRC += " Generated From JavaScript!"; '
'l_SRC; '
into l_SRC separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.
* Compiling the JavaScript
l_JS_RESULT->COMPILE( SCRIPT_NAME = 'FSP.JS' SCRIPT = l_SRC ).
* Check if there's any error
if l_JS_RESULT->LAST_CONDITION_CODE <> 0.
write: / 'Compile Error', l_JS_RESULT->LAST_ERROR_MESSAGE.
exit.
else.
write / 'JavaScript was compiled successfuly'.
endif.
* Execute the JavaScript
l_JS_RESULT->EXECUTE( SCRIPT_NAME = 'FSP.JS' ).
if l_JS_RESULT->LAST_CONDITION_CODE <> 0.
write: / 'Error in EXECUTE',l_JS_RESULT->LAST_ERROR_MESSAGE.
exit.
else.
write / 'JavaScript was executed'.
endif.
* Read the JavaScript value
l_RET_VAL = l_JS_RESULT->EVALUATE( JAVA_SCRIPT = 'l_SRC;' ).
" Display the JavaScript value
write : / l_RET_VAL.
Post a Comment