在 CRM WebClient UI 开发框架中用javascript触发 ABAP event
环境:SAP CRM WebClient UI
需求:在WebClient UI里不通过用户手动点击,而是使用JavaScript代码自动触发ABAP后台的代码。
解决方案:
1. 定义一个hidden button, 其onClick事件处理函数(event handler)设置成SAP ABAP后台的event Refresh:
2. 通过document.getElementByID拿到该button的实例,再调用其方法click()触发ABAP backend事件:
var button = document.getElementByID("<%= button_id %>");
button.click();
打开page,发现backend event成功被触发:
BAP 7.40推出了许多新的关键字 - keyword,您的Sublime Text可能无法针对这些新关键字实现期望的语法高亮显示(syntax highlight)或者自动完成(auto completion)。
实际上只需手动修改Sublime Text安装目录下的DataPackagesABAP里的两个文件就能保证对于SAP ABAP Netweaver新推出的关键字,也能支持语法高亮显示和自动完成:
1. ABAP.tmLanguage
例如增加对REDUCE关键字的支持:
2. ABAP.sublime-completions
将期望支持自动完成功能的关键字维护进这个文件的对应位置即可。
测试:REDUCE这个新关键字能够自动完成,输入完成之后呈自动高亮状态。
我在S/4HANA的事务码MM02里为Material维护图片文件作为附件:
通过如下简单的ABAP代码即可将图片文件的二进制内容读取出来:
REPORT zgos_api.
DATA ls_appl_object TYPE gos_s_obj.
DATA lo_gos_api TYPE REF TO cl_gos_api.
DATA lt_attachment_list TYPE gos_t_atta.
DATA lt_role_filter TYPE gos_t_rol.
DATA: lv_id TYPE matnr VALUE '16',
lt_att TYPE TABLE OF sibflporb.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = lv_id
IMPORTING
output = lv_id.
ls_appl_object-typeid = 'BUS1001006'.
ls_appl_object-instid = lv_id.
ls_appl_object-catid = 'BO'.
START-OF-SELECTION.
TRY.
lo_gos_api = cl_gos_api=>create_instance( ls_appl_object ).
APPEND cl_gos_api=>c_attachment TO lt_role_filter.
lt_attachment_list = lo_gos_api->get_atta_list( lt_role_filter ).
CATCH cx_gos_api INTO DATA(error).
WRITE:/ error->get_text( ).
RETURN.
ENDTRY.
DATA: ls_key TYPE gos_s_attkey,
lv_base64 type string.
LOOP AT lt_attachment_list ASSIGNING FIELD-SYMBOL(<id>).
ls_key-atta_id = <id>-atta_id.
ls_key-atta_cat = <id>-atta_cat.
DATA(ls) = lo_gos_api->get_al_item( ls_key ).
BREAK-POINT.
ENDLOOP.
然后我把这段代码封装到一个Function moduleZDIS_GET_MATERIAL_IMAGES里,在Java代码里消费这个function module,把S/4HANA返回的二进制内容存成本地图片文件。在ABAP系统里图片二进制文件内容的类型为RAWSTRING,如何在Java里使用它?
使用JCO在Java里消费ABAP function module的代码:
static private void getProductImageTest(){
JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
JCoRepository repo = destination.getRepository();
JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_MATERIAL_IMAGES");
JCoParameterList imports = stfcConnection.getImportParameterList();
String materialID = "16";
imports.setValue("IV_MATERIAL_ID", materialID);
stfcConnection.execute(destination);
JCoParameterList exports = stfcConnection.getExportParameterList();
int abapDuration = exports.getInt("EV_DURATION");
StringBuilder sb = new StringBuilder();
sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ",");
sb.append("\"" + UPSELL_PRODUCT + "\":[");
JCoTable codes = exports.getTable("ET_IMAGES");
int row = codes.getNumRows();
System.out.println("Total rows: " + row);
System.out.println("ABAP duration: " + abapDuration);
for( int i = 0; i < row; i++){
codes.setRow(i);
sb.append("{\"" + FILE_ID + "\":" + codes.getString("FILEID") + ","
+ "\"" + FILE_OWNER + "\":\"" + codes.getString("OWNER") + "\"" + ",");
sb.append("{\"" + FILE_CDATE + "\":" + codes.getString("CREATION_DATE") + ","
+ "\"" + FILE_NAME + "\":\"" + codes.getString("FILENAME") + "\"");
storeLocalFile(codes);
if( i < row - 1){
sb.append("},");
}
else{
sb.append("}");
}
}
sb.append("]}");
System.out.println("Final json: " + sb.toString());
} catch (JCoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
其中把ABAP的类型为RAWSTRING的字段保存成本地文件的代码封装于方法storeLocalFile里:
static private void storeLocalFile(JCoTable codes){
InputStream is = codes.getBinaryStream("FILECONTENT");
try {
File file = new File("c:\\temp\\" + codes.getString("FILENAME"));
byte[] bytes = new byte[is.available()];
is.read(bytes);
OutputStream output = new FileOutputStream(file);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
bufferedOutput.write(bytes);
bufferedOutput.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
执行Java代码后,在本地C盘temp目录下能够看到S/4HANA里Material ID为16的两个附件图片:
- 点赞
- 收藏
- 关注作者
评论(0)