CRM and CX Blogs by Members
Find insights on SAP customer relationship management and customer experience products in blog posts from community members. Post your own perspective today!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Recently We have came across, a requirement to read the Survey values , which can be read using the class cl_crm_svy_survey_texts,

DATA: rr_survey_texts TYPE REF TO cl_crm_svy_survey_texts.

* create survey texts object
    CREATE OBJECT rr_survey_texts
      EXPORTING
        i_application_id = i_app_id
        i_survey_id      = wa_survey-surveyid
        i_survey_version = wa_survey-surveyversion
        i_language       = sy-langu
        i_valueguid      = wa_survey-valueguid
        i_valueversion   = wa_survey-valueversion
      EXCEPTIONS
        survey_not_found = 1
        OTHERS           = 2.


   CHECK sy-subrc = 0.

    CLEAR e_all_values[].
    CALL METHOD rr_survey_texts->get_all_values
      IMPORTING
        e_all_values = e_all_values.

    CLEAR et_texts[].
    CALL METHOD rr_survey_texts->get_all_texts
      IMPORTING
        et_texts = et_texts.

The above method will get the values selected only after the data is saved in database ,

But there are some requirement which we need to read from the Runtime before save , when validation to be done in ORDER_SAVE BADI BEFORE_SAVE method .

For that we can take the advantage of the CRM_ORDER_READ ( which inturn calls the CRM_SURVEY_READ_OW ) FM where it will return the Survey latest Information in runtime .

      CALL FUNCTION 'CRM_SURVEY_READ_OW'
        EXPORTING
          iv_guid                       = iv_guid
*         IT_GUID                       =
          iv_kind                       = 'A'
        IMPORTING
          et_survey_wrk                 = lt_survey_wrk
          et_survey_wrk_latest_version  = lt_survey_wrk_latest_version
        EXCEPTIONS
          parameter_error               = 1
          record_not_found              = 2
          at_least_one_record_not_found = 3
          OTHERS                        = 4.
      IF sy-subrc <> 0.
* Implement suitable error handling here
      ENDIF.

This lt_survey_wrk_latest_version variable will have the VALUEXML attribute which contains the runtime Version of the Surveys

then Convert the XML which in Hexadecimal  into STRING and read the data using the    cl_crm_svy_value.

Data : ET_String type STRING_TABLE.

   CALL FUNCTION 'CRM_SVY_DB_CONVERT_HEX2STRING'
    EXPORTING
      x = iv_valuexml
    IMPORTING
      s = lv_value_xml.

  CREATE OBJECT lr_survey_values
    EXPORTING
      i_internal_values_xml   = lv_value_xml
    EXCEPTIONS
      error_in_generation     = 1
      error_in_parsing        = 2
      error_in_transformation = 3
      OTHERS                  = 4.

  IF lr_survey_values IS BOUND.


    DATA : lt_all_values TYPE survy_t_all_values.
    DATA : ls_all_values LIKE LINE OF lt_all_values.
    DATA : lv_question TYPE string .
    DATA : lv_answer TYPE string .
    DATA : lv_string TYPE string.
    DATA : lt_values TYPE crm_svy_api_string_t.

    DATA : lv_application_id  TYPE  crm_svy_db_appl_id.
    DATA : lv_surveyversion TYPE  crm_svy_db_svers.
    DATA : lt_survey_texts  TYPE  crm_svy_api_survey_text_t.
    DATA : ls_survey_texts TYPE crm_svy_api_survey_text.


    CALL FUNCTION 'CRM_SVY_GET_SURVEY_TEXTS'
      EXPORTING
*       SURVEY           =
        application_id   = lv_application_id " Application Name  ( CRM_SURVEY_SERVICE,CRM_SURVEY_SALES etc)
        survey_id        = iv_survey_id " Survey Name
*       LANGUAGE         = SY-LANGU
      IMPORTING
        survey_texts     = lt_survey_texts
      CHANGING
        survey_version   = lv_surveyversion
      EXCEPTIONS
        survey_not_found = 1
        OTHERS           = 2.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.


    CALL METHOD lr_survey_values->values_get_all
      IMPORTING
        et_all_values = lt_all_values.

******* This case in the Survey have a Single Question with Check Boxes as answers and a Text Edit Box

   LOOP AT lt_all_values INTO ls_all_values.

      lv_question = ls_all_values-question_id.
      lv_answer = ls_all_values-value.


      READ TABLE lt_survey_texts INTO ls_survey_texts WITH KEY type = 'Answer' id = lv_answer.
      IF sy-subrc EQ 0.

        lv_string = ls_survey_texts-text.
        APPEND lv_string TO et_string.
      ELSE.
        APPEND lv_answer TO et_string.
      ENDIF.
    ENDLOOP.

  ENDIF.