Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Back to Sending Mails - Home Page

Applies to:

SAP  Netweaver 7.0 and Higher

Summary

This article explains about the Publishing a webservice in R/3, Consuming a Webservice in R/3 and Consuming the same webservice in ADOBE flex.

Author(s):  

   
Company:      Kaar Technologies - SAP Netweaver Application development Consultant
Created on:   Sep 9th 2009
Author(s) Bio:

 Rajkumar Shanmuganathan is a SAP Certified Netweaver Application Consultant worked on various areas like  OO-ABAP,Workflow, Business server pages, ABAP  webdynpro, and Adobe Flex and was involved in SAP implementations for around 4+ years.

Publishing a Webservice in R/3.

Create a RFC enabled Function module that will send Email to the person based on the input parameters.







Check whether your mail is being sent in SOST transaction. Once SAP BASIS Team configures mail connections in SCOT transaction, all the mails in the Queue will be sent to the external

world.

 
 
 
 

Source code for the Function module are as follows:

FUNCTION ZKAAR_PUBLISH_WEB_SERVICE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(FROM) TYPE  STRING
*"     VALUE(TO) TYPE  STRING
*"     VALUE(MESSAGE) TYPE  STRING
*"     VALUE(SUBJECT) TYPE  STRING
*"  EXPORTING
*"     VALUE(RETURN) TYPE  STRING
*"----------------------------------------------------------------------

*----------------------------------------------------------------------*
* CLASS-DEFINITIONS                                                    *
*----------------------------------------------------------------------*
  DATA: SEND_REQUEST       TYPE REF TO CL_BCS.
  DATA: DOCUMENT           TYPE REF TO CL_DOCUMENT_BCS.
  DATA: SENDER             TYPE REF TO IF_SENDER_BCS.
  DATA: RECIPIENT          TYPE REF TO IF_RECIPIENT_BCS.
  DATA: EXCEPTION_INFO     TYPE REF TO IF_OS_EXCEPTION_INFO,
        BCS_EXCEPTION      TYPE REF TO CX_BCS.

*----------------------------------------------------------------------*
* INTERNAL TABLES                                                      *
*----------------------------------------------------------------------*
  DATA: L_MAILTEXT TYPE SOLI_TAB.

  TRY.
* Create persistent send request
      SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

* Move the Subject from string to BCS subject type
      DATA: L_SUBJECT TYPE SO_OBJ_DES.
      L_SUBJECT = SUBJECT.

* Move the message from string to internal table
      CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
        EXPORTING
          TEXT      = MESSAGE
        TABLES
          FTEXT_TAB = L_MAILTEXT.

* Create the Document
      DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
                          I_TYPE    = 'RAW'
                          I_TEXT    = L_MAILTEXT
                          I_SUBJECT = L_SUBJECT ).

* Add document to send request
      CALL METHOD SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).

* Get sender object
      DATA: L_SENDER TYPE AD_SMTPADR.
      L_SENDER = FROM.
      SENDER = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( L_SENDER ).

* Add sender
      CALL METHOD SEND_REQUEST->SET_SENDER
        EXPORTING
          I_SENDER = SENDER.

* Get Recipient Object
      DATA: L_RECIPIENT TYPE AD_SMTPADR.
      L_RECIPIENT = TO.
      RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( L_RECIPIENT

).

* Add recipient with its respective attributes to send request
      CALL METHOD SEND_REQUEST->ADD_RECIPIENT
        EXPORTING
          I_RECIPIENT = RECIPIENT.

* Set that you don't need a Return Status E-mail
      CALL METHOD SEND_REQUEST->SET_STATUS_ATTRIBUTES
        EXPORTING
          I_REQUESTED_STATUS = 'E'
          I_STATUS_MAIL      = 'E'.

* set send immediately flag
      SEND_REQUEST->SET_SEND_IMMEDIATELY( 'X' ).

* Send document
      CALL METHOD SEND_REQUEST->SEND( ).

      COMMIT WORK.

      IF SY-SUBRC EQ 0.
        RETURN = 'Message Sent Successfully'.
       ELSE.
        RETURN = 'Error in Sending Email'.
      ENDIF.
    CATCH CX_BCS INTO BCS_EXCEPTION.
*      data: l_message type string.
*      l_message = bcs_exception->GET_TEXT( ).
  ENDTRY.

ENDFUNCTION.







Now this Function module has to be published as a Webservice.

Once you complete the above steps, go to transaction soamanager** and check whether your webservice is available.

Click the tab Application and Scenario communication and click the hyperlink Single service Administration.

Click Apply selection.

 Click Show WSDL options and change the settings as below,

Click the hyperlink Open WSDL document for selected bindings to see the generated WSDL file.

 

Consuming webservice in R/3.

 

First of all we need to check whether the above published webservice can be consumed inside R/3. That makes easy to integrate with other technologies such as Flex, .net...etc

Create a client Proxy.

Enter the URL that was generated from SOAMANAGER Transaction.

Once you complete the above steps, activate the generated proxy class.



Create a logical port for the generated proxy class in order to consume the Webservice in the transaction LPCONFIG.

Provide the URL of the WSDL location in the call parameters tab.



 Creating a simple Report to check the consumption of webservice:

The above report will have the following code.

REPORT  ZKAAR_WEB_CONSUME_REPORT.

PARAMETER: ZFROM     TYPE STRING,
           ZTO       TYPE STRING,
           ZMESSAGE  TYPE STRING,
           ZSUBJECT  TYPE STRING.

DATA: ZPROXY       TYPE REF TO ZKAARTECH_CO_ZKAAR_MAIL_WEB_SE,
      ZINPUT       TYPE ZKAARTECH_ZKAAR_PUBLISH_WEB_S1,
      ZOUTPUT      TYPE ZKAARTECH_ZKAAR_PUBLISH_WEB_SE,
      ZCONTROLLER  TYPE PRXCTRLTAB.

TRY.
    CREATE OBJECT ZPROXY.
  CATCH CX_AI_SYSTEM_FAULT .
ENDTRY.

ZINPUT-CONTROLLER = ZCONTROLLER.
ZINPUT-FROM       = ZFROM.
ZINPUT-TO         = ZTO.
ZINPUT-MESSAGE    = ZMESSAGE.
ZINPUT-SUBJECT   =  ZSUBJECT.

TRY.

    CALL METHOD ZPROXY->ZKAAR_PUBLISH_WEB_SERVICE
      EXPORTING
        INPUT  = ZINPUT
      IMPORTING
        OUTPUT = ZOUTPUT.

  CATCH CX_AI_SYSTEM_FAULT .
ENDTRY.

IF ZOUTPUT IS INITIAL.

  WRITE: 'Message Sent successfully'.

ENDIF.

Check whether your mail is being sent in SOST transaction as before. Once SAP BASIS Team configures mail connections in SCOT transaction, all the mails in the Queue will be transferred to the external world.


The above step confirms that Webservice is successfully consumed inside R/3.

Consuming Webservice in ADOBE Flex.

Create a Flex Project in the Flex Builder 3.0.



Click Data in the menu path and select import WSDL,

Paste the WSDL link generated from SAP,


 


 

Once you import the WSDL file successfully package will be created in your Flex project.


 

Just create a simple Design that takes the input from the flex and processes the same with webservice.


 

Parameter is returned from SAP.


 

Add the following code in the Source Tab.

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>

<![CDATA[

import mx.controls.Alert;

import com.kaartechsap.;;

var zservice:Service=new Service();

public function sendmail():void

**
{
zservice.zkaarPublishWebService(zFrom.text,zMessage.text,zSubject.text,zTo.text);
Alert.show(zservice.zkaarPublishWebService_lastResult.toString());
}
]]>

</mx:Script>

<mx:Canvas x="140" y="49" width="308" height="351" borderColor="#090A0B" backgroundColor="#DAEFF2">

<mx:Form x="21.5" y="50" width="265" height="291" backgroundColor="#C6C4B8">

<mx:FormItem label="From" >

<mx:TextInput id="zFrom"/>

</mx:FormItem>

<mx:FormItem label="To" >

<mx:TextInput id="zTo"/>

</mx:FormItem>

<mx:FormItem label="Subject" >

<mx:TextInput id="zSubject"/>

</mx:FormItem>

<mx:FormItem label="Message" >

<mx:TextInput id="zMessage" height="112"/>

</mx:FormItem>

<mx:Button label="Send" click="sendmail()"/>

</mx:Form>

<mx:Label x="30" y="24" text="Send Email From Flex Application" width="256.5" fontWeight="bold" fontSize="13"/>

</mx:Canvas>

</mx:Application>

There will be a delay in getting the response, so set busy cursor in Flex code. With the above code I have to click the button twice to get the response.

Check in the SOST transaction in SAP you will get the mails in Queue.

Related Content

Please include at least three references to SDN documents or web pages.

http://help.sap.com/saphelp_47x200/helpdata/en/ee/23df3a750f6a02e10000000a11405a/frameset.htm
http://saptechnical.com/Tutorials/Others/WebService/Flex.htm

http://www.sdn.sap.com/irj/scn/weblogs;jsessionid=(J2EE3417800)ID1031504650DB01409508375869655617End?blog=/pub/wlg/1645