BTP - GET_USER_ALIAS returns empty value - abap

Hi I made my first Hello World in BTP and it didn't show my user alias...
Why?
CLASS zpo_hello_world DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zpo_hello_world IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( |Hello World ! ({ cl_abap_context_info=>get_user_alias( ) })| ).
ENDMETHOD.
ENDCLASS.

Some of the methods in cl_abap_context_info can only be used in a cloud environment.
cl_abap_context_info=>
New Method
Old Environemnt
ET_SYSTEM_DATE
SYST-DATUM
GET_SYSTEM_TIME
SYST-UZEIT
GET_USER_TECHNICAL_NAME
SYST-UNAME
GET_USER_LANGUAGE_ABAP_FORMAT
SYST-LANGU
GET_USER_LANGUAGE_ISO_FORMAT
ISO format for language
GET_USER_TIME_ZONE
SYST-TZONE
GET_USER_ALIAS
Alias of user master data (Cloud)
GET_SYSTEM_URL
URL of the system (Cloud)
GET_USER_FORMATTED_NAME
BAPI_USER_GET_DETAIL
GET_USER_DESCRIPTION
BAPI_USER_GET_DETAIL
GET_USER_BUSINESS_PARTNER_ID
Business-Partner-ID of the user (Cloud)
(see here)
Thus means if you run On-Premise those methods will not return any value as far as I know.
But I just saw that you are using the BTP abap cloud example. I've also tested it here it seems that there no alias is set for the users.
You can verify your user with the get_user_business_partner_id-method which is only valid in cloud.
METHOD if_oo_adt_classrun~main.
TRY.
out->write( |Hello World { cl_abap_context_info=>get_user_business_partner_id( ) }| ).
CATCH cx_abap_context_info_error.
ENDTRY.
ENDMETHOD.
I'm not sure weather you can set a user alias with transaction SU1 due there is no SAP Gui support I guess.

Related

How to read ABAP code using a java client

I have a requirement were I need to read ABAP code written by SAP developers. I want to write my own client using Java/Python which can integrate with SAP system and get me the ABAP code.
What I understand that ABAP code is stored in SAP database like HANA, mysql etc. So is there a way which SAP provides where we can read the code like we can do in Git/SVN etc.
I've used RFC calls RPY_FUNCTIONMODULE_READ and RPY_FUNCTIONMODULE_READ_NEW through the perl NWRFC wrapper/library to retreive ABAP code.
You can access tables with below techniques:
Using SAP Connectors via RFC (RFC_READ_TABLE)
Using SOAP Web Service with same function (RFC_READ_TABLE)
Using custom web services with existing functions which are reading report, functions, etc.
You can use both Java or Pyhton for RFC, there is already exits github repo for python.
If you will select reading directly in db table, you need to know structure of saved data. It has own mechanism for OOP objects. Daniel Berlin try to implement binary parser in C++ in sap-reposrc-decompressor project. Never forget this source depended with SAP version.
I think using ADT (ABAP Development Tools) plugin is good for updated systems. There is already Eclipse plugin exists for ADT. ADT not exists in old systems.
If you are planning to use your solution in old system (after 7.01), you can build your own solution with abapGit and custom web services.
NOTE: Keep in mind, report and data elements (variables, tables, types) saved in separate tables. Dynpro objects (screens etc), reports (Smartforms) hard things to decompile.
Before you re-invent a wheel, Take a look at:
ABAPgit. https://docs.abapgit.org/
or the old SAPLink https://wiki.scn.sap.com/wiki/display/ABAP/SAPlink.
If you want JUST the source code, You could expose a very simple rest service/ Endpoint in SAP.
This service would just read the raw code and return it as plain text.
Every abaper could create this for you.
BUT is the raw source only. There is much more to a complete development
and why tools like ABAPGIT exist.
In SICF, create a new endpoint / service.
EG ZCODE_MONKEY with the class below as an example.
Now activate the service.
Call the endpoint
http://server:PORT/zcode_monkey?name=ZCODE_MONKEY
Sample implementation
CLASS zcode_monkey DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES: if_http_extension.
ENDCLASS.
CLASS zcode_monkey IMPLEMENTATION.
METHOD if_http_extension~handle_request.
DATA: lo_src type ref to CL_OO_SOURCE,
l_name TYPE string,
l_repname type c length 30,
l_clskey type seoclskey ,
l_source type rswsourcet,
resultcode TYPE string.
FIELD-SYMBOLS: <line> TYPE LINE OF rswsourcet.
l_name = server->request->get_form_field( name = 'NAME' ).
l_clskey = l_name.
l_repname = l_name.
create OBJECT lo_src
EXPORTING
clskey = l_clskey
EXCEPTIONS
class_not_existing = 1
others = 2 .
IF sy-subrc <> 0.
read REPORT l_repname into l_source.
else.
lo_src->read( ).
lo_src->if_oo_clif_source~get_source( IMPORTING source = l_source ).
ENDIF.
LOOP AT l_source ASSIGNING <line>.
CONCATENATE resultCode
cl_abap_char_utilities=>cr_lf
<line>
INTO resultCode RESPECTING BLANKS. " always show respect ;)
ENDLOOP.
SErver->response->set_content_type( content_type = 'text/plain' ).
server->response->set_cdata( EXPORTING data = resultcode ).
server->response->set_status(
EXPORTING
code = 200
reason = 'this is a 3.50 piece of code. Dont ask...its a demo ' ).
ENDMETHOD.
ENDCLASS.

