cancel
Showing results for 
Search instead for 
Did you mean: 

CRM DINAMIC SURVEY

Former Member
0 Kudos

Hi, All

     I need help with create the dynamic questionnaire. In transaction CRM_SURVEY_SUITE I have the own survey with a few questions. I want to hide/delete some questions depending on the answers of to previous questions. To do this, I created own FM Z*PBO (copy of CRM_MKTCA_SURVEY_MKT_PBO). To begin, I write a simple code to instantly hide some questions, starting with the third. I'am using method ir_survey_values->no_display_add in that FM:


     loop at lt_all_values into ls_all_values.

      

       if ls_all_values-question_id = zcl_crm_mkt_svz_survey=>c_qst2 and lv_excl_flag is initial.

              lv_excl_flag = 'X'.

              continue.

       elseif lv_excl_flag = 'X'.

        

          ls_no_display-name = ls_all_values-question_id.

          append ls_no_display to lt_no_display.

       else.

         continue.

       endif.

     endloop.

call method ir_survey_values->no_display_add

      exporting

        it_no_display = lt_no_display.


As a result, I hide the questions and their answers. But the texts(for example: Text of the sections of the survey, Text for the answers ) are displaying. Tell me please  detail - how to hide them too. Maybe I need to create the javascript code, which can hide/show questions or sections of the survey? Or something else?



I found the code that returns text. Maybe there is functionality that they can change this texts?


data lo_surv_texts type ref to cl_crm_svy_survey_texts.

   data lt_crm_text_t type crm_svy_api_survey_text_t.

   create object lo_surv_texts

     exporting

       i_application_id = i_application_id

       i_survey_id      = i_survey_id

       i_survey_version = i_survey_version

       i_language       = sy-langu

     exceptions

       survey_not_found = 1

       others           = 2.

   call method lo_surv_texts->get_all_texts

     importing

       et_texts = lt_crm_text_t.




Regards, Valeriy

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hello.

Task completed.

For to bind a function with the answer of the question, I used the event "onclick":

   ex_script-name     = 'answer2'.                           "#EC NOTEXT
   ex_script-language = 'JavaScript1.2'.                     "#EC NOTEXT
   ex_script-script   = 'function answer2() { '.


   concatenate ex_script-script

                alert(''onclick!'');'

               '}'

     into ex_script-script.


append ls_javascript to lt_javascript.
   call method ir_survey_values->javascript_add
     exporting
       it_script = lt_javascript.


    clear ls_svy_on_event.
   ls_svy_on_event-question_id   = 'id_b748794d673ed77de10000000a781c65'.
   ls_svy_on_event-answer_id     = 'id_0050568601f81ed598ea87be3f400ab9'.
   ls_svy_on_event-name          = 'onclick'.          

   concatenate 'answer2' '()' into ls_svy_on_event-call_function.
   append ls_svy_on_event to lt_svy_on_event.

  

    call method ir_survey_values->on_event_add
     exporting
       it_on_event = lt_svy_on_event.

Former Member
0 Kudos

Hi Valeriy,

Have a look at the FM: CRM_SVY_EXAMPLE_DYNAMIC_PBO.

It plays with survey elements using java script.

Regards,

Naresh

Former Member
0 Kudos

Hi Naresh.

Yes, I saw too that module. It describes the common techniques. There is how to assign JavaScript for the field:

   read table lt_all_values into ls_value
                            with key answer_id = 'event'.
   check sy-subrc = 0.
   check not ls_value is initial.
   concatenate function '(this)' into function.
   ls_on_event-question_id   = 'on_event'.
   ls_on_event-answer_id     = 'on_event'.
   ls_on_event-name          = ls_value-value.
   ls_on_event-call_function = function.
   append ls_on_event to lt_on_event.
   call method ir_survey_values->on_event_add
     exporting
       it_on_event = lt_on_event.

What do I need write in JavaScript to hide the Texts? From the example I understood that is possible assign it to perform for questions and answers. But I did not understand what is the 'function' (function name). It is FM? And what is '(this)'? How then to link the FM and JavaScript? How should I load javascript?

I want to hide the text without answering yet on any question. For example, to ten questions I have seen only two on the screen. How to download and execute such JavaScript?

Then I need JavaScript that will display some questions and answers with texts. And to tie it to questions and answers.

Maybe there is a solution without javascript?

Former Member
0 Kudos

Hi Valeriy,

