20
Developing SAP Applications: By: Ram Manohar Tiwari www.rmtiwari.com Table of Content Table of Content ........................................................................................................................ 1 Applies To: ................................................................................................................................. 2 Article Summary......................................................................................................................... 2 Solution Summary: ..................................................................................................................... 3 a) Database Table: .................................................................................................................. 3 b) SAP Business Objects: ....................................................................................................... 3 c) Dialogue Program & GOS (Generic Object Services): ....................................................... 3 d) SAP Business Workflow: .................................................................................................... 3 e) Data Upload Program: ........................................................................................................ 3 f) EDI / IDoc: ........................................................................................................................... 3 g) Mass Document Attachment Program ................................................................................ 3 h) Reports:............................................................................................................................... 3 Clarifying GOS concepts before starting the development: ......................................................... 4 GOS (Generic Object Service) in Action for Customer Business Object ‘ZFRIENDS’: ...... 4 Attaching documents to Business Objects in background / providing a custom GOS like functionality for BSPs: ............................................................................................................ 5 Sample Code of Program [ Only implemented for Notes and URLs ] .......................................... 6 Step By Step Solution details:.................................................................................................... 9 a) Database Table: .................................................................................................................. 9 b) Table Maintenance Generator: ........................................................................................... 9 c) Lock Object: ...................................................................................................................... 10 d) Number Range: ................................................................................................................. 11 e) SAP Business Objects: ..................................................................................................... 12 f) Dialogue Program & GOS (Generic Object Services): ..................................................... 14 Source Code for Xn ZFR01: ....................................................................................................... 16 g) SAP Business Workflow: .................................................................................................. 20 h) Data Upload Program: ...................................................................................................... 20 i) EDI / IDoc: ......................................................................................................................... 20 j) Mass Document Attachment Program .............................................................................. 20 k) Reports:............................................................................................................................. 20 Disclaimer & Liability Notice..................................................................................................... 20

Sap Application Development

  • Upload
    1j1j1j

  • View
    22

  • Download
    6

Embed Size (px)

DESCRIPTION

Sap Application Development procedures

