Can we send SMS to a phone using ABAP?
Yes we can.
First we have to do the SMS configurations. SAP SMS Configurationwill let you know how to do configurations.
Once you have done the configurations you can do the coding. Here a simple program to send SMS using ABAP. SWN_TEST_SEND_SMS1 is also available as a standard program. Here you can send only single line SMS. If you want to send SMS with separate lines you have to pass the i_type as'RAW' when create_document. That's it. If you have done the SMS configurations correctly, sending SMS is every easy and will be useful too.
Here is the simple coding that I have written....
*&---------------------------------------------------------------------*
*& Report Z_SEND_SMS
*&---------------------------------------------------------------------*
*& DESC: Test program for sending sms notifications *
*& DEVELOPER: Rukshani
*& DATE: 13.09.2014
*&---------------------------------------------------------------------*
REPORT z_send_sms NO STANDARD PAGE HEADING.
* INCLUDES ------------------------------------------------------------*
* TABLES - SAP TABLES--------------------------------------------------*
TABLES sscrfields.
* INTERNAL TABLES------------------------------------------------------*
DATA: it_note TYPE STANDARD TABLE OF txw_note,
wa_note LIKE LINE OF it_note.
* FLAGS----------------------------------------------------------------*
* VARIABLES------------------------------------------------------------*
* SELECTION SCREEN
SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS: p_addr TYPE ad_pagnmbr OBLIGATORY. " Mobile number
PARAMETERS: p_subj TYPE so_obj_des. " Subject of sms
SELECTION-SCREEN BEGIN OF LINE. " Body of sms single-line/multi-line
SELECTION-SCREEN COMMENT (33) FOR FIELD p_body.
PARAMETERS: p_body TYPE soli-line.
SELECTION-SCREEN PUSHBUTTON 81(4) text-001 USER-COMMAND cli2 .
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b1.
* INITIALIZATION-------------------------------------------------------*
* AT SELECTION SCREEN--------------------------------------------------*
AT SELECTION-SCREEN.
IF sscrfields-ucomm = 'CLI2'.
CALL FUNCTION 'TXW_TEXTNOTE_EDIT'
TABLES
t_txwnote = it_note.
ENDIF.
* TOP OF PAGE----------------------------------------------------------*
* END OF PAGE----------------------------------------------------------*
* START OF SELECTION---------------------------------------------------*
* AT USER-COMMAND------------------------------------------------------*
* END OF SELECTION-----------------------------------------------------*
END-OF-SELECTION.
IF it_note[] IS INITIAL.
APPEND p_body TO it_note.
ENDIF.
PERFORM send_sms TABLES it_note USING p_addr p_subj .
*&---------------------------------------------------------------------*
*& Form SEND_SMS
*&---------------------------------------------------------------------*
* Send SMS
*----------------------------------------------------------------------*
* -->P_IT_NOTE SMS text
* -->P_ADDR Mobile No
* -->P_SUBJ Subject
*----------------------------------------------------------------------*
FORM SEND_SMS TABLES I_NOTE
USING I_NUMBER I_SUBJECT.
* Data definition
DATA: lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_dockey TYPE soodk,
lo_recipients TYPE bcsy_re,
lo_bcs_exception TYPE REF TO cx_bcs,
i_service TYPE ad_pagserv,
i_recipient TYPE REF TO if_recipient_bcs,
i_text TYPE soli_tab,
wa_text TYPE soli_tab WITH HEADER LINE,
i_length TYPE so_obj_len.
LOOP AT i_note INTO wa_note.
i_length = i_length + STRLEN( wa_note ).
MOVE-CORRESPONDING wa_note TO wa_text.
APPEND wa_text TO i_text.
ENDLOOP.
TRY.
* handle request
lo_send_request = cl_bcs=>create_persistent( ).
* set 'send immediately' -> no send queue required
lo_send_request->set_send_immediately( 'X' ).
* create document
lo_document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = i_text
i_language = sy-langu
i_subject = i_subject
i_length = i_length ).
* add document to send request
CALL METHOD lo_send_request->set_document( lo_document ).
* document key
lo_dockey-objtp = lo_document->get_doctp( ).
lo_dockey-objyr = lo_document->get_docyr( ).
lo_dockey-objno = lo_document->get_docno( ).
TRY.
* create SMS/pager recipient
CALL METHOD cl_cam_address_bcs=>create_sms_address
EXPORTING
i_service = i_service
i_number = i_number
RECEIVING
result = i_recipient.
* add recipient to send request
CALL METHOD lo_send_request->add_recipient
EXPORTING
i_recipient = i_recipient.
CATCH cx_address_bcs cx_send_req_bcs.
MESSAGE e000(yv01) WITH 'Unable to add SMS address' i_number.
EXIT.
ENDTRY.
* add recipient with its respective attributes to send request
CALL METHOD lo_send_request->add_recipient
EXPORTING
i_recipient = i_recipient
i_express = 'X'.
* check if document has recipients at all
lo_recipients = lo_send_request->recipients( ).
IF lo_recipients IS INITIAL.
MESSAGE e040(swn).
EXIT.
ELSE.
* sms has been sent
CALL METHOD lo_send_request->send( ).
COMMIT WORK.
MESSAGE s000(yv01) WITH 'SMS has been sent to' i_number.
ENDIF.
CATCH cx_bcs INTO lo_bcs_exception.
* error messages
MESSAGE e034(salert) WITH lo_bcs_exception->error_type.
MESSAGE e041(salert) WITH i_number.
EXIT.
ENDTRY.
ENDFORM. " SEND_SMS
Thanks