I have tried with sample survey and it worked with following syntax in PBO FM.

FM Copy code continues here...

*************************

DATA: lt_javascript   TYPE survy_t_javascript,

         ls_javascript   TYPE survy_s_javascript,

         lt_svy_on_event TYPE survy_t_on_event,

         ls_svy_on_event TYPE survy_s_on_event.

   ls_javascript-name     = 'hideFields'.                   "#EC NOTEXT

   ls_javascript-language = 'JavaScript1.2'.                 "#EC NOTEXT

   ls_javascript-script   = 'function hideFields() { '.      "#EC NOTEXT

   CONCATENATE ls_javascript-script

              'document.getElementById(''q2'').style.visibility="hidden"; }'

               INTO ls_javascript-script.


APPEND ls_javascript TO lt_javascript.

   CALL METHOD ir_survey_values->javascript_add

     EXPORTING

       it_script = lt_javascript.

* </Create and insert JavaScript>

* <Assign script to event onLoad in the BODY-tag>

   ls_svy_on_event-question_id   = 'html'.                   "#EC NOTEXT

   ls_svy_on_event-answer_id     = 'body'.                   "#EC NOTEXT

   ls_svy_on_event-name          = 'onLoad'.                 "#EC NOTEXT

   CONCATENATE ls_javascript-name '()' INTO ls_svy_on_event-call_function. "#EC NOTEXT


.............................


Continue code standard FM....



in above q2 is the question ID which I gave when I created the survey which you can define in CRM_SURVEY_SUITE - Expert Mode.


Try this.


Regards,

Naresh



Former Member
0 Kudos

Naresh, Thanks a lot for the help.


I tried to use your code, but have the same result as using

  call method ir_survey_values->no_display_add
    exporting
      it_no_display = lt_no_display.


The Text of the Section is displayed on the screen. I attach 4 images  for example.

Before hiding of the third question:

After  hide of  the third question:

example of the structure of the third question:

Example of the Section with third  question:

It is possible to hide the Text of Sections or the entire Section?

Regards, Valeriy

Former Member
0 Kudos

Hi Valeriy,

This is new javascript which will hide the whole section of a particular question.

ls_javascript-name     = 'hideFields'.                    "#EC NOTEXT

   ls_javascript-language = 'JavaScript1.2'.                 "#EC NOTEXT

   ls_javascript-script   = 'function hideFields() { '.      "#EC NOTEXT

   CONCATENATE ls_javascript-script

*               This will give all the sections of survey - SECTION1 is hard coded  - Do not change it

                 'var elements = document.getElementsByClassName(''Section1''); '

                 'for (var i=0; i<elements.length; i++) {'

                   'var children = elements[i].children;'

                   'for (var j=0; j<children.length; j++) {'

*                   If the question ID is q2 then hide the whole section in next statement

                     'if( children[j].id == "q2" ) { '

*                   Hide the whole section of question q2

                     'elements[i].style.visibility = "hidden"; }'

                   '}}}'

               INTO ls_javascript-script.                    "#EC NOTEXT

   APPEND ls_javascript TO lt_javascript.


Replace it with the old code and try.


Regards,

Naresh

Former Member
0 Kudos

I tried to use your code. Also called method call method

ir_survey_values->get_internal_values_xml

    importing

      e_internal_values_xml     = lv_internal_values_xml

      e_internal_values_xml_hex = lv_internal_values_xml_hex

    exceptions

      transformation_failure    = 1

      others                    = 2.

It shows that add the function hideFields for the event onLoad.