Citation preview

  • Developing SAP Applications:

    By: Ram Manohar Tiwari

    www.rmtiwari.com

    Table of Content

    Table of Content ........................................................................................................................ 1 Applies To: ................................................................................................................................. 2 Article Summary......................................................................................................................... 2 Solution Summary:..................................................................................................................... 3

    a) Database Table:.................................................................................................................. 3 b) SAP Business Objects: ....................................................................................................... 3 c) Dialogue Program & GOS (Generic Object Services): ....................................................... 3 d) SAP Business Workflow: .................................................................................................... 3 e) Data Upload Program: ........................................................................................................ 3 f) EDI / IDoc:........................................................................................................................... 3 g) Mass Document Attachment Program................................................................................ 3 h) Reports:............................................................................................................................... 3 Clarifying GOS concepts before starting the development: ......................................................... 4

    GOS (Generic Object Service) in Action for Customer Business Object ZFRIENDS:...... 4

    Attaching documents to Business Objects in background / providing a custom GOS like functionality for BSPs:............................................................................................................ 5 Sample Code of Program [ Only implemented for Notes and URLs ].......................................... 6

    Step By Step Solution details:.................................................................................................... 9 a) Database Table:.................................................................................................................. 9 b) Table Maintenance Generator: ........................................................................................... 9 c) Lock Object: ...................................................................................................................... 10 d) Number Range:................................................................................................................. 11 e) SAP Business Objects: ..................................................................................................... 12 f) Dialogue Program & GOS (Generic Object Services): ..................................................... 14 Source Code for Xn ZFR01:....................................................................................................... 16 g) SAP Business Workflow: .................................................................................................. 20 h) Data Upload Program: ...................................................................................................... 20 i) EDI / IDoc:......................................................................................................................... 20 j) Mass Document Attachment Program.............................................................................. 20 k) Reports:............................................................................................................................. 20

    Disclaimer & Liability Notice..................................................................................................... 20

  • Applies To:

    This article can be helpful to the SAP Technical Consultants who are looking for an example application solution to understand the basics as well as advanced concepts of developing SAP applications.

    Article Summary

    Using a very simple (Non)Business-scenario, this article presents a step by step solution for developing SAP applications using Database Tables, Lock Objects, SAP Business Objects & GOS (Generic Object Services), Business Workflows and email from SAP. By: Ram Manohar Tiwari Title: SAP Applications Development: Business Objects & Workflows Date: 08 Oct 2005 Web-Link: Click on SAP Application Development - An Example to check out the latest version of this document.

    Requirement:

    To brush-up my skills is the reason & the requirement is to develop an SAP application in order to automate the process of sending Birthday Greetings to my friends on their Bday. This SAP application involves Business Object, GOS (Generic Object Services), SAP Business Workflows and email from SAP. Through this application, I should be able to maintain the Bday & email details of my Friends. Further, workflow should remind me a day before Bday arrives, through a workitem in my SAP Inbox. I can then attach the greetings as a URL or a document. Further, a day after, on actual Bday, the business workflow will send all the attached greetings to my friend as an email attachment.

  • Solution Summary:

    Database Table: A database table ZFRIENDS will be created to keep the persistent information about Friends e.g. Birthday & email-id, against their generated ID. Lock Object: for the table will be created. Number Range: for the Friend-ID will also be maintained. SAP Business Objects: Friends info and operations required will be encapsulated in a SAP Business Object called ZFRIENDS'. Its key and attributes will correspond to the underlying database table ZFRIENDS. Basically, Friend-ID will be the key for Business Object as well as primary key of table ZFRIENDS. Dialogue Program & GOS (Generic Object Services): A Dialogue Program with Transaction ZFR01 will be developed to maintain information of Friends. This program will implement GOS ( Generic Object Services ) for the related Business Object, in order to attach documents along with Friends info. While saving a new Friends info, it will generate a new ID using a number range, update the table and will also trigger an event ZFRIENDS.Created. This transaction will also be called from the method on Business Object ZFRIENDS.Create. SAP Business Workflow: A workflow will be developed which will be triggered at the creation of a new friends info [ Event ZFRIENDS.Created ]. This workflow instance will send a workitem, a day before my Friends Bday. I can attach greeting cards (documents) by executing the workitem which in turn opens Xn ZFR01. Further, on actual Bday, the workflow will send all the attached greetings to my friend as an email attachment. Data Upload Program: A batch upload program, capable of running in foreground as well as in background, will be developed, to create new Friends info documents in SAP. Input data will come from a tab delimited file on PC (Foreground) or Application Server (Background). Rather than making a BDC of ZFR01, a BAPI will be developed and called to create new Friends Info. This BAPI FM will also be used while implementing a no dialogue method ZFRIENDS.CreateFromData. EDI / IDoc: In case EDI/IDoc inbound/outbound communication is required to integrate SAP system with another System, then a message type/IDoc Type can be generated for Friends Info, using the BAPI created in last step. A distribution model will be maintained to enable data-exchange between SAP and external systems. Mass Document Attachment Program: A batch report will be developed, capable of running in background, for mass attachment of URLs to Friends info document. This Report can take inputs maintained against the friends id in a tab delimited file on application server. Reports: A simple ALV report will be written to show information for a range of Friends. Further, in the report, you can click on Friends ID to navigate to Xn ZFR01 and display / modify the attachments.

  • Clarifying GOS concepts before starting the development:

    GOS (Generic Object Service) in Action for Customer Business Object ZFRIENDS:

    The transaction ZFR01, after enabling it for GOS, will show an icon for GOS toolbar in the left-most corner. You can click on it to attach documents in reference to the current business object instance. E.g. see the screen-shots later on, in this case Friend with ID - 0003 has documents and notes attached with it. Some extra coding is required to enable GOS toolbar in the transaction. Declaration in Top Include : * Object Service class cl_gos_manager definition load. data: lo_friends_gos type ref to cl_gos_manager, ls_object type borident. Now in PAI: of previous screen: * In case of Change, key is known. But in case of Create Key is not * created as yet. In that case id would be initial and key will be set later * on, when available after save. ls_object-objtype = 'ZFRIENDS'. ls_object-objkey = ' '. create object lo_friends_gos exporting is_object = ls_object ip_no_instance = 'X' exceptions others = 1. After Save, when key is available in case of create and immediately in case of change * For Object Service ls_object-objkey = ZFRIENDS-ID. call method lo_friends_gos->set_id_of_published_object EXPORTING is_object = ls_object. ENDIF.

  • Attaching documents to Business Objects in background / providing a custom GOS like functionality for BSPs:

    Since GOS can only be used with SAPGui Front-end, mass-attachment of documents (in background) cant be handled with GOS. However, the basic applications (Classes & Methods), used in GOS, can be utilized to create a custom program for this purpose. Since this new program will have the ability to run, irrespective of front-end, the same code can also be utilized to provide a GOS like facility in BSPs or while developing an upload program for attaching documents/URLs to Business Objects, in background. Basically, inside the application, main business object and attachment, both are treated as Business Objects and then a link is maintained between both the object instances. The relationship type, while maintaining the link, describes whether the attached object is a URL or a file attachment and so on. However, while the main business object to which you are trying to attach the document / URL is already known e.g. for Purchase order and hence the Object type ('BUS2012 )is known and instance (?) exists in database but the instance for the attachment has to be created first before the linking. Attachment can be a URL or a File and so you need to first upload (in case of file), and in the process also get a business object instance generated of BO type MESSAGE. The program-processing will have following steps:

  • Upload the File to be attached or in case of URL just get the URL name. In case of mass upload, the input can be read from a data-file on application server, having reference (e.g. URL / File Path ) against the Business Object key ( e.g. Purchase Order Number ). Create an instance of BO type MESSAGE using BO Method MESSAGE.Create. In case you are not comfortable with BO macros, simply call the main FM used within. The important part here is to pass the document type e.g. URL, EXT (for external files), and contents of file as well the file type ( e.g. TXT, PDF ) in case of File attachments. Here the document type can be derived from the relationship type of the link. Now, the attachment is created in database as a MESSAGE and instance is known. Well refer to this as object_b and main Business Object as objet_a. Now, maintain the link. Check the attached documents through GOS toolbar Attachment List, using main business objects transaction e.g. ME23N for Purchase Orders.

    Sample Code of Program [ Only implemented for Notes and URLs ]

    *---------------------------------------------------------------------* * Report Z_RMTIWARI_ATTACH_DOC_TO_BO *---------------------------------------------------------------------* * Written By : Ram Manohar Tiwari * Function : We need to maintain links between Business Object and * the attachment. Attachment document is basiclally a * business object of type 'MESSAGE'.In order to maintain * links, first the attachment will be crated as Business * Object of type 'MESSAGE' using Message.Create method. * Need to check if we can also use FM * 'SO_DOC_INSERT_WITH_ORIG_API1' or SO_OBJECT_INSERT rather * than using Message.Create method. *---------------------------------------------------------------------* REPORT Z_RMTIWARI_ATTACH_DOC_TO_BO . * Include for BO macros INCLUDE : . * Load class. CLASS CL_BINARY_RELATION definition load. CLASS CL_OBL_OBJECT definition load. PARAMETERS: * Object_a P_BOTYPE LIKE obl_s_pbor-typeid DEFAULT 'ZFRIENDS', " e.g. 'BUS2012' P_BO_ID LIKE OBL_S_PBOR-INSTID DEFAULT '00007', " Key e.g. PO No. * Object_b P_DOCTY LIKE obl_s_pbor-typeid DEFAULT 'MESSAGE' NO-DISPLAY, P_MSGTYP LIKE SOFM-DOCTP DEFAULT 'URL' NO-DISPLAY, * Relationship P_RELTYP LIKE mdoblrel-reltype DEFAULT 'URL'.

  • types: BEGIN OF ty_message_key, FOLTP TYPE SO_FOL_TP, FOLYR TYPE SO_FOL_YR, FOLNO TYPE SO_FOL_NO, DOCTP TYPE SO_DOC_TP, DOCYR TYPE SO_DOC_YR, DOCNO TYPE SO_DOC_NO, FORTP TYPE SO_FOR_TP, FORYR TYPE SO_FOR_YR, FORNO TYPE SO_FOR_NO, END OF ty_message_key. DATA : lv_message_key type ty_message_key. DATA : lo_message type swc_object. DATA : lt_doc_content type standard table of SOLI-LINE with header line. * First derive the Attachment's ( MESSAGE )document type. P_DOCTY = 'MESSAGE'. CASE P_RELTYP. * In case of URls WHEN 'URL'. P_MSGTYP = 'URL'. * In case of Notes / Private Notes WHEN 'NOTE' OR 'PNOT'. P_MSGTYP = 'RAW'. * In case of file attachments WHEN 'ATTA'. P_MSGTYP = 'EXT'. * Not implemented as yet...exit EXIT. WHEN OTHERS. * ....exit EXIT. ENDCASE. *----------------------------------------------------------------* * Create an initial instance of BO 'MESSAGE' - to call the * instance-independent method 'Create'. swc_create_object lo_message 'MESSAGE' lv_message_key. * define container to pass the parameter values to the method call * in next step. swc_container lt_message_container. * Populate container with parameters for method swc_set_element lt_message_container 'DOCUMENTTITLE' 'Title'. swc_set_element lt_message_container 'DOCUMENTLANGU' 'E'. swc_set_element lt_message_container 'NO_DIALOG' 'X'. swc_set_element lt_message_container 'DOCUMENTNAME' P_DOCTY. swc_set_element lt_message_container 'DOCUMENTTYPE' P_MSGTYP. * 'DocumentContent' is a multi-line element ( itab ). * In case of URLs..it should be concatenated with &KEY& in the begining. CASE P_MSGTYP. WHEN 'URL'. lt_doc_content = '&KEY&http://www.rmtiwari.com' . append lt_doc_content.

  • * In case of Notes or Private Notes, get the data from files on appl * server or from wherever(? - remember background). WHEN 'RAW'. lt_doc_content = 'Hi How r u?' . append lt_doc_content. * In case of File attachments WHEN 'EXT'. * Upload the file contents using open dataset in lt_doc_content . * Some conversion ( Compress ) might be required. * Not sure at this point ENDCASE. swc_set_element lt_message_container 'DocumentContent' lt_doc_content. swc_call_method lo_message 'CREATE' lt_message_container. * Refresh to get the reference of create 'MESSAGE' object for attachment swc_refresh_object lo_message. * Get Key of new object swc_get_object_key lo_message lv_message_key. * Now we have attachment as a business object instance. We can now * attach it to our main business object instance. * Create main BO object_a data: lo_is_object_a type SIBFLPORB. lo_is_object_a-INSTID = P_BO_ID. lo_is_object_a-TYPEID = P_BOTYPE. lo_is_object_a-CATID = 'BO'. * Create attachment BO object_b data: lo_is_object_b type SIBFLPORB. lo_is_object_b-INSTID = lv_message_key. lo_is_object_b-TYPEID = P_DOCTY. lo_is_object_b-CATID = 'BO'. *TRY. CALL METHOD CL_BINARY_RELATION=>CREATE_LINK EXPORTING IS_OBJECT_A = lo_is_object_a IS_OBJECT_B = lo_is_object_b IP_RELTYPE = P_RELTYP . *CATCH CX_OBL_PARAMETER_ERROR . *CATCH CX_OBL_MODEL_ERROR . *CATCH CX_OBL_INTERNAL_ERROR . *ENDTRY. * Check if everything OK...who cares!! commit work.

  • Step By Step Solution details:

    Database Table: The database table is created using Xn SE11 and it contains fields as below: This is a client dependent table and hence primary keys are MANDT & ID. Name, Date of Birth and email are other fields in the table.

    Table Maintenance Generator: This can be used to get a user interface generated for creating/updating records in the table directly. Then Xn SM30 can be used to update records in table directly. However, this is just for knowledge sake as self-developed transaction ZFR01 will be used in place of SM30.

  • Lock Object: for the table will be created using Xn SE11. This will be used to lock the relevant table row before updating the tables using transaction ZFR01.

  • Number Range: for the Friend-ID will also be maintained using Xn SNRO. This will be used to get the next number for Friend-ID at the time of new Friends Info creation through Xn ZFR01.

  • SAP Business Objects: Friends info and operations required will be encapsulated in a SAP Business Object called ZFRIENDS'. Its key and attributes will correspond to the underlying database table ZFRIENDS. Basically, Friend-ID will be the key for Business Object as well as primary key of table ZFRIENDS. Use Xn SWO1 to create a new Business Object ZFRIENDS.

  • Dialogue Program & GOS (Generic Object Services): A Dialogue Program with Transaction ZFR01 will be developed to maintain information of Friends. This program will implement GOS ( Generic Object Services ) for the related Business Object, in order to attach documents along with Friends info. While saving a new Friends info, it will generate a new ID using a number range, update the table and will also trigger an event ZFRIENDS.Created. This transaction will also be called from the method on Business Object ZFRIENDS.Create. Final Transaction:

    1st Screen 100:

    2nd Screen 1000:

  • Source Code for Xn ZFR01:

    *&---------------------------------------------------------------------* *& Module pool Z_RMTIWARI_FRIENDS * *& * *&---------------------------------------------------------------------* *& * *& * *&---------------------------------------------------------------------* INCLUDE ZFR_TOP . " global Data * INCLUDE ZFR_O01 . " PBO-Modules * * INCLUDE ZFR_I01 . " PAI-Modules * * INCLUDE ZFR_F01 . " FORM-Routines * *&---------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE STATUS_0100 OUTPUT. SET PF-STATUS 'ZFRIENDS1'. SET TITLEBAR 'T1'. check not lo_friends_gos is initial. call method lo_friends_gos->UNPUBLISH. ENDMODULE. " STATUS_0100 OUTPUT *&---------------------------------------------------------------------* *& Module USER_COMMAND_0100 INPUT

  • *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE USER_COMMAND_0100 INPUT. CASE sy-ucomm. WHEN 'CREATE'. CLEAR : ZFRIENDS. * Object Service ls_object-objtype = 'ZFRIENDS'. ls_object-objkey = ' '. create object lo_friends_gos exporting is_object = ls_object ip_no_instance = 'X' exceptions others = 1. CALL SCREEN 1000. WHEN 'CHANGE'. CLEAR : ZFRIENDS-NAME, ZFRIENDS-DOB. SELECT SINGLE * FROM ZFRIENDS WHERE ID eq ZFRIENDS-ID. if sy-subrc ne 0. message E003(aq) with 'Please enter a valid ID'. else. * In case of change , lock the table CALL FUNCTION 'ENQUEUE_EZFRIENDS' EXPORTING * MODE_ZFRIENDS = 'S' * MANDT = SY-MANDT ID = ZFRIENDS-ID * X_ID = ' ' * _SCOPE = '2' * _WAIT = ' ' * _COLLECT = ' ' * EXCEPTIONS * FOREIGN_LOCK = 1 * SYSTEM_FAILURE = 2 * OTHERS = 3 . IF SY-SUBRC 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. * Object Service ls_object-objtype = 'ZFRIENDS'. ls_object-objkey = ' '. create object lo_friends_gos exporting is_object = ls_object ip_no_instance = 'X' exceptions others = 1.

  • * For Object Service ls_object-objkey = ZFRIENDS-ID. call method lo_friends_gos->set_id_of_published_object EXPORTING is_object = ls_object. ENDIF. CALL SCREEN 1000. WHEN 'BACK'. LEAVE TO SCREEN 0. WHEN 'CANCEL'. ENDCASE. ENDMODULE. " USER_COMMAND_0100 INPUT *&---------------------------------------------------------------------* *& Module STATUS_1000 OUTPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE STATUS_1000 OUTPUT. SET PF-STATUS 'ZFRIENDS'. SET TITLEBAR 'T1'. * In case of create IF ZFRIENDS-ID IS INITIAL. check not lo_friends_gos is initial. * Object Service ls_object-objtype = 'ZFRIENDS'. ls_object-objkey = ' '. create object lo_friends_gos exporting is_object = ls_object ip_no_instance = 'X' exceptions others = 1. ENDIF. ENDMODULE. " STATUS_1000 OUTPUT *&---------------------------------------------------------------------* *& Module USER_COMMAND_1000 INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE USER_COMMAND_1000 INPUT. CASE sy-ucomm. WHEN 'SAVE'. * In Case of create IF ZFRIENDS-ID IS INITIAL. * Get next friend's id from number range CALL FUNCTION 'NUMBER_GET_NEXT' EXPORTING NR_RANGE_NR = '01' OBJECT = 'ZFRIENDS' * QUANTITY = '1'

  • * SUBOBJECT = ' ' * TOYEAR = '0000' * IGNORE_BUFFER = ' ' IMPORTING NUMBER = ZFRIENDS-ID * QUANTITY = * RETURNCODE = EXCEPTIONS INTERVAL_NOT_FOUND = 1 NUMBER_RANGE_NOT_INTERN = 2 OBJECT_NOT_FOUND = 3 QUANTITY_IS_0 = 4 QUANTITY_IS_NOT_1 = 5 INTERVAL_OVERFLOW = 6 BUFFER_OVERFLOW = 7 OTHERS = 8 . IF SY-SUBRC 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. * Object Service - pass the key of business object ZFRIENDS's * instance to global service manager ls_object-objtype = 'ZFRIENDS'. ls_object-objkey = ZFRIENDS-ID. call method lo_friends_gos->set_id_of_published_object EXPORTING is_object = ls_object. ENDIF. MODIFY ZFRIENDS. if sy-subrc eq 0. message s003(aq) with 'Friend ' ZFRIENDS-ID 'Created/Updated'. endif. IF NOT ZFRIENDS-ID IS INITIAL. CALL FUNCTION 'DEQUEUE_EZFRIENDS' EXPORTING * MODE_ZFRIENDS = 'S' * MANDT = SY-MANDT ID = ZFRIENDS-ID * X_ID = ' ' * _SCOPE = '3' * _SYNCHRON = ' ' * _COLLECT = ' ' . ENDIF. commit work and wait. WHEN 'BACK'. call method lo_friends_gos->UNPUBLISH. LEAVE TO SCREEN 0.

  • WHEN 'CANCEL'. call method lo_friends_gos->UNPUBLISH. LEAVE TO SCREEN 0. WHEN 'EXIT'. call method lo_friends_gos->UNPUBLISH. EXIT. ENDCASE. ENDMODULE. " USER_COMMAND_1000 INPUT SAP Business Workflow: A workflow will be developed which will be triggered at the creation of a new friends info [ Event ZFRIENDS.Created ]. This workflow instance will send a workitem, a day before my Friends Bday. I can attach greeting cards (documents) by executing the workitem which in turn opens Xn ZFR01. Further, on actual Bday, the workflow will send all the attached greetings to my friend as an email attachment. Data Upload Program: A batch upload program, capable of running in foreground as well as in background, will be developed, to create new Friends info documents in SAP. Input data will come from a tab delimited file on PC (Foreground) or Application Server (Background). Rather than making a BDC of ZFR01, a BAPI will be developed and called to create new Friends Info. This BAPI FM will also be used while implementing a no dialogue method ZFRIENDS.CreateFromData. EDI / IDoc: In case EDI/IDoc inbound/outbound communication is required to integrate SAP system with another System, then a message type/IDoc Type can be generated for Friends Info, using the BAPI created in last step. A distribution model will be maintained to enable data-exchange between SAP and external systems. Mass Document Attachment Program: A batch report will be developed, capable of running in background, for mass attachment of URLs to Friends info document. This Report can take inputs maintained against the friends id in a tab delimited file on application server. Reports: A simple ALV report will be written to show information for a range of Friends. Further, in the report, you can click on Friends ID to navigate to Xn ZFR01 and display / modify the attachments.

    Disclaimer & Liability Notice

    Obviously, www.rmtiwari.com will not be held liable for any damages caused due to use or misuse of code and methods, and anyone using the given solutions, is doing it under his/her own responsibility.

    By : Ram Manohar Tiwari

    www.rmtiwari.com