Get stacktrace of errors from PyRFC call?

Up to now I get only an error message if something inside my SAP RFC function is wrong
pyrfc._exception.ABAPRuntimeError: RFC_ABAP_MESSAGE (rc=4): key=No authorization,
message=No authorization [MSG: class=00, type=E, number=001, v1-4:=No authorization;;;]
It would increase the development speed a lot if I could get a stacktrace of ABAP function. Is there a way to get a stacktrace like for example in Python?
Related: https://softwarerecs.stackexchange.com/questions/52350/sentry-event-from-exception-to-html
Sentry uses a particular JSON to represent a stacktrace and the content of the local variables. Above link contains an example.
Stack trace inside ABAP can be called
with the class cl_abap_get_call_stack.
Local variables are not included in the stack trace returned by the class cl_abap_get_call_stack.
But you could use a log-point to monitor local variables and the stack trace. Log-points can be created, changed and viewed in transaction saab.
A example code snippet:
DATA(formatted_stack) =
cl_abap_get_call_stack=>format_call_stack_with_struct(
cl_abap_get_call_stack=>get_call_stack( ) ).
LOG-POINT ID my_log_point FIELDS formatted_stack
local_variable1 local_variable2.
For the authorization-error, please check transaction su53.
When you see the red authorization-object S_RFC, it means you are not allowed to call the function module in any way!
With ABAP 753 release there was introduced such structure as EPP - Extended Passport.
It seems to be doing something that you want, i.e. showing trace of the called system. I put "seems to be" because I have no 753+ system by my hands so I cannot check in practice.
From the description it should do what you want:
An Extended Passport (EPP) is a data structure that can be sent from a client to a server and is used to analyze call stacks
Extended Passport can be used by frameworks and analysis tools to track external call stacks in communication between clients and servers beyond system boundaries. The values of the EPP components can be saved to log files and used for monitoring. One example of this are short dumps, which all display the most important EPP components.
The DEMO_EPP gives the following usage pattern of EPP:
cl_demo_epp=>init( ).
"this program
cl_demo_epp=>append( ).
"Calling RFC to remote instance
CALL FUNCTION 'DEMO_RFM_EPP_1' DESTINATION instance.
"New SAP LUW
CALL FUNCTION 'DEMO_UPDATE_DELETE' IN UPDATE TASK
EXPORTING
values = VALUE demo_update_tab( ).
COMMIT WORK.
cl_demo_epp=>append( ).
cl_demo_output=>new(
)->begin_section( `Extended Passport (EPP)`
)->display( name = 'EPP Trace'
data = cl_demo_epp=>get( ) ).

Get Information about available parameters of report via XBP