But, unfortunately, I could not hide the section. I tried to use other names: "Section", "Section2", "Section3" instead of the "Section1". And I tried to use various questions id. But nothing worked(

How did you know that it is necessary to use "Section1," not another name? Can I see in the debugger how to perform this script?

Former Member
0 Kudos

Hi Valeriy,

Open the survey in CRM_SURVEY_SUITE and then right click on it.

Select the option View Source which will open the HTML Content of the survey.

Scroll down towards bottom where <BODY> tag starts.

Below is my example survey from where I picked up Section 1.

  <body onLoad="hideFields()">

    <div class="Title">This is a Test Survey</div>

    <form action="SAPEVENT:WFF_EVENT" class="saveSnapshot" enctype="" id="SAPSurvey" method="post" onSubmit="return check(this)">

      <input name="svyApplicationId" type="hidden" value="CRM_SURVEY_CORE">

      <input name="SurveyId" type="hidden" value="ZTEST">

      <input name="svySurveyId" type="hidden" value="ZTEST">

      <input name="svyVersion" type="hidden" value="0000000003">

      <input name="SchemaVersion" type="hidden" value="1 ">

      <input name="svySchemaVersion" type="hidden" value="1 ">

      <input name="svyLanguage" type="hidden" value="EN">

      <input name="conid" type="hidden" value="">

      <input name="svyMandatoryMessage" type="hidden" value="Fill all mandatory fields before saving">

      <div Id="" class="Section1">

        <div class="SectionText1">Section 1</div>

        <div Id="q1" class="Question">

          <div class="QuestionText">Question 1</div>

          <table datatable="0" summary="Survey Questions" class="QuestionListInline">

            <tr>

              <td class="ListAnswerFieldText">Maintain Answer and Attributes</td>

              <td class="ListAnswerField">

                <input class="AnswerFieldRW" maxlength="40 " name="survey/result/q1/a1" size="40 " type="text" value="">

              </td>

            </tr>

          </table>

        </div>

        <div class="Event">

          <input name="onInputProcessing(SUBMIT)" type="hidden">

          <input class="ButtonInside" type="submit" value="Save">

          <input class="ButtonInside" type="reset" value="Reset">

        </div>

      </div>

      <div Id="" class="Section1">

        <div class="SectionText1">Section 2</div>

        <div Id="q2" class="Question">

          <div class="QuestionText">Question 2</div>

          <table datatable="0" summary="Survey Questions" class="QuestionList">

            <tbody>

              <tr>

                <td class="ListAnswerField">

                  <input class="AnswerFieldRW" maxlength="20 " name="survey/result/q2/id_0050568e5ac81ed59fa7278b4fff63a2" size="20 " type="text" value="">

                </td>

                <td class="ListAnswerFieldText">Answer for Question 2</td>

              </tr>

            </tbody>

          </table>

        </div>

      </div>

    </form>

  </body>

check the names for your class. Also you can try with the class name "Question" but then script will be different to find the parent section of the question and hide it.

Give a try.

Regards,

Naresh

Former Member
0 Kudos

Hi, Naresh

I have successfully hidden the question and the text section by using the code:

var element =document.getElementById('id_2ff6114df680d13de10000000a781c65');element.parentNode.style.display="none";

Now I still have to tie a script that would be performed in answering the question. For example, I have a the question q1(id_b748794d673ed77de10000000a781c65)  and the answer a1 (id_0050568601f81ed598ea87be3f400ab9) as ListAnswerCheckbox. The answer may take 2 values: val1(id_3f6f794d673ed77de10000000a781c65) and val2(id_0050568601f81ed598eabcde1c180ad0).

I want to bind a functions to the values of the answer that will show the next question from the questionnaire. I don't know how to do it.

I tried the following code, but it did not work:

     ls_javascript-name     = 'answerDSP'.                    "#EC NOTEXT
     ls_javascript-language = 'JavaScript1.2'.                 "#EC NOTEXT
     ls_javascript-script   = 'function answerDSP() { '.      "#EC NOTEXT

   concatenate ls_javascript-script
                'var element =  document.getElementById(''id_2ff6114df680d13de10000000a781c65'');'
*              'element.style.visibility="hidden";'
*             'var parent = element.parentNode.id;'
              'element.parentNode.style.display="none";'
                  '}'
                into ls_javascript-script.

    append ls_javascript to lt_javascript.

   call method ir_survey_values->javascript_add
     exporting
       it_script = lt_javascript.

   ls_svy_on_event-question_id   = Q1.                   "#EC NOTEXT
   ls_svy_on_event-answer_id     = A1.                   "#EC NOTEXT
   ls_svy_on_event-name          = VAL1.                 "#EC NOTEXT

   concatenate 'answerDSP' '()' into ls_svy_on_event-call_function. "#EC NOTEXT
   append ls_svy_on_event to lt_svy_on_event.

   call method ir_survey_values->on_event_add
     exporting
       it_on_event = lt_svy_on_event.




This possible to do? How to associate a function with the value of the answer?

Former Member
0 Kudos

Hi Valeriy,

I am not completely sure about this requirement.

I think its possible with dynamic list box. You can have a look at the standard dynamic PBO FM. In last section of code, it shows how to play with dynamic list box.

Regards,

Naresh