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.
(1) Create Webservice enabled Adobe PDF attachment in CRM Application
(2) 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 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.
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。