I would like to get information about a report. I want to know which values are available if I call the report like this:
SUBMIT (IV_REPORT_NAME)
WITH SELECTION-TABLE selection_table
AND RETURN.
Here is my attempt:
FUNCTION /FOO/GET_REPORT_INFO.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*" IMPORTING
*" VALUE(IV_REPORT_NAME) TYPE PROGRAMM
*" EXPORTING
*" VALUE(EV_RESULT_JSON) TYPE STRING
*"----------------------------------------------------------------------
DATA:
ld_return TYPE BAPIRET2,
it_selscreen_info TYPE STANDARD TABLE OF BAPI_SELSCREEN_INFO,"TABLES PARAM
wa_selscreen_info LIKE LINE OF it_selscreen_info,
it_selscreen_texts TYPE STANDARD TABLE OF TEXTPOOL,"TABLES PARAM
wa_selscreen_texts LIKE LINE OF it_selscreen_texts,
ld_external_user_name TYPE BAPIXMLOGR-EXTUSER.
ld_external_user_name = 'foo'.
CALL FUNCTION 'BAPI_XBP_READ_SELSCREEN'
EXPORTING
program = IV_REPORT_NAME
external_user_name = ld_external_user_name
IMPORTING
return = ld_return
TABLES
selscreen_info = it_selscreen_info
.
ev_result_json = /ui2/cl_json=>serialize( data = it_selscreen_info pretty_name = /ui2/cl_json=>pretty_mode-low_case ).
IF sy-subrc <> 0.
MESSAGE |Error sy-subrc: { sy-subrc }| TYPE 'E'.
ENDIF.
ENDFUNCTION.
But the result is always empty.
What am I doing wrong? Is BAPI_XBP_READ_SELSCREEN the right method for my goal?
This is an answer about "why BAPI_XBP_READ_SELSCREEN doesn't work", not an answer about the main question which is "how to retrieve selection screen parameters".
I'm not sure, but I guess BAPI_XBP_READ_SELSCREEN doesn't work because you don't apply the usual logic of XBP.
XBP ("eXternal Background Processing") is a library of APIs used to schedule jobs from an external scheduler. It's based on XMI ("eXternal Management Interface" Framework) which is used by other APIs, and which offers common features, especially the log.
XMI and the applications based on XMI are RFC-capable, so that they can be called remotely.
To use the APIs based on XMI, the minimum is to :
call the function module BAPI_XMI_LOGON to open a session to use a library of APIs you want to use.
For XBP, use the parameters INTERFACE = 'XBP' and VERSION = '3.0' (or the version you want).
The parameters EXTCOMPANY and EXTPRODUCT can be any max-16-characters names, as agreed with the administrators of the SAP software (they can be possibly blank too), and are subject to S_XMI_PROD authorizations if defined. If they are used with different XMI API libraries during the same RFC session, the same values must be passed.
call the APIs you want (BAPI_XBP_READ_SELSCREEN in your case).
Note that there's an "external user" parameter (usually EXTERNAL_USER_NAME), which is a free field, to be an identifier of the external application. This value is stored with the logs so that you may display or retrieve the XMI logs by external application (transaction RZ15).
call BAPI_XMI_LOGOFF
There is one INTERFACE parameter, whose value must be the same as the one used in BAPI_XMI_LOGON.
In case, you have called an API which updates data, call BAPI_TRANSACTION_COMMIT to commit these updates.
I've just tested BAPI_XBP_READ_SELSCREEN successfully with the program below (please handle the exceptions), so I think it's a good idea to use it :
DATA rfcdest TYPE rfcdest.
rfcdest = ''. " XBP is well suited for RFC, but this test doesn't need remote call.
CALL FUNCTION 'BAPI_XMI_LOGON'
DESTINATION rfcdest
EXPORTING
extcompany = 'ABAPERS&CO'
extproduct = 'NICEJOBSCHEDULER'
interface = 'XBP'
version = '3.0'.
DATA: selscreen_info TYPE STANDARD TABLE OF bapi_selscreen_info,
selscreen_texts TYPE STANDARD TABLE OF textpool.
CALL FUNCTION 'BAPI_XBP_READ_SELSCREEN'
DESTINATION rfcdest
EXPORTING
program = 'BTCAUX10'
external_user_name = 'SRO'
sel_text_lang = 'E' " English
TABLES
selscreen_info = selscreen_info
selscreen_texts = selscreen_texts.
CALL FUNCTION 'BAPI_XMI_LOGOFF'
DESTINATION rfcdest.
IF rfcdest IS NOT INITIAL. " if you use RFC, close the connection!
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = rfcdest
EXCEPTIONS
destination_not_open = 1
OTHERS = 2.
ENDIF.
Please check user name "foo". If foo is not external user then result will be empty because of wrong external user.
regards,
Umar Abdullah

