Thursday 20 November 2014

Squishing an unpredictable address structure...

We've all been there - address structures can be unpredictable, and you need to output them in a consistent format... The following ABAP code sample allows you to take the ADRC structure, put them into some kind of priority, split into a table, ignore the blank lines, and then concatenate into the final target field...

typesbegin of tty_addr,
        addrline type char255,
       end of tty_addr.

datat_addrlines type table of tty_addr,
      w_addrlines type tty_addr.

dataw_adrc type adrc.


select single from adrc into w_adrc where ADDRNUMBER = l_adrnr.
check sy-subrc 0.

concatenate w_adrc-name_text
            w_adrc-street
            w_adrc-city1
            w_adrc-STR_SUPPL1
            w_adrc-STR_SUPPL2
            w_adrc-STR_SUPPL3
            w_arc-POST_CODE1
            w_arc-POST_CODE2
            w_arc-POST_CODE3
"Whatever fields MIGHT be populated from ADRC...

into l_DispatchAddress separated by '<&,>'.

split l_DispatchAddress at '<&,>' into table t_addrlines.
clear l_DispatchAddress.

loop at t_addrlines into w_addrlines.
  if w_addrlines is not initial.
    concatenate l_DispatchAddress w_addrlines-addrline into l_DispatchAddress separated by ', '.
  endif.
endloop.