Change date format in web service response for CRM attachments
- Why the date is always displayed as YYYY-MM-DD
- How to change date format according to customer need
- Advantage of this solution
In this document Create Webservice enabled word document in attachment assignment block the steps to create attachments based on word template whose content would be filled by web service are demonstrated.
However, one customer complains that in the generated word document, the date format is not displayed as they expected.
They hope the date is displayed as “2012.11.28” instead.
Why the date is always displayed as YYYY-MM-DD
In the previous document the architecture about the web service call in document scenario is briefing introduced. The function module generated by office integration framework will be called and the web service response to be merged into word document is contained in importing parameter output of the function module.
The date field in response structure has ABAP type D(8). Although there is available BAdI definition which enables customer to change the content of web service response, this BAdI does not work for this very case, since the field displayed in below screenshot will not be directly used to merge with the document template.
Instead, it is the serialized xml string ( handled by another automatically generated transformation ) which will be merged into the word document and finally displayed to end user.
After transformation, we could observe the value of ABAP type D(8), 20121128, has been transformed to 2012-11-28.
according to sap help, this is working as designed:
How to change date format according to customer need
Since there is no such BAdI definition provided by SAP which could allow changes on the transformed xml payload, it is necessary to put the adaptation on the response stream into a proper place with the modification-free approach.
1. use SE24 to open class CL_CRM_OI_RT, click Class->Enhance:
Create anew class enhancement implementation:
select method CALL_WS and insert a new post-method for it:
Once done, you could observe a tab in column “PostExit” for method CALL_WS. Click on it and you will navigate to source code editor.
2. Implement the post-exit method.
The signature of method CALL_WS contains three changing parameters:
ev_data: the string format of xml response. We need to change the date format there.
er_payload: the reference pointing to ABAP response. We do not need to change it.
ex_payload: the xstring format of xml response. We need to change the date format there.
the complete source code of transformation could be found in attachment.
Here just some important codes:
Here I develop a new transformation ztime_convert to do the time format transformation:
- line13: detect the xml node whose name equal to ValfromDate, as I would like to only transform Valid from Date as example.
- line15: define a local variable in this transformation, with its value filled by the content of xml node ValfromDate, which has format YYYY-MM-DD.
- line16: call ABAP class method ZCL_JERRY_TOOL=>GET_NEW_DATE. This method must be defined as public and static.
- line17: the transformation variable lv_old works as importing parameter IV_OLD_DATE of ABAP method in line 16.
- line18: ABAP method returns the transformed date to returning parameter and fill the content to a new variable lv_new_date.
- line20: write the content of variable lv_new_date back to xml node ValfromDate.
The implementation of GET_NEW_DATE is very simple:
After that we could test in the runtime and observed the post exit get called, once the execution of CALL_WS method finishes:
And in the final merged word document, the format is now YYYY.MM.DD:
Advantage of this solution
If the default transformation rule listed in screenshot above with title “Value Representation in XML” is not expected by you, you could change the transformed field content with this solution. We didn’t do any modifications on standard code, so the change will survive after system upgrade or patch applied.
Although in theory it is possible to change the implementation of automatically generated transformation which could fulfill the same result,
However it is always not recommended to do manual changes on any stuff which are automatically generated.