Skip to Content

Hi all,
According to my current project requirement we needed to retrieve the specific Data of an attachment(mainly for .txt type of file) of PO:Purchase Order(which can only be retrieved in an

internal table in binary format) using function module ‘BBP_PD_PO_GETDETAIL’ and send it in base64 format using ‘SCMS_BASE64_ENCODE_STR’ Conversion method.
All were working fine but the data we were getting in the attachment was not as per standards.
we were getting a lots of “null”s at the end of our Data.(PFA-Attachment1 )
Large number of “null” characters were getting added at the end of the attachment contents when we opened the newly generated attachment as a text file from

target side/destination side.
The internal table in which we were getting this attachment data in binary format has the row contents (The only field ‘LINE’)as type of RAW and of length

1022.
Thus this length of 1022 was the issue because after assigning the attachment data it considers the remaining characters as “null” if the assigned data is not

large enough.
It can be undestood in more detail using following example.

e.g. :

The attachment that is the textfile of a Purchase Order contains the string data as :
Test Proxy completed//(The only Data needs to be sent as a contents of a text file )

Instead the textfile which was generated at destination contained data :

Test Proxy completed.nullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
….(continued).”

(PFA-Attachment1 )

———————————————————————————————————
Actual Issue was:

      The actual issue was those nulls were getting added while retrieving this data in binary format itself(got it with the help of Debugging)
when we were calling that “BBP_PD_PO_GETDETAIL” method and retrieving the attachment data in Binary format in an internal table.
The size of normal string is much greater than the actual data provided hence it considers remaining spaces as “null”
(“0” in binary/hexadecimal format).

———————————————————————————————————-

So after few RND I came up with the solution for this issue which can be stated as follow:

Consider that we have retrieved binary Data into an internal table :
lt_contents.(Last field of lt_attachment table which is the outcome of ‘BBP_PD_PO_GETDETAI’ function module)
**********************************************************************************************************

DATA: lv_file TYPE string,
      lv_file_con1 TYPE xstring,
      lv_file_con2 TYPE xstring,
      lv_file_space type xstring,
      lv_file_con TYPE xstring.

LOOP AT lt_contents INTO lw_content.      // Get the RAW/Binary Data into a local work area for further usage.

lv_file_con1 = lw_content-line.
CONCATENATE  lv_file_con2 lv_file_con1 INTO lv_file_con2 IN BYTE MODE. //collecting the attachment contents in a variable from an internal table.

ENDLOOP.

lv_file_con = lv_file.
CLEAR lv_file.
lv_file_space = ‘0’.       // Hexadecimal symbol for space that is 0 which is to be eliminated.

shift lv_file_con2 right deleting trailing lv_file_space in byte mode.  // removing RHS spaces/’0’s/null’s.

**After RIGHT SHIFT  the spaces are get added at LHS of Data which can be eliminated using left shift.

shift lv_file_con2 left deleting leading lv_file_space in byte mode.  // all spacess/0’s/”null”s are get eliminated in this step

CALL FUNCTION ‘SCMS_BASE64_ENCODE_STR’      // converting Data into base64 format
  EXPORTING
  input    = lv_file_con2
**      unescape = ‘X’
  IMPORTING
  output   = lv_file.        // This lv_file will content the required data (without any “null” characters)hexadecimal form.

**————————————————————————————————————-

Thats it from my side.
If you expert guyz can suggest any changes I need to be made or can come up with a better solution
then you are most welcome.

Thanks and Regards,

Vikas Kailas Khanase
(+91) 9324758106

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. madhumati mishra

    Hi ,

    this solution doesnt work for me . I dont get correct string length after shifting and deleting. I am using xstrlength .

    below is the code –

    READ TABLE lt_content INTO ls_content_binary INDEX lv_count.

    *    lv_space = ‘0’.

    *    SHIFT ls_content_binary-line RIGHT DELETING TRAILING lv_space IN BYTE MODE.

    *    SHIFT ls_content_binary-line LEFT DELETING LEADING lv_space IN BYTE MODE.

         lv_len = xstrlen( ls_content_binaryline ).


    xstrlength still returns 1022. I see 0 in place for place in line even after shift.


    can you help.


    Thanks

    Madhumati

    (0) 
    1. N Cuky

      It didn’t work for me too, but I found a solution (actually, a workaround) that depends on your specific use of the RAW1022 table after the conversion.

      If the RAW table is eventually sent to a function/method like CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD, then you can prevent it from saving the trailing spaces/zeros by sending the parameter BIN_FILESIZE which should simply contain the length of the whole xstring object. If you don’t specify the file size then the method will save the complete RAW table including the spaces at the end.

      You can get the filesize before the conversion loop to the RAW table, by using XSTRLEN on the xstring itself.

      Instead of the conversion loop, you can use the function module ‘SCMS_XSTRING_TO_BINARY’ which returns a binary table (no specific length specified; you can use RAW1022, RAW255 or any length you want) and the aforementioned file size.

       

      (0) 
  2. Vikas Khanase Post author

    Hello Cuky,

    This logic is used in order to send data directly to other system such as PI, Oracle and there we have to download the file.

    IF you have to download or upload respective contents in SAP directory or local machine then your logic is excellent.
    🙂

     

    (0) 

Leave a Reply