Hi everybody,
in this blog post I'm going to show you how you are able to make a picture via a mobile application and store it as an attachment in SAP (SAPoffice). The blog post contains out of the following steps:
- General information
- Finding the storage place
- Encode and decode
- Creating the attachment
- Creating the linking
So let's start:
1. General information
SAPoffice is a mailing and also a filing system with which you are able to store and administrate files but also able to send files inside the SAP system or over the internet. I'll show you how you can make it to store a picture which was made with you mobile camera and save it as an attachment in the SAPoffice. The picture which is made via the phone's own camera is going to be sent as a base64 encoded String to the SAP system. Some of you may ask themselves, why I receive the image encoded as a base64 String. In the background there is a ICF-Service which handles HTTP-Requests. This request is also responsible for receiving the picture. The image itself is encoded at the mobile application and sent via POST-Request to the SAP system. This is the main reason why I have to deal with a String.
2. Finding the storage place
It's quite important for the SAP system where it hase to save the image. Therefore a function module called "SO_FOLDER_ROOT_ID_GET' is existing:
CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET' EXPORTING region = 'B' IMPORTING folder_id = lf_folder.
This FM returns the unique ID of the folder associated to the region which you set as an importing parameter.
3. Encode and decode
As already mentioned, the image is transfered to the SAP system as a base64 encoded String. For the further process this String is going to be converted into a XString. What is the difference? Both, String and XString are datatypes for objects with variable length. The only difference is that a String is for a sequence of text and the XString for a sequence of byte. The main conversion is done by a FM too. This one is called "SSCF_BASE64_DECODE":
CALL FUNCTION 'SSFC_BASE64_DECODE' EXPORTING b64data = is_meldung-pic1 IMPORTING bindata = lf_picture.
Internal, SAP stores all attachments as binary encoded files. This means that we have to get our XString into a binary form. This is done via the FM "SCMS_XSTRING_TO_BINARY":
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = lf_picture append_to_table = 'X' IMPORTING output_length = lf_size TABLES binary_tab = lt_content.
As a return value you get a table which contains the whole content of the picture encoded and the length of the encoded value itself.
4. Creating the attachment
The first step is to create an object of type "SODOCCHGI1". With this object you are able to provide general information like the description, language, size, datatype of the attachment and whether the file should be changeable or not for example. For creating the file you have to call the FM "SO_DOCUMENT_INSERT_API1":
CALL FUNCTION 'SO_DOCUMENT_INSERT_API1' EXPORTING folder_id = lf_folderid document_data = ls_document_data document_type = lf_doctype IMPORTING document_info = lf_docinfo TABLES CONTENTS_HEX = lt_content.
As importing parameters the FM takes the determined ID of the folder where the attachment should be stored, the object of type "SODOCCHGI1", the datatype of the object and the binary encoded table which stores the image itself. The return value is an object of type "SOFOLENTI1". This object contains the information of the unique created SAPoffice object. The last step is now to create the linking between the SAPoffice object and the SAP object itself.
5. Creating the linking
For making the correct linking between the two objects. For each of them you have to provide the following information:
- instid
This is nothing else then the unique number of the object.
- typeid:
This is the so called business object. Each object which can be created or can be stored through an application in SAP is a business object. A Notification for example has the ID 2038 --> "BUS2038".
- catid:
Nothing else than a short ID which stands for the category. In our case we're dealing with business objects --> "BO".
Finally we can now start with creating the link. The class "cl_binary_relation" provides a method called "create_link". This method takes the two created objects and builds as a relation type "ATTACHMENT" the linking. The complete linking procedure looks like following:
lo_object_a-instid = af_qmnum. lo_object_a-typeid = 'BUS2038'. lo_object_a-catid = 'BO'. lo_object_b-instid = lf_att_key. lo_object_b-typeid = 'MESSAGE'. lo_object_b-catid = 'BO'. TRY. CALL METHOD cl_binary_relation=>create_link EXPORTING is_object_a = lo_object_a is_object_b = lo_object_b ip_reltype = 'ATTA'. ENDTRY.
Now you picture should be attached in the attachment list for the corresponding SAP object.
For further questions just contact me!
Greetings
Stefan