Skip to content

Instantly share code, notes, and snippets.

@njames
Created August 14, 2019 05:03
Show Gist options
  • Save njames/3e832c42a06a8c89620b26708dcf4d4d to your computer and use it in GitHub Desktop.
Save njames/3e832c42a06a8c89620b26708dcf4d4d to your computer and use it in GitHub Desktop.
example determining user from service now
method get_caller_id.
data: uri type string
, body type string
, token type string
, agreements type string
, lo_response type ref to if_rest_entity
* , parser TYPE REF TO /ui5/cl_json_parser
, user type ref to data
, user_row type ref to data
.
*format of the uri see service now api documentation
* * create a http client
me->create_client( ).
uri = |/sys_user?sysparm_limit=1&user_name={ i_email }|.
* Set the URI if any
cl_http_utility=>set_request_uri(
exporting
request = me->http_client->request " HTTP Framework (iHTTP) HTTP Request
uri = uri " URI String (in the Form of /path?query-string)
).
* Set request header if any
call method me->rest_client->if_rest_client~set_request_header
exporting
iv_name = 'accept'
iv_value = 'application/json'.
call method me->rest_client->if_rest_client~set_request_header
exporting
iv_name = 'content-type'
iv_value = 'application/json'.
* HTTP GET
me->rest_client->if_rest_client~get( ).
* HTTP response
lo_response = me->rest_client->if_rest_client~get_response_entity( ).
* HTTP return status
data(http_status) = lo_response->get_header_field( '~status_code' ).
* HTTP JSON return string
data(json_response) = lo_response->get_string_data( ).
* parse json and find caller id
* /ui5/cl_json_parser
* lr_data TYPE REF TO data,
* lv_val TYPE string.
user = /ui2/cl_json=>generate( json = json_response ).
* LOOP AT User->result ASSIGNING FIELD-SYMBOL(<user_row>).
*
* /ui2/cl_data_access=>create( ir_data = <User_row> iv_component = `sys_id`)->value( IMPORTING ev_data = r_caller_id ).
* ENDLOOP.
field-symbols: <data> type data
, <result> type data
.
*data : ref type any.
*
*ref = /ui2/cl_data_access=>create( ir_data = user iv_component = `RESULT`)->ref( ).
* reference for the dynamic access to the json response
* https://wiki.scn.sap.com/wiki/display/Snippets/Dynamic+Data+Accessor+Helper+Class+for+ABAP
data(ref) = /ui2/cl_data_access=>create( iv_data = user iv_component = `RESULT[1]`)->ref( ) ."at(`SYS_ID`)->ref( ) .
if ref is bound.
assign ref->* to <data>.
assign component `SYS_ID` of structure <data> to <result>.
endif.
data: type_converter type char50
.
FIELD-SYMBOLS: <field> type any
.
* get REFERENCE OF <result> into <field>.
ASSIGN <result>->* to <field>.
if <field> is assigned.
* type_converter = <result>.
r_caller_id = <field>.
endif.
endmethod.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment