Reports Wand for SAP Custom Content

18
Reports Wand for SAP Custom Content Developer’s Guide

Transcript of Reports Wand for SAP Custom Content

Page 1: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide

Page 2: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 2

Contents Overview of Reports Wand for SAP custom content application development ... 3

Introduction ................................................................................................................................... 3

Skills needed to develop custom content .......................................................................... 3

Understanding the Reports Wand product ................................................................. 3

The Reports Wand Concept ................................................................................................... 3

A simple example .......................................................................................................... 4

The Flights Scenario ................................................................................................................. 4

The Report Container Class ................................................................................................... 4

The Report Definition Class ................................................................................................... 6

The Report Metadata Class ................................................................................................... 7

The Report Drilldown Class .................................................................................................... 8

The Calculation Class............................................................................................................... 9

The Result .................................................................................................................................. 10

Appendix A - Setting an authorization check to control user access ................. 13

Appendix B - Using multiple tables .......................................................................... 13

Appendix C - Setting required parameters .............................................................. 13

Appendix D - Limiting which fields can be used as a filter ................................. 14

Appendix E - Setting default values ......................................................................... 15

Appendix F - Setting the from clause for a table join .......................................... 15

Appendix G – performing a record level authorization check ............................... 15

Page 3: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 3

Overview of Reports Wand for SAP custom content application development

Introduction Reports Wand for SAP now provides a framework to enable creating and using custom content with

the product. The framework is included since version 20.3.0. This will enable end users to run custom

reports from within Reports Wand and even build calculations with drilldown capability. The user can

then leverage the power of Reports Wand to filter the data and create layouts that suits their needs

while applying the relevant security built into the custom content.

Skills needed to develop custom content The skills needed to develop custom content are dependent on the complexity of the report itself. A

Super user with some basic table knowledge will likely be able to create a simple report (e.g. single

table or a single table join) as the framework provides most of the functionality. An ABAP developer

user id in SAP is required as a few small ABAP classes are created.

Understanding the Reports Wand product

The Reports Wand Concept In versions prior to 20.3, the Reports Wand product provided a user interface to be able to execute

CDS views in SAP and present the results in Excel. Since the 20.3.0 version, we have added prebuilt

content as well as the ability to create your own custom content. There are four main parts to building

the new custom content in Reports Wand

1. Report Definition – Defines the required parameters, list of available fields and annotations

for the parameters and fields

2. Report metadata – Provides information about each of the parameters and fields (e.g. Field

Name, Description, Type, whether it can be used as a filter)

3. Drill down Definition – Accepts any parameters and filters passed by the user, runs the query

and returns the results to Excel

4. Calculations (Optional) – Allows users to return calculations (e.g. Sum, Count, Max, Min)

based on parameters and fields as well as drilling down on the resulting balance to see the

details that match the calculated balance.

Please note: As requirements for the product change it may be necessary to change the

underlying interfaces or classes. Please always use the most recent version of the respective

interface or classes. The version is contained in the interface or class name e.g.

/excel4ap/20_xxxxx_03_00 which means it was developed as part of the 20.3.0 release.

Page 4: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 4

A simple example

The Flights Scenario We are going to use the table SFLIGHT to demonstrate how easy it is to enable Reports Wand to use

this information and empower an end user to build their own reports. The SAP table SFLIGHT contains

flight information relating to airlines, types of aircraft, dates of flights and flight capacity.

The Report Container Class The starting point is to create an ABAP class that implements the interface

/excel4ap/20_if_rep_def_03_00.

This new class directs Reports Wand where to get the information noted above which will be in other

classes that you create. It also gives Reports Wand some information about your report like its name,

when it was created etc. You can name your new class anyway you like, just note the class name will

be used as the report identification in Reports Wand. The entire new class looks like this:

class zcl_flights definition public. public section. interfaces /excel4ap/20_if_rep_def_03_00. protected section. private section. endclass. class zcl_flights implementation. method /excel4ap/20_if_rep_def_03_00~get_calculation_class_name. r_calculation_class_name = 'ZCL_RW_FLT_CLC'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_definition_class_name. r_definition_class_name = 'ZCL_RW_FLT_DEF'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_description. r_description = 'Report to get flight information'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_drilldown_class_name. r_drilldown_class_name = 'ZCL_RW_FLT_DRL'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_metadata_class_name. r_metadata_class_name = 'ZCL_RW_FLT_MTA'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_name. r_name = 'Flights'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_component. r_component = 'ZREPORT'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_component_description. r_component_description = 'My Z reports'. endmethod. method /excel4ap/20_if_rep_def_03_00~get_created_on.

Page 5: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 5

r_created_on = '20200522'. endmethod. method /excel4ap/20_if_rep_def_03_00~is_authorized. r_authorized = 1. endmethod. endclass.

