Use ABAP functions to read and parse SAP CRM Survey data

This article describes the source code and parsing.

Source code:

*&---------------------------------------------------------------------*
*& Report ZSURVEY_READ
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZSURVEY_READ.

DATA: vguid    TYPE crm_svy_db_sv_guid.
DATA: vvers    TYPE crm_svy_db_svers.
DATA: svy_content TYPE string.
DATA: ret TYPE bapiret1.
DATA: apppar   TYPE TABLE OF crm_svy_db_sv_pair.

data: lv_x type xstring.

 CALL FUNCTION 'CRM_SVY_SURVEY_GET'
      EXPORTING
        application_id     = 'CRM_SURVEY_ACTIVITY'
        survey_id          = 'JERRY_TEST'
        survey_version     = '0000000003'
        language           = 'E'
        media_type         = '01'
        parameter_xml      = 'CRM_SVY_BSP_SYSTEMPARAM.XML'
        values_guid        = vguid
        values_version     = vvers
      IMPORTING
        return             = ret
        content            = svy_content
      TABLES
        application_params = apppar.

call FUNCTION 'CRM_SVY_DB_CONVERT_STRING2HEX'
  EXPORTING
     s = svy_content
  IMPORTING
     x = lv_x.


DATA lr_conv TYPE REF TO cl_abap_conv_in_ce.

data: lv_request_xml type string.
CALL METHOD cl_abap_conv_in_ce=>create
  EXPORTING
    input = lv_x
  RECEIVING
    conv  = lr_conv.

CALL METHOD lr_conv->read
  IMPORTING
    data = lv_request_xml.

DATA(reg_pattern) = '.*svyValueGuid(?:.*)value="(.*)">.*SurveyId.*'.

DATA(lo_regex) = NEW cl_abap_regex( pattern = reg_pattern ).

DATA(lo_matcher) = lo_regex->create_matcher( EXPORTING text = lv_request_xml ).

IF lo_matcher->match( ) <> abap_true.
  WRITE:/ 'fail'.
  RETURN.
ENDIF.

DATA(lt_reg_match_result) = lo_matcher->find_all( ).

READ TABLE lt_reg_match_result ASSIGNING FIELD-SYMBOL(<match>) index 1.

read TABLE <match>-submatches ASSIGNING FIELD-SYMBOL(<submatch>) index 1.
data:lv type string.

lv = lv_request_xml+<submatch>-offset(<submatch>-length).

TRY.

reg_pattern = '.*<input(.*)>.*'.

lo_regex = NEW cl_abap_regex( pattern = reg_pattern ).

lo_matcher = lo_regex->create_matcher( EXPORTING text = lv ).

IF lo_matcher->match( ) <> abap_true.
  WRITE:/ 'fail in input scan'.
  RETURN.
ENDIF.


data: result_tab TYPE match_result_tab.


FIND ALL OCCURRENCES OF REGEX '<input(.*)>' IN lv RESULTS result_tab.

WRITE:/ lv.
BREAK-POINT.

CATCh cx_root into data(cx_root).
   WRITE:/ cx_root->get_text( ).
   return.
endtry.

CLEAR: lt_reg_match_result.

lt_reg_match_result = lo_matcher->find_all( ).

READ TABLE lt_reg_match_result ASSIGNING FIELD-SYMBOL(<input>) index 1.

WRITE:/ 'input:', lines( <input>-submatches ).

read TABLE <input>-submatches ASSIGNING <submatch> index 1.

data(new) = lv+<submatch>-offset(<submatch>-length).

WRITE:/ new.

The main function of this ABAP code is to read the content of the questionnaire from a CRM (Customer Relationship Management) system and analyze the data. Here is a detailed explanation of the code:

  1. Reporting Statements and Data Definitions :

    • REPORT ZSURVEY_READ.: Defines the name of a report, which may be a customized CRM report.
    • The data definition section declares some variables for storing data obtained from the CRM system.
  2. Call the CRM function :

    • CALL FUNCTION 'CRM_SVY_SURVEY_GET': This is a function call used to obtain questionnaire information from the CRM system. Some parameters like application ID, survey ID, version, etc. are passed to get relevant information. The obtained data is stored in svy_contentvariables.
  3. Convert content to hexadecimal :

    • CALL FUNCTION 'CRM_SVY_DB_CONVERT_STRING2HEX': This function call converts the obtained questionnaire content ( svy_content) into hexadecimal encoding and stores the result in lv_xthe variable.
  4. Text transcoding and analysis :

    • Use cl_abap_conv_in_cethe class to create a transcoder that converts hexadecimal data into text form.
    • lv_request_xmlThis XML then contains the questionnaire data through regular expression matching and analysis .
    • The goal of this section of code is to extract specific information about the questionnaire from the XML, such as svyValueGuidthe value of .
  5. Regular expression matching :

    • Use regular expressions to match lv_request_xmldata in and extract key information.
    • DATA(reg_pattern)and DATA(lo_regex)are used to define and create regular expression objects.
    • lo_matcherThe object is used to perform matching operations and extract matching results.
  6. Exception handling :

    • Use the TRY-CATCH block to handle possible exceptions. If the regular expression fails to match, an exception is generated.
    • The exception handling section prints out exception information and terminates code execution.
  7. Continue matching :

    • After a successful match svyValueGuid, the code continues matching inputtags. This part of the code may be to extract more information related to the questionnaire.
  8. Final output :

    • Finally, the code extracts the match results and stores the results in newa variable.
    • The results are printed, possibly for further processing or display to the user.

The core task of this code is to obtain questionnaire information from the CRM system and analyze it. The specific questionnaire content and required information may vary based on actual circumstances. Regular expressions are used in the code to extract the required information from the XML data and then store it in variables for subsequent processing. In practical applications, these data may be used to generate reports, analyze user feedback, or other operations related to the questionnaire.

Guess you like

Origin blog.csdn.net/i042416/article/details/132836249