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.

/wp-content/uploads/2014/06/clipboard1_484559.png


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.


/wp-content/uploads/2014/06/clipboard2_484560.png

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.

/wp-content/uploads/2014/06/clipboard3_484561.png

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.

/wp-content/uploads/2014/06/clipboard4_484562.png

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:

/wp-content/uploads/2014/06/clipboard6_484564.png

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:

/wp-content/uploads/2014/06/clipboard7_484565.png


Create anew class enhancement implementation:

/wp-content/uploads/2014/06/clipboard8_484566.png

select method CALL_WS and insert a new post-method for it:

/wp-content/uploads/2014/06/clipboard9_484567.png

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.

/wp-content/uploads/2014/06/clipboard10_484568.png

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.

/wp-content/uploads/2014/06/clipboard10_484568.png

the complete source code of transformation could be found in attachment.

Here just some important codes:

/wp-content/uploads/2014/06/clipboard11_484570.png

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:

/wp-content/uploads/2014/06/clipboard12_484571.png


After that we could test in the runtime and observed the post exit get called, once the execution of CALL_WS method finishes:

/wp-content/uploads/2014/06/clipboard13_484572.png

And in the final merged word document, the format is now YYYY.MM.DD:

/wp-content/uploads/2014/06/clipboard14_484574.png

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,

/wp-content/uploads/2014/06/clipboard15_484575.png

However it is always not recommended to do manual changes on any stuff which are automatically generated.

/wp-content/uploads/2014/06/clipboard16_484576.png

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Piet Demeester

    Hi Jerry,

    I think this is absolutely great!

    It is a solution to a number of real world problems.

    As far as I understand (as I am quite new to SAP),

    it is the only way to display a date in another format than YYYY-MM-DD in a dynamic table.

    By dynamic table, I mean a table of which the number of rows is not known beforehand.

    Some minor comments:

    The transformation that you made is not a simple transformation, but a XSL transformation (as far as I see).

    Also: the string manipulation can be done without a call to an ABAP class method:

    (…)

    <xsl:template match=”@*|node()”>

        <xsl:copy>

            <xsl:apply-templates select=”@*|node()”/>

        </xsl:copy>

    </xsl:template>

    <xsl:template match=”ValfromDate”>

        <xsl:copy>

          <xsl:value-of select=”concat(substring(.,1,4),’.’,substring(.,6,2),’.’,substring(.,9,2))”/>

        </xsl:copy>

    </xsl:template>

    (…)

    Thanks for this excellent blog.

    By the way, your other blogs about document templates are very informative as well.

    Regards,

    Piet

    (0) 
    1. Jerry Wang Post author

      Hello Piet,

      Thanks a lot for your comment. I have corrected the error. And Your suggestion to format the date is really very effective and better than mine. I will share it with my colleagues 🙂

      Best regards,

      Jerry

      (0) 

Leave a Reply