Friday 10 August 2018

Use ABAP code to fill CRM Survey question and submit

Suppose I have created a Survey in CRM:

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

The requirement is to develop an ABAP API which can fill the questions and then submit. With this API, we can draw the survey question layout using other UI library in mobile phone, and allow users to fill the survey question in phone. Once filled, the answers of survey question will be submitted to ABAP backend using the developed API.

The question design of my survey is very simple. The highlighed value below will be used in my ABAP API.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

We can toggle the display model between HTML and XML in CRM transaction code CRM_SURVEY_SUITE. And by default the attributes like QuestionId and AnswerID will be filled with the automatically-generated guid.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

As I prefer ID which are more readable, I switch to Expert Mode to manually assign ID to those elements.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning


SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

How to use my API


REPORT zsurvey_test.

DATA(lo_tool) = NEW zcl_crm_survey_tool( ).

lo_tool->submit( iv_question1 = 'X' iv_question2 = '' ).​

This report will submit a survey with the following answers to the two questions:

1. Yes, I can work with ABAP.
2. No, I can not work with Java.

Here below is the explanation of this API.

As documented in the comment, the API consists of four steps.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

Step 1: get rendered survey template’s HTML source code.

The survey template is stored in XML format. When the survey is requested by end user, the XSLT provided by SAP will transform it to HTML format.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

Such transformation is encapsulated in my method GET_SURVEY_TEMPLATE by calling function module CRM_SVY_SURVEY_GET, returning the template source code in HTML format.
Meanwhile a new version of Survey value is generated by function module CRM_SVY_VALUES_SET. A new GUID which represents this new version is also merged into the HTML source code.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

Step 2: retrieve generated value version’s guid.

As explained in step 1, the guid is contained in the HTML source code. It is necessary to append this guid later as the survey submit request body, otherwise the survey submit will fail.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

As a result in my method GET_NEW_SURVEY_INSTANCE_GUID I use the regular expression below to parse the GUID.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

Step 3: prepare submit request body

So far I didn’t know what exactly content I need to submit, so I test the survey submit in browser and debug a little bit.

I just fill both questions with answer yes, and observe what exactly is filled and sent to the survey submit entry point function module CRM_SVY_RESULT_DISPATCHER.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

And here is exact request:

svyApplicationId=CRM_SURVEY_ACTIVITY&SurveyId=JERRY_TEST&svySurveyId=JERRY_TEST&svyVersion=0000000003&SchemaVersion=1 &svySchemaVersion=1 &svyLanguage=EN&conid=&svyValueGuid=FA163EEF573D1ED89E9D22A316FC4754&svyValueVersion=0000000001&svyMandatoryMessage=Fill all mandatory fields before saving&survey/result/question1/answer1_placeholder=answer1_yes&survey/result/question2/answer2_placeholder=answer2_yes&onInputProcessing=SUBMIT
The blue one is the survey value GUID I mentioned, the red one represents “yes I can work with ABAP”, and the purple one for “yes I can work with Java”.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

This means in my API I can simply assemble such string accordingly and finally call FM CRM_SVY_RESULT_DISPATCHER.

For simplicity reason I hard code all other Survey header fields like Application ID, Survey ID etc. The request body method assembly is done in my method ASSEMBLE_REQUEST_BODY.

Step 4: submit survey via FM CRM_SVY_RESULT_DISPATCHER.

This FM will return successful message “The Values Were Saved Successfully” to indicate that the submit is done without errors.

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

We can check in backend to double confirm:

SAP CRM, SAP ABAP Development, SAP Certification, SAP ABAP Learning

3 comments:

  1. It has been great for me to read such great informationsap-pp training

    ReplyDelete
  2. Thanks for the Post-it is Very Informative But I want to Share my Experience with you that i Pass my Microsoft Dynamics Exam dumps at first attempt by getting Best Exam Preparation Material Form Dumpshq .com They Really were Amazing and I recommend you to Get Certification Exam from Dumpshq.

    ReplyDelete
  3. A basic and special logo can help in structure your organization's image picture. logo design service

    ReplyDelete