The class simply tells Reports Wand where to get each of the pieces of information in order to use this

report along with a few other pieces of descriptive information. Note there is an is_authorized method

that just returns 1 for true for this report as it does not require an authorization check. See Appendix A

for an example of how to apply an authorization check to control which users can run the report.

Once this class has been created and activated it will appear in the Reports Wand list of available

reports like the screenshot below.

To debug your class, set an external breakpoint and click the Create Report button in Reports Wand

Notice that the report identification number is the class name, the name of the report is populated,

the module has the component name and the component description is in the sub module column.

These are all useful for searching for the report. The creation date is also displayed below.

Page 6: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 6

The Report Definition Class In the main report class, the method get_definition_class_name was populated with a returning value

of 'ZCL_RW_FLT_DEF'. This instructs Reports Wand to use this class name to find the report

definition information. You can name this class anything you like, just return that name for that

method and Reports Wand will make use of that class.

Here is the entire report definition class:

class zcl_rw_flt_def definition public inheriting from /excel4ap/20_rep_def_dtl_03_00 final create public . public section. methods set_execution_criteria redefinition. protected section. private section. endclass. class zcl_rw_flt_def implementation. method set_execution_criteria. append 'SFLIGHT' to me->table_list. endmethod. endclass.

This simple class let’s Reports Wand know that you want to work with the SAP table SFLIGHT. The key

to this class is that it inherits from the /excel4ap/20_rep_def_dtl_03_00 class. In this example we simply

specify the table name and Reports Wand will return all the fields of this table. If you more advanced

functionality such as specifying fields to return or defining parameters, see the appendix below.

To debug your definition class, set an external breakpoint and click the Create Report button in

Reports Wand and then click Next, or on an already created report, click the Report Editor or the

Parameter List button. Note: you need to launch again each time as the report definition is cached

after it has run.

Page 7: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 7

The Report Metadata Class In the main report class, we populated the method get_metadata_class_name with a returning value of

'ZCL_RW_FLT_MTA'. This lets Reports Wand know where to get the report metadata from.

Here is the entire metadata class:

class zcl_rw_flt_mta definition public inheriting from /excel4ap/20_rep_meta_03_00 final create public . public section. methods set_execution_criteria redefinition. protected section. private section. endclass. class zcl_rw_flt_mta implementation. method set_execution_criteria. append 'SFLIGHT' to me->table_list. endmethod. endclass.

Report Wand will use the table to get the field metadata for you. You only need to inherit from the

/excel4ap/20_rep_meta_03_00 class.

To debug your metadata class, set an external breakpoint and click the Create Report button in

Reports Wand and then click Next, or on an already created report, click the Report Editor or the

Parameter List button. Note: you need to launch again each time as the report definition is cached

after it has run.

Page 8: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 8

The Report Drilldown Class The new Reports Wand framework makes establishing the drilldown class easy by inheriting from the

/excel4ap/20_rep_drill_03_00 drilldown class. Here is it below:

class zcl_rw_flt_drl definition public inheriting from /excel4ap/20_rep_drill_03_00 final create public . public section. methods set_execution_criteria redefinition. protected section. methods populate_table_list redefinition. private section. endclass. class zcl_rw_flt_drl implementation. method set_execution_criteria. super->set_execution_criteria( ). me->from_clause = 'SFLIGHT'. endmethod. method populate_table_list. append 'SFLIGHT' to me->table_list. endmethod. endclass.

As there is no specific additional processing required one simply sets the information relating to the

SFLIGHT table and as the class inherits from the /excel4ap/20_rep_drill_03_00 drilldown class, Reports

Wand handles the rest of the processing.

To debug your drilldown class, set an external breakpoint and Execute the report in Reports Wand

Page 9: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 9

The Calculation Class Finally, the calculation class is listed below.

class zcl_rw_flt_clc definition public inheriting from /excel4ap/20_rep_calc_03_00 final create public . public section. methods set_execution_criteria redefinition. protected section. methods populate_table_list redefinition. private section. endclass. class zcl_rw_flt_clc implementation. method set_execution_criteria. super->set_execution_criteria( ). me->from_clause = 'SFLIGHT'. endmethod. method populate_table_list. append 'SFLIGHT' to me->table_list. endmethod. endclass.

This class requires only that the table details are populated. The class inherits from

/excel4ap/20_rep_calc_03_00 and Reports Wand does the rest.

To debug your calculation class, set an external breakpoint and click the Refresh>Range button while

on a cell that has a calculation you created using the Insert Run Report button

Page 10: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 10

The Result Once these simple classes are created, a user can use the Report Definition to build their reports in

Excel. Here is an example of a basic layout that a user can create in Reports Wand.