Activiti BPMN - How to pass username in variables/expression who have completed task?

I am very new to Activiti BPMN. I am creating a flow diagram in activiti. I m looking for how username (who has completed the task) can be pass into shell task arguments. so that I can fetch and save in db that user who has completed that task.
Any Help would be highly appreciated.
Thanks in advance...
Here's something I prepared for Java developers based on I think a blog post I saw
edit: https://community.alfresco.com/thread/224336-result-variable-in-javadelegate
RESULT VARIABLE
Option (1) – use expression language (EL) in the XML
<serviceTask id="serviceTask"
activiti:expression="#{myService.toUpperCase(myVar)}"
activiti:resultVariable="myVar" />
Java
public class MyService {
public String toUpperCase(String val) {
return val.toUpperCase();
}
}
The returned String is assigned to activiti:resultVariable
HACKING THE DATA MODEL DIRECTLY
Option (2) – use the execution environment
Java
public class MyService implements JavaDelegate {
public void execute(DelegateExecution execution) throws Exception {
String myVar = (String) execution.getVariable("myVar");
execution.setVariable("myVar", myVar.toUpperCase());
}
}
By contrast here we are being passed an ‘execution’, and we are pulling values out of it and twiddling them and putting them back.
This is somewhat analogous to a Servlet taking values we are passed in the HTMLRequest and then based on them doing different things in the response. (A stronger analogy would be a servlet Filter)
So in your particular instance (depnding on how you are invoking the shell script) using the Expression Language (EL) might be simplest and easiest.
Of course the value you want to pass has to be one that the process knows about (otherwise how can it pass a value it doesn't have a variable for?)
Hope that helps. :D
Usually in BPM engines you have a way to hook out listener to these kind of events. In Activiti if you are embedding it inside your service you can add an extra EventListener and then record the taskCompleted events which will contain the current logged in user.
https://www.activiti.org/userguide/#eventDispatcher
Hope this helps.
I have used activiti:taskListener from activiti app you need to configure below properties
1. I changed properties in task listener.
2. I used java script variable for holding task.assignee value.
Code Snip:-

How can you use SessionAsSigner in a Java Bean called from an XPage?

According to Phillip Riand (see: discussion on openNTF) this is not possible... They need to know the design element to find out who signed it. Therefore, it is only available in SSJS.
There are 2 ways that I know of to use the sessionAsSigner object in Java beans:
1 By resolving the sessionAsSigner object:
FacesContext context = FacesContext.getCurrentInstance();
Session sessionAsSigner = context.getApplication().getVariableResolver().
resolveVariable(context, "sessionAsSigner");
2 By using the getCurrentSessionAsSigner() function from the com.ibm.xsp.extlib.util.ExtLibUtil class in the Extension Library.
To be able to use it (in Java as wel as SSJS) you'll want to make sure that all design elements were signed by the same user ID. If that's not the case, the sessionAsSigner object will not be available ('undefined').
I found that the solution is right at hand :-)
I changed my XPage (in this example an XAgent) to:
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
This is an xAgent returning json data...
<xp:this.afterRenderResponse><![CDATA[#{javascript:Controller.verify(sessionAsSigner)}]]></xp:this.afterRenderResponse>
and in the bean I simply used the session in the argument when I needed to open a database/document as signer. Sometimes the solution is so simple :-)
/John
This is quite an old post that I just stumbled upon. Tried some of the solutions mentioned above:
resolveVariable did not work for me, at least not for sessionAsSigner as this throws a runtime error (I can resolve plain old session, though...)
to be honest I didn't quite understand the Controller.verify(sessionAsSigner) method; is Controller something specific to XAgents? If so, I don't have an XAgent here, so can't use it
didn't feel like importing extra ExtLib classes here...
So I came up with another solution that appears to be very simple:
created a method in my javaBean that takes a session object as argument; since sessionAsSigner belongs to the same class as session I don't have to import something new.
Javacode is:
public void testSession(Session s) throws Exception{
System.out.println(" > test effective user for this session = "
+ s.getEffectiveUserName());
}
This is called from SSJS as either
mybean.testSession(session);
or
myBean.testSession(sessionAsSigner);
Maybe helps others, too