Skip to Content
Author's profile photo Jerry Wang

Create word attachment which consumes external web service

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.

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 web service 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.


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.


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:



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 web service 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 drop down 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


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.


Implement BAdI to consume the web service

Create a BAdI implementation via the customizing activity below:


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.


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:


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:



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


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.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Jorge Weiss
      Jorge Weiss

      Thanks Jerry!


      Author's profile photo Piet Demeester
      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,


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

      Any ideas?

      Author's profile photo Jerry Wang
      Jerry Wang
      Blog 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,


      Author's profile photo Piet Demeester
      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="(...)">



      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="(...)">



      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()"/>




      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.



      Author's profile photo Jerry Wang
      Jerry Wang
      Blog 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:


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


      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,


      Author's profile photo Piet Demeester
      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.



      Author's profile photo Former Member
      Former Member

      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_name-name  nsuri = main_message_name-namespace prefix = 'nm' ).

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


      Author's profile photo Nicolas Busson
      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.

      Author's profile photo Dirk Jäckel
      Dirk Jäckel

      Hi Jerry,

      another very good document from you! Congratulation, once more!

      Because I'm not the profi in creating WEB-Services, it would be great (in my opinion) when you describe excatly which WSDL URL is meant when you write "own" WSDL URL...and where it could be found!

      So I found out that I've to insert the "binding" WSDL URL into the Document Template Designer - Screen which I found into SOAMANAGER - Screen.

      By the way:
      Which WSDL URL must be inserted when I've created the HTTP-Connection (TX: SM59) to prevent the Unauthorized Error message which you've described in another blog?

      Thanks and greetings from Germany!

      Dirk Jäckel