First, they will select the fields to include on the layout. Users can optionally add sorting, grouping or

subtotals.

They can then use any of the fields to filter the results. Here the user is filtering their report for the

aircraft type 747-400.

The user can then execute the report and would be able to format it as they wish in Excel.

Page 11: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 11

An example of the calculation functionality is shown below. Here the user is building a calculation to

count the number of entries there are where the airline code equals AA.

Page 12: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 12

Once refreshed, the calculation returns the result showing that there are 26 records where the airline

code is equal to AA.

If the user double clicks the balance, they can drilldown to the detail of the 26 records.

Page 13: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 13

Appendix A - Setting an authorization check to control user access The authorization check below in the main report class will only allow SAP users that have the

S_ALR_87012326 transaction code to run the report in Reports Wand.

method /excel4ap/20_if_rep_def_03_00~is_authorized. authority-check object 'S_TCODE' id 'TCD' field 'S_ALR_87012326'. if sy-subrc = 0. r_authorized = 1. else. r_authorized = 0. endif. endmethod.

Appendix B - Using multiple tables In the definition class add the tables that you are joining.

method set_execution_criteria. append 'SKA1' to me->table_list. append 'SKAT' to me->table_list. endmethod.

Appendix C - Setting required parameters In the definition class, override the get_parameter_list method. The example below sets the chart of

accounts and the language as required parameters. It also defines the required chart of accounts

parameter as a range.

method get_parameter_list. data: parameter type ddfieldname_l, temp_parameter like line of r_parameter_list. parameter = 'SKA1~KTOPL'. append parameter to i_parameter_fields. parameter = 'SKAT~SPRAS'. append parameter to i_parameter_fields. r_parameter_list = super->get_parameter_list( i_report = i_report i_parameter_fields = i_parameter_fields ). loop at r_parameter_list into temp_parameter. if temp_parameter-fieldname = 'SKA1~KTOPL'. temp_parameter-isrange = 1. modify r_parameter_list from temp_parameter. exit. endif. endloop. endmethod.

Page 14: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 14

Appendix D - Limiting which fields can be used as a filter In the definition class, override the get_field_list method. The example below only allows fields from

table SKA1 and not the MANDT (client) field to be displayed in the filter field list.

method get_field_list. data: field type /excel4ap/20_rw_rep_field, table type tabname, table_count type i, counter type i value 0. describe table me->table_list lines table_count. loop at me->table_list into table. select dd03l~fieldname dd03l~inttype dd04t~ddtext dd03l~datatype from dd03l as dd03l left outer join dd04t as dd04t on dd04t~rollname eq dd03l~rollname and dd04t~ddlanguage eq sy-langu into corresponding fields of field where tabname = table and inttype ne '' order by dd03l~position. if field-ddtext is initial. field-ddtext = field-fieldname. endif. if table = 'SKA1' and field-fieldname <> 'MANDT'. field-isfilter = 1. else. field-isfilter = 0. endif. counter = counter + 1. field-pos = counter. if table_count > 1. concatenate table '~' field-fieldname into field-fieldname. concatenate field-ddtext '(' field-fieldname ')' into field-ddtext separated by space. endif. append field to r_field_list. endselect. endloop. endmethod.

Page 15: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 15

Appendix E - Setting default values In the definition class, override the get_parameter_annotations method to provide default values. In

the example below, the default chart of accounts is set to INT and the default language is set to E.

method get_parameter_annotations. data: annotation type /excel4ap/5_rw_param_anno. if i_parameter_name = 'SKA1~KTOPL'. annotation-annoname = 'CONSUMPTION.DEFAULTVALUE'. annotation-value = 'INT'. append annotation to parameter_annotations. endif. if i_parameter_name = 'SKAT~SPRAS'. annotation-annoname = 'CONSUMPTION.DEFAULTVALUE'. annotation-value = 'E'. append annotation to parameter_annotations. endif. endmethod.

Appendix F - Setting the from clause for a table join In the drilldown class set the join as an example below. The same join condition must be set in the

calculation class if you wish to enable calculations as well. It is quite simple to create a query in SAP

with transaction code SQVI and extract the join condition from there if you need some assistance.

method set_execution_criteria. super->set_execution_criteria( ). me->from_clause = 'SKA1 INNER JOIN SKAT ON SKAT~KTOPL = SKA1~KTOPL AND SKAT~SAKNR = SKA1~SAKNR'. endmethod.

Appendix G – performing a record level authorization check In order to perform a record level authorization check we need to ensure that the columns that hold

the values for the authorization check are included in the drilldown and calculation classes. The user

may or may not have selected them when running the report so we override the

read_reports_wand_columns method in the drill down class to ensure the required columns are

included. In the example below we need the VBRK~VKORG and the VBRK~FKART fields.

method read_reports_wand_columns. data: field type fieldname. super->read_reports_wand_columns( input_stream = input_stream ). describe table variable_fields_table lines end_index. loop at variable_fields_table into field. if field = 'VBRK~VKORG'. vkorg_exists = 1. endif. if field = 'VBRK~FKART'. fkart_exists = 1. endif. endloop.

Page 16: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 16

if vkorg_exists = 0. if me->fields_clause is initial. me->fields_clause = 'VBRK~VKORG AS VBRK-VKORG'. else. concatenate me->fields_clause ', VBRK~VKORG AS VBRK-VKORG' into me->fields_clause respecting blanks. endif. append 'VBRK~VKORG' to variable_fields_table. endif. if fkart_exists = 0. if me->fields_clause is initial. me->fields_clause = 'VBRK~FKART AS VBRK-FKART'. else. concatenate me->fields_clause ', VBRK~FKART AS VBRK-FKART' into me->fields_clause respecting blanks. endif. append 'VBRK~FKART' to variable_fields_table. endif. endmethod.

We then simply override the execute method to add the record level authorization check as shown

below.

method execute. field-symbols: <table> type standard table, <rec> type any. super->execute( ). assign me->internal_table->* to <table>. if <table> is assigned. loop at <table> assigning <rec>. auth_check( exporting auth_object = 'V_VBRK_VKO' field_name = 'VBRK-VKORG' activity = '03' org_object = 'VKORG' changing record = <rec> ). check rw_error is initial. auth_check( exporting auth_object = 'V_VBRK_FKA' field_name = 'VBRK-FKART' activity = '03' org_object = 'FKART' changing record = <rec> ). endloop. endif. endmethod.

For the related calculation class, we just amend the set_execution_criteria method to add the

required fields.

method set_execution_criteria. super->set_execution_criteria( ). me->from_clause = 'VBRK INNER JOIN VBRP ON VBRK~VBELN = VBRP~VBELN'. if me->operator <> 'COUNT'. concatenate column_name ' AS ' table_column ' VBRK~VKORG AS VBRK-VKORG VBRK~FKART AS VBRK-FKART' into me->fields_clause respecting blanks.

Page 17: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 17

concatenate me->balance_fields ' VBRK~VKORG VBRK~FKART' into me->balance_fields respecting blanks. else. me->fields_clause = 'VBRK~VKORG AS VBRK-VKORG VBRK~FKART AS VBRK-FKART'. me->balance_fields = 'VBRK~VKORG VBRK~FKART'. endif. endmethod.

We then override the execute method to perform the authorization check for the calculation class.

method execute. data: component type abap_componentdescr. field-symbols: <table> type standard table, <rec> type any, <current> type any, <sum> type any. super->execute( ). assign me->internal_table->* to <table>. if <table> is assigned. read table <table> index 1 assigning <rec>. component = get_column_component( <rec> ). create data sum type handle component-type. assign sum->* to <sum>. loop at <table> assigning <rec>. auth_check( exporting auth_object = 'V_VBRK_VKO' field_name = 'VBRK-VKORG' activity = '03' org_object = 'VKORG' changing record = <rec> ). check rw_error is initial. auth_check( exporting auth_object = 'V_VBRK_FKA' field_name = 'VBRK-FKART' activity = '03' org_object = 'FKART' changing record = <rec> ). counter = counter + 1. if me->operator = 'SUM' or me->operator = 'AVG'. assign component table_column of structure <rec> to <current>. <sum> = <sum> + <current>. endif. endloop. get_max( i_component = component ). get_min( i_component = component ). endif. endmethod.

Page 18: Reports Wand for SAP Custom Content

Reports Wand for SAP Custom Content Developer’s Guide 18

Finally, for the calculation class we override the write_response method to cater for these changes.

method write_response. field-symbols: <avg> type any, <sum> type any, <max> type any, <min> type any. output_stream->write_boolean( 1 ). if me->operator = 'COUNT'. me->write_field_to_stream( exporting output_stream = output_stream field = counter ). elseif me->operator = 'SUM'. assign sum->* to <sum>. me->write_field_to_stream( exporting output_stream = output_stream field = <sum> ). elseif me->operator = 'AVG'. assign sum->* to <sum>. assign sum->* to <avg>. <avg> = <sum> / counter. me->write_field_to_stream( exporting output_stream = output_stream field = <avg> ). elseif me->operator = 'MAX'. assign max->* to <max>. me->write_field_to_stream( exporting output_stream = output_stream field = <max> ). elseif me->operator = 'MIN'. assign min->* to <min>. me->write_field_to_stream( exporting output_stream = output_stream field = <min> ). endif. me->write_field_to_stream( exporting output_stream = output_stream field = counter ). output_stream->write_boolean( 0 ). endmethod.