The purpose of this blog is to explain how we can easily use AJAX to trigger asynchronous HTTP requests in Web UI Tableviews.
There are several blogs available on SAP Community about AJAX implementations in the Web UI but this blog focuses on Tableview integration.
Use cases for such implementation are unlimited but the current use case is the following:
In the Business Partner’s Identification Numbers assignment block we maintain the National Register ID of B2C customers (in a custom ID Type). For GDPR reasons, this value is encrypted before it is stored in the database table. But there is a business requirement to allow some agents seeing the decrypted ID. For performance reason and to avoid reloading the whole page, an asynchronous HTTP request is triggered to display the decrypted ID.
Remark: how is this ID first encrypted is not part of the scope of this blog. What is important to know is that encryption/decryption is done by an external API.
Figure 1 - Final result
Figure 2 - Iterator
METHOD CONSTRUCTOR.
gr_context_node_tv = ir_context_node_tv.
ENDMETHOD.
METHOD if_htmlb_tableview_iterator~render_cell_start.
DATA: lv_ident_type TYPE string,
lv_ident_code TYPE string,
lv_txtid_s TYPE string,
lv_txtid_l TYPE string,
lv_index TYPE char2,
lr_button TYPE REF TO cl_thtmlb_button,
lr_textview TYPE REF TO cl_thtmlb_textview.
* Check that Identification Number Row Type is 'National Register ID'
lv_ident_type = gr_context_node_tv->collection_wrapper->find( iv_index = p_row_index )->get_property_as_string( 'IDENTIFICATIONTYPE' ).
CHECK lv_ident_type EQ 'ZNATID'.
MOVE p_row_index TO lv_index.
* Generate the suffix and the full DOM element ID for the table row Texview (in this case: ‘<span>’)
CONCATENATE 'TXT.ZNATID.' lv_index INTO lv_txtid_s.
MOVE p_tableview_id TO lv_txtid_l.
REPLACE FIRST OCCURRENCE OF 'Table' IN lv_txtid_l WITH lv_txtid_s.
CASE p_column_key.
* Add Button 'Decrypt' in column <STRUCT.ATTR1>
WHEN '<STRUCT.ATTR1>'.
lv_ident_code = gr_context_node_tv->collection_wrapper->find( iv_index = p_row_index )->get_property_as_string( 'IDENTIFICATIONNUMBER' ).
CREATE OBJECT lr_button.
lr_button->onclientclick = |doDecryptID('| && lv_ident_code && |','| && lv_txtid_l && |')|.
lr_button->text = TEXT-001.
p_replacement_bee = lr_button.
* Add Textview in column <STRUCT.ATTR2>
WHEN '<STRUCT.ATTR2>'.
CREATE OBJECT lr_textview.
lr_textview->id = lv_txtid_s.
p_replacement_bee = lr_textview.
ENDCASE.
ENDMETHOD.
Figure 3 - Controller
METHOD do_init_context.
CALL METHOD super->do_init_context.
CREATE OBJECT iterator
EXPORTING
ir_context_node_tv = typed_context->builidnumber.
ENDMETHOD.
<%
data: lv_url TYPE string,
lv_query type string.
lv_query = 'q='.
lv_url = cl_crm_web_utility=>create_url( iv_path = '/sap/crm/zcrajax_natid_d' iv_query = lv_query iv_in_same_session = 'X' ).
%>
<script>
var gv_txtid;
function GetXmlHttpObject(){
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function stateChanged() {
if (xmlhttp.readyState == 4) {
document.getElementById(gv_txtid).innerHTML = xmlhttp.responseText;
}
}
function getRequestURL(str) {
var url = "<%= lv_url %>" + str;
url = url + "&sid=" + Math.random();
return url;
}
function doDecryptID(str,txtid){
if (str.length == 0 ) {
document.getElementById(txtid).innerHTML = "";
return;
}
gv_txtid = txtid;
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null ){
alert ("Your browser does not support XML HTTP Request");
return;
}
var requesturl = getRequestURL(str);
xmlhttp.onreadystatechange = stateChanged;
xmlhttp.open("GET",requesturl,true);
xmlhttp.send(null);
}
</script>
<chtmlb:configTable id = "Table"
table = "//BuilIDNumber/Table"
…
iterator = "<%= controller->iterator %>"
…
/>
Figure 4 - Handler Class
Figure 5 - SICF Service
METHOD if_http_extension~handle_request.
DATA: lv_ident_code TYPE string,
lv_ident_nr TYPE string.
lv_ident_code = server->request->get_form_field( 'q' ).
* call external API to retrieve the decrypted ID from the encrypted ID
********************************************
Call External API
exporting lv_ident_code (encrypted ID)
importing lv_ident_nr (decrypted ID)
********************************************
server->response->set_cdata( lv_ident_nr ).
ENDMETHOD.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |