Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos

Hello!

Since you have a task to attach a file to HR object you may spend a lot of time learning how to do it.

Once I did it I wanted to keep this knowledge here to return here when required.

Actualy here is just 1 method implementation to do it:

class-methods ATTACH_FILE_TO_THE_HROBJID

     importing

       !IV_OBJID type HROBJID

       !IV_OTYPE type OTYPE

       !IV_FILENAME type STRING

       !IV_DESCRIPT type STRING

       !IR_LOGINST type ref to CL_CCMS_APPLOG optional

       !IV_HEXCONT type XSTRING

       !IV_DOCTYPE type SO_OBJ_TP default 'PDF' .


METHOD attach_file_to_the_hrobjid.

   DATA: lv_msgty TYPE msgty,

         lv_msgid TYPE msgid,

         lv_msgno TYPE symsgno,

         lv_msgv1 TYPE msgv1,

         lv_msgv2 TYPE msgv2,

         lv_msgv3 TYPE msgv3.

   DEFINE add_log_msg.

     if ir_loginst is supplied.

       lv_msgty = &1.

       lv_msgid = &2.

       lv_msgno = &3.

       lv_msgv1 = &4.

       lv_msgv2 = &5.

       lv_msgv3 = &6.

       ir_loginst->add_message(

                     ip_msg_type = lv_msgty

                     ip_msg_id   = lv_msgid

                     ip_msg_no   = lv_msgno

                     ip_msg_v1   = lv_msgv1

                     ip_msg_v2   = lv_msgv2

                     ip_msg_v3   = lv_msgv3 ).

     endif.

   END-OF-DEFINITION.

*  Check if the object exists

*  The fastest way is to access the DB table

   DATA: lv_plvar TYPE hrp1000-plvar.

   SELECT SINGLE plvar INTO lv_plvar

     FROM hrp1000

     WHERE plvar = cl_hap_pmp_const=>plvar and

           otype = iv_otype AND

           objid = iv_objid.

   IF sy-subrc <> 0.

     "add_log_msg 'E' ' ' 030 iv_otype iv_objid ''. "#EC NOTEXT

     RETURN.

   ENDIF.

  

* Here is your logic to check if there's already file attached

   IF sy-subrc <> 4.

     "add_log_msg 'E' ' ' 031 iv_objid '' ''. "#EC NOTEXT

     RETURN.

   ENDIF.

  

   DATA: ls_folder TYPE soodk.

   CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'

     EXPORTING

       region                = 'B'                           "#EC NOTEXT

     IMPORTING

       folder_id             = ls_folder

     EXCEPTIONS

       communication_failure = 1

       owner_not_exist       = 2

       system_failure        = 3

       x_error               = 4

       OTHERS                = 5.

   IF sy-subrc <> 0.

* Implement suitable error handling here

   ENDIF.

*  Convert hex string to hex table

   DATA: lv_binsize TYPE i,

         lt_hextab  TYPE TABLE OF solix,

         ls_docdat  TYPE sodocchgi1,

         ls_docinfo TYPE sofolenti1.

   CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

     EXPORTING

       buffer        = iv_hexcont

     IMPORTING

       output_length = lv_binsize

     TABLES

       binary_tab    = lt_hextab.

*  Prepare some attributes for the file

   ls_docdat-doc_size  = lv_binsize.

   ls_docdat-obj_name  = iv_filename.

   ls_docdat-obj_descr = iv_descript.

*  Create the file

   CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'

     EXPORTING

       folder_id                  = ls_folder

       document_data              = ls_docdat

       document_type              = iv_doctype

     IMPORTING

       document_info              = ls_docinfo

     TABLES

       contents_hex               = lt_hextab

     EXCEPTIONS

       folder_not_exist           = 1

       document_type_not_exist    = 2

       operation_no_authorization = 3

       parameter_error            = 4

       x_error                    = 5

       enqueue_error              = 6

       OTHERS                     = 7.

   IF sy-subrc <> 0.

* Implement suitable error handling here

     add_log_msg 'E' sy-msgid sy-msgno sy-msgv1 sy-msgv2 sy-msgv3. "#EC NOTEXT

     RETURN.

   ENDIF.

*  Create relation between file and the object

   DATA:   ls_object        TYPE borident,

           ls_attachmnt     TYPE borident.

   ls_object-objkey = lv_plvar && iv_objid.

   ls_object-objtype = c_prefix_hrobj && iv_otype.

   ls_attachmnt-objtype = 'MESSAGE'.                         "#EC NOTEXT

   ls_attachmnt-objkey  = ls_docinfo-doc_id(34).

   CALL FUNCTION 'BINARY_RELATION_CREATE'

     EXPORTING

       obj_rolea      = ls_object

       obj_roleb      = ls_attachmnt

       relationtype   = 'ATTA'                               "#EC NOTEXT

       fire_events    = abap_false

     EXCEPTIONS

       no_model       = 1

       internal_error = 2

       unknown        = 3

       OTHERS         = 4.

   IF sy-subrc <> 0.

* Implement suitable error handling here

     add_log_msg 'E' sy-msgid sy-msgno sy-msgv1 sy-msgv2 sy-msgv3. "#EC NOTEXT

     RETURN.

   ELSE.

     COMMIT WORK AND WAIT.

   ENDIF.

*Here you may implement your logic to check if file was attached

   IF sy-subrc = 0.

*    Success

     "add_log_msg 'S' ' ' 032 iv_objid '' ''. "#EC NOTEXT

     RETURN.

   ELSE.

*    Failed

     "add_log_msg 'E' ' ' 033 iv_objid '' ''. "#EC NOTEXT

     RETURN.

   ENDIF.

ENDMETHOD.



I know this will help me in future. Hope it will useful for you as well.


Best regards,

Alex Guryanov