In these two documents below, I have demonstrated the steps how to create word or pdf document as attachments which could consume web service created via CRM web service tool.


Create Webservice enabled Adobe PDF attachment in CRM Application

Create Webservice enabled word document in attachment assignment block


As we could find in document template creation UI, there is a third option which allows us to assign a WSDL URL of an external webservice to a document template. So this document will explain how to create a word template which could consume your own web service, instead of the one created via CRM web service tool.

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


Ensure you have a working web service

for simplicity I just create a simple function module which do the add operation and expose it into web service with tcode SOAMANAGER.

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

In order to consume it, An ABAP proxy is needed.

Test the ABAP consumer proxy in ABAP workbench and make it work, since it will be used later:

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

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

You could use this document as reference about how to expose a function module into web service via SOAMANAGER and the necessary configuration to consume it in ABAP.


Create word template and assign your webservice WSDL to it


In document creation UI, choose radio box “Own WSDL URL”, paste your web service WSDL URL to input field and press entry key, then your web service operation will appear in dropdown list automatically. Choose your operation and click button “Start Designer”.

If you meet with 401 unauthorization error message after enter key is pressed, refer to this document about solution:

How to resolve Unauthorized error message when defining external web service for document template

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

Now a new word document is opened. You could drag the output variable EV_RESULT from SAP Data panel to your word body area.

Its content will be filled by your web service in the runtime. Finish the template development.

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

Implement BAdI to consume the web service

Create a BAdI implementation via the customizing activity below:

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

You could refer to my implementation code below:

DATA: lo_proxy            TYPE REF TO co_zweb_add_21,

            ls_input            TYPE zweb_add_operation,

            ls_output           TYPE zweb_add_operationresponse,

            lo_payload_protocol TYPE REF TO if_wsprotocol_payload,

            lo_payload          TYPE REF TO if_ws_payload,

            lo_payload_handler  TYPE REF TO if_ws_payload_handler,

            lt_request_data     TYPE prx_t_param,

            ls_request_data     TYPE prx_s_param,

            lv_valid_from       TYPE comt_valid_from.

line 17: Create an instance of ABAP consumer proxy class.

line 21: The current business object instance GUID is included in importing parameter IS_OBJECT-INSTID.

Since my word template is created based on BOR type BUS1178, so I could directly query product database table COMM_PRODUCT to

find the corresponding product record.

line 24 ~ 27: I just fetch the valid from timestamp of current product and pass the date value to the first importing parameter of web service call. The second importing parameter is assigned with 0, so in the final word document we will see the valid from date of current product.

line 28: execute web service call, the result is included in parameter ls_output.

line 38 and 39: pass out the web service response payload to changing parameter, which will be used by Office integration framework to merge with word template.

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

Test in UI

Now you could create attachment based on your word template by clicking button “With Template”. Choose the word template you have just developed from pop up window:

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

you could still set breakpoint in your BAdI implementation and the function module exposed as web service to observe how they are consumed in the runtime:

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

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

Finally the web service calculation result is displayed in the word document.

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

Further reading


If you would like to know how a word template is merged with data from xml file, you can find technical detail in this document Understand how the word template is merged with xml data stream .

To report this post you need to login first.

8 Comments

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

  1. Piet Demeester

    This idea is very promising.

    However, I run into a problem.

    When I try it, and I create the template,

    I can select the WSDL operation: so far so good.

    But when I click on the ‘Start Designer’ button,

    an exception CX_SY_REF_IS_INITIAL is raised in CL_CRM_OI_DOCX_HELPER.PROCESS_WSSCHEMA_MAIN_NODE.

    I even have this issue when I follow this approach with a web service created using the Web Service Tool.

    Any ideas?

    (0) 
    1. Jerry Wang Post author

      Hello Demeester,

      Sorry for late reply. This is a known issue we identified recently. It could be fixed by note 2045009. And kindly get informed that currently it is only possible to consume external web service with single schema node in document template. Please find more detail about this limitation in consulting note 2045119. Feel free to contact me in case you meet with further issue.

      Best regards,

      Jerry

      (0) 
      1. Piet Demeester

        Hi Jerry,

        No need to apologize. Thanks for your help.

        After applying note note 2045009, and not checking the check box ‘Map Name’ when generating the web service as requested in the PDF file attached to note 2045119, I am able to start the template designer. So, that allowed me to move ahead.

        Further down the road, I ran into another issue though.

        As far as I can see, it seems there is an issue when calling transformation CRM_OFFICE_INT_RT_ADD_NS in CL_OI_HANDLER.FILL_TEMPLATE_WITH_DATA.

        The value in the lv_xpayload looks in my case like:

        <?xml version=”1.0″ encoding=”utf-8″ ?>

        <nm:ZWEB_ADD_OPERATIONResponse xmlns:nm=”urn:sap-com:(…)” xmlns:prx=”(…)”>

          <EV_RESULT>20130225</EV_RESULT>

        </nm:ZWEB_ADD_OPERATIONResponse>

        which after the transformation becomes:

        <?xml version=”1.0″ encoding=”utf-8″ ?>

        <n0:nm:ZWEB_ADD_OPERATIONResponse xmlns:n0=”urn:sap-com:document:sap:soap:functions:mc-style” xmlns:prx=”(…)”>

          <n0:EV_RESULT>20130225</n0:EV_RESULT>

        </n0:nm:ZWEB_ADD_OPERATIONResponse>

        which is invalid xml, as a name not contains twice a colon (:)

        When I change this transformation in our sandbox environment, by adding

        (…)

          <xsl:when test=”contains(name(),’nm:’)”>

                <xsl:element name=”{translate(name(),’nm:’,’n0:’)}”>

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

              </xsl:element>

              </xsl:when>

        (…)

        in the ‘choose’ block of that transformation, it works.

        This is of course not a general fix though.

        I am in contact with SAP support on this problem.

        Regards,

        Piet

        (0) 
        1. Jerry Wang Post author

          Hi Piet,

          Thanks a lot to try this solution. And thanks a lot to point out the typo error in the note. I have fixed it.

          Regarding your issue, I have done test in our internal system. The web service response before transformation:

          /wp-content/uploads/2014/08/clipboarda1_515178.png

          the response after transformation. The transformation just adds the namespace n0 to all subnodes which do not have that namespace yet.

          /wp-content/uploads/2014/08/clipboarda2_515179.png

          And regarding your response, currently I don’t know why you already have a namespace “nm” in the beginning. I will investigate this once I resolved lots of high priority incidents assigned to me recently 🙂

          Best regards,

          Jerry

          (0) 
          1. Piet Demeester

            Hi Jerry,


            An update from my side: I have been able to work around the issue by adding a transformation at the end of the processing in the implementation of IF_EX_CRM_OI_WEBSERVICE_BADI~CALL_WEB_SERVICE which replaces the nm by n0.

            As far as I see though, it is not correct that the transformation CRM_OFFICE_INT_RT_ADD_NS assumes the n0 notation. But anyway, this allows me to continue.


            Thanks,


            Piet

            (0) 
        2. sameer p

          HI Piet,

          I was going through the guide and I ran into the same issue as you. Your post was very useful and saved me much time in debugging. To make up for the time saved, i spent that time figuring out the root cause of the problem 🙂 :

          The issue is due to CL_PROXY_STYLESHEET_ST=>GENERATE , line :

          lr_message = xml_factory->create_list( name = main_message_namename  nsuri = main_message_namenamespace prefix = nm).

          Changing this fixed the issue. I haven’t found a relevant note for this. Hope it helps someone.

          Cheers

          (0) 
  2. Nicolas Busson

    Hello Jerry,

    Thanks a lot for you great series about MS Word templates… And I know it is been quite a while since you wrote them, but by any chance do you have any hint to “transform” exsting word documents into “CRM Word Templates” that consume WebService? Because in real life, when implementing SAP CRM from scratch, enterprises have a huge number of templates already and they just want to make them available in the system. The problem is: when you create a new Template in CRM at the moment you need to start from a blank page, and if you copy/paste the content from an existing document, most of the layout is gone…

    So if you have any tips & tricks to somehow enrich existing documents with the side panel to drag and drop webservice fields, and then upload the result in CRM that would be fantastic!

    Thanks a lot,

    Nicolas Busson.

    (0) 

Leave a Reply