Use ABAP code to fill CRM Survey question and submit
Suppose I have created a Survey in CRM:
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.
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.
As I prefer ID which are more readable, I switch to Expert Mode to manually assign ID to those elements.
Those IDs will appear in the final rendered Survey HTML, have a look at them:
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:
- Yes, I can work with ABAP.
- No, I can not work with Java.
The source code could be found from my github: https://github.com/i042416/KnowlegeRepository/blob/master/ABAP/survey/ZCL_CRM_SURVEY_TOOL.abap
Here below is the explanation of this API.
As documented in the comment, the API consists of four steps.
Step1: 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.
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.
Step2: 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.
As a result in my method GET_NEW_SURVEY_INSTANCE_GUID I use the regular expression below to parse the GUID.
Step3: 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.
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”.
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.
Step4: 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.
We can check in backend to double confirm: