Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting of segments in IDOC

ravi_rajput
Participant
0 Kudos

Hi Abap experts.

I need to sort/arrange  the data as shown in the screen shot given below.

row 5, 6,7 should come at top as row 1,2and 3 and 1,2,3,4 should come below it as row 4,5,6,7.

Any help with code sample or example ? The basic type is DELVRY03.

Thanks for your help.


1 ACCEPTED SOLUTION

juan_suros
Contributor
0 Kudos

Ravi,

You need to determine the beginning and end of each group of Handling Unit segments, then sort the groups in the new order, and write the parts of the iDoc to a new internal table.

Try this:

TYPES:

  BEGIN OF item_list_type,

    sort  TYPE c,

    start TYPE sytabix,

    end   TYPE sytabix,

  END OF item_list_type.

DATA: t_items TYPE STANDARD TABLE OF item_list_type WITH HEADER LINE.

DATA: li_new  TYPE STANDARD TABLE OF edidd.

DATA: x_idx   TYPE i.

DATA: x_pre   TYPE i. "precede Handling Unit data

DATA: x_post  TYPE i. "follow Handling Unit data

FIELD-SYMBOLS: <d> TYPE edidd.

  LOOP AT li_data_temp ASSIGNING <d>.

    x_idx = sy-tabix.

    CASE <d>-segnam.

      WHEN 'E1EDL37'.

        IF x_pre IS INITIAL.

          x_pre = x_idx - 1.

        ENDIF.

        IF t_items IS NOT INITIAL.

*         Mark end of previous Handling Unit group

          t_items-end = x_idx - 1.

          APPEND: t_items.

          CLEAR: t_items.

        ENDIF.

*       Mark beginning of this Handling Unit group

        t_items-start = x_idx.

      WHEN 'E1EDL54'.

      IF x_post IS INITIAL.

        x_post = x_idx.

      ENDIF.

*     Mark end of last Handling Unit group

      t_items-end = x_idx - 1.

      APPEND: t_items.

    ENDCASE.

  ENDLOOP.

  IF t_items-end IS INITIAL.

*   Finish last Handling Unit group, if document did not have a E1EDL54 segment

    DESCRIBE TABLE t_data LINES: t_items-end, x_post.

    APPEND: t_items.

  ENDIF.


APPEND LINES OF li_data_temp FROM 1 TO x_pre TO li_new.

SORT: t_items.

LOOP AT t_items.

  APPEND LINES OF li_data_temp FROM t_items-start TO t_items-end TO li_new.

ENDLOOP.

APPEND LINES OF li_data_temp FROM x_post TO li_new.

The internal table li_new[] is now ordered as you desired. This approach will allow you to reorder the Handling Unit groups, each beginning with a E1EDL37 segment, according to the sort fields you define.

1 REPLY 1

juan_suros
Contributor
0 Kudos

Ravi,

You need to determine the beginning and end of each group of Handling Unit segments, then sort the groups in the new order, and write the parts of the iDoc to a new internal table.

Try this:

TYPES:

  BEGIN OF item_list_type,

    sort  TYPE c,

    start TYPE sytabix,

    end   TYPE sytabix,

  END OF item_list_type.

DATA: t_items TYPE STANDARD TABLE OF item_list_type WITH HEADER LINE.

DATA: li_new  TYPE STANDARD TABLE OF edidd.

DATA: x_idx   TYPE i.

DATA: x_pre   TYPE i. "precede Handling Unit data

DATA: x_post  TYPE i. "follow Handling Unit data

FIELD-SYMBOLS: <d> TYPE edidd.

  LOOP AT li_data_temp ASSIGNING <d>.

    x_idx = sy-tabix.

    CASE <d>-segnam.

      WHEN 'E1EDL37'.

        IF x_pre IS INITIAL.

          x_pre = x_idx - 1.

        ENDIF.

        IF t_items IS NOT INITIAL.

*         Mark end of previous Handling Unit group

          t_items-end = x_idx - 1.

          APPEND: t_items.

          CLEAR: t_items.

        ENDIF.

*       Mark beginning of this Handling Unit group

        t_items-start = x_idx.

      WHEN 'E1EDL54'.

      IF x_post IS INITIAL.

        x_post = x_idx.

      ENDIF.

*     Mark end of last Handling Unit group

      t_items-end = x_idx - 1.

      APPEND: t_items.

    ENDCASE.

  ENDLOOP.

  IF t_items-end IS INITIAL.

*   Finish last Handling Unit group, if document did not have a E1EDL54 segment

    DESCRIBE TABLE t_data LINES: t_items-end, x_post.

    APPEND: t_items.

  ENDIF.


APPEND LINES OF li_data_temp FROM 1 TO x_pre TO li_new.

SORT: t_items.

LOOP AT t_items.

  APPEND LINES OF li_data_temp FROM t_items-start TO t_items-end TO li_new.

ENDLOOP.

APPEND LINES OF li_data_temp FROM x_post TO li_new.

The internal table li_new[] is now ordered as you desired. This approach will allow you to reorder the Handling Unit groups, each beginning with a E1EDL37 segment, according to the sort fields you define.