Skip to main content

SAP CRM: Reading the sections of a survey

Submitted by Stefan Barsuhn on

The questions and answers of a (returned) CRM survey can be easily accessed via database tables. But if you want to access the section belonging to a question, you need to get your hands dirty and access the Survey XML.

This can be done as follows:

**********************************************************************
* returns section for given survey question
**********************************************************************
  DATA: lr_survey  TYPE REF TO cl_crm_svy_survey,
        lr_svy_dom TYPE REF TO cl_crm_svy_dom.

  CLEAR rv_section.

  " get survey XML
  DO 1 TIMES.

    " create survey object
    CREATE OBJECT lr_survey
      EXPORTING
        application_id = iv_application
        survey_id      = iv_survey_id
*       suppress_after_imp = SVYDB_FALSE
*       survey_guid    =
      .
    CHECK lr_survey IS BOUND.

    " load survey data
    lr_survey->load(
      EXPORTING
        survey_version = iv_version_id    " Surveyversion: Initial lädt aktuelle Version
        language       = sy-langu    " Sprachenschlüssel: Initial lädt in Anmeldesprache
      EXCEPTIONS
        error_occured  = 1
        OTHERS         = 2
    ).
    CHECK sy-subrc EQ 0.

    " the XML contains the sections
    lr_survey->get_xml(
      EXPORTING
        language      = sy-langu    " Sprachenschlüssel
      IMPORTING
        raw_xml           =  DATA(lv_xml)
      EXCEPTIONS
        error_occured = 1
        OTHERS        = 2
    ).
    CHECK sy-subrc EQ 0.

    " convert XML into parsable DOM
    CREATE OBJECT lr_svy_dom
      EXPORTING
        i_application_id = iv_application
        i_survey_id      = iv_survey_id.
    CHECK lr_svy_dom IS BOUND.

    " convert to DOM
    CALL METHOD lr_svy_dom->create_survey_dom
      EXPORTING
        i_xml_xstring    = lv_xml
      EXCEPTIONS
        error_in_parsing = 1
        OTHERS           = 2.

    CHECK sy-subrc EQ 0.

    " transform DOM into tree
    lr_svy_dom->get_tree(
      IMPORTING
        e_tree_tab = DATA(lt_tree)
    ).
    CHECK lines( lt_tree ) NE 0.

    " The tree is built top down
    " This means if we find a question ID it belongs
    " to the Section directly above it
    LOOP AT lt_tree ASSIGNING FIELD-SYMBOL(<fs_tree>).
      IF <fs_tree>-node_type EQ 'SectionText'.
        " memorize section text
        DATA(lv_section_text) = <fs_tree>-text.
        CONTINUE.
      ENDIF.

      " when ID matches the question ID, assign it
      IF <fs_tree>-id EQ iv_question_id.
        rv_section = lv_section_text.
        RETURN.
      ENDIF.
    ENDLOOP.
  ENDDO.
ENDMETHOD.

Tags