如何用代码读取SAP CRM的Categorization Schema

时间:2022-07-24
本文章向大家介绍如何用代码读取SAP CRM的Categorization Schema,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

As I mentioned in my blog How to create Service Request subject data by code I need to generate a large number of Service Request as test data which should have Subject Category filled randomly.

So it is necessary for me to figure out how to read out the hierarchy data of a given Categorization Schema so that I can spread it randomly to the created Service Request.

I explore it a bit in BOL browser and find the root BOL node for the displayed schema above is MC_Schema:

Then use relation “MC_CategoryRel” to get all Category node in first level.

The category node has relation MC_TreeChildRel, which could be used to get all its children category nodes recursively.

With this idea I have developed a API method to return the hierarchy Schema data based on a given Schema ID:

Result:

With this API, I have also developed a report to display the Schema tree in SAPGUI, which has exactly the same structure as displayed in WebUI.

Source code for report to display schema in SAPGUI:

REPORT ORDER_DISPLAY_SCHEMA_TREE.

PARAMETERS: schemaid type string OBLIGATORY DEFAULT 'WJ_SOFTWARE'.

DATA: g_alv_tree TYPE REF TO cl_gui_alv_tree,
      gt_data    TYPE STANDARD TABLE OF cl_home_schema_tool=>ty_displayed_node,
      ok_code    LIKE sy-ucomm,
      save_ok    LIKE sy-ucomm,
      ls_data    LIKE LINE OF gt_data.

END-OF-SELECTION.
  DATA(lo_tool) = NEW cl_home_schema_tool( ).
  DATA(lt_fieldcat) = lo_tool->get_fieldcat_by_data( ls_data ).
  PERFORM change_label.
  CALL SCREEN 100.

MODULE pbo OUTPUT.
  SET PF-STATUS 'MAIN100'.
  SET TITLEBAR 'MAINTITLE'.
  IF g_alv_tree IS INITIAL.
    PERFORM init_tree.
    CALL METHOD cl_gui_cfw=>flush
      EXCEPTIONS
        cntl_system_error = 1
        cntl_error        = 2.
    ASSERT sy-subrc = 0.
  ENDIF.
ENDMODULE.

MODULE pai INPUT.
  save_ok = ok_code.
  CLEAR ok_code.
  CASE save_ok.
    WHEN 'EXIT' OR 'BACK' OR 'CANC'.
      PERFORM exit_program.
    WHEN OTHERS.
      CALL METHOD cl_gui_cfw=>dispatch.
  ENDCASE.
  CALL METHOD cl_gui_cfw=>flush.
ENDMODULE.
FORM change_label.
  READ TABLE lt_fieldcat ASSIGNING FIELD-SYMBOL(<id>) INDEX 1.
  <id>-seltext = <id>-reptext = <id>-scrtext_m = <id>-scrtext_s = <id>-scrtext_l = 'Category ID'.
  <id>-outputlen = 20.
  READ TABLE lt_fieldcat ASSIGNING FIELD-SYMBOL(<text>) INDEX 2.
  <text>-seltext = <text>-reptext = <text>-scrtext_m = <text>-scrtext_l = 'Description'.
  <text>-scrtext_s = 'Name'.
  <text>-outputlen = 40.
ENDFORM.
FORM init_tree.
  g_alv_tree = lo_tool->get_tree( ).
  DATA l_hierarchy_header TYPE treev_hhdr.
  PERFORM build_hierarchy_header CHANGING l_hierarchy_header.
  CALL METHOD g_alv_tree->set_table_for_first_display
    EXPORTING
      is_hierarchy_header = l_hierarchy_header
    CHANGING
      it_fieldcatalog     = lt_fieldcat
      it_outtab           = gt_data.
  PERFORM create_tree.
  g_alv_tree->frontend_update( ).
  lo_tool->expand( ).
ENDFORM.
FORM create_tree.
  lo_tool->get_hierarchy_data( schemaid ).
  lo_tool->draw_tree( ).
ENDFORM.
FORM build_hierarchy_header CHANGING p_hierarchy_header TYPE treev_hhdr.
  p_hierarchy_header-heading = 'Categorization Schema'.
  p_hierarchy_header-width = 30.
  p_hierarchy_header-width_pix = ' '.
ENDFORM.

FORM exit_program.
  LEAVE PROGRAM.
ENDFORM.