0
votes

How can I retrieve Customer Master Data from a report and print it?

I have the Customer Number but I don't know what is the best approach to retreive other details such as:

  • Name
  • Street Address
  • Communication
  • (in the future also Contact Persons)

In my research I've found as options:

  • Doing a query on KNA1 and other Customer Master Data Tables

  • BAPI_CUSTOMER_GETLIST

  • BAPI_CUSTOMER_GETDETAIL2

I need a snippet of code that can print Customer master in a report.

2

2 Answers

2
votes

You could try function module BAPI_CUSTOMER_GETDETAIL2, it returns CUSTOMERADDRESS.

More importantly, I found this in less than a minute by

  • Starting transaction BAPI
  • Clicking on the 'Alphabetical' tab
  • Find Customer (good guess on my part, but not too hard a guess)
  • Stare at the methods and see that only GETDETAIL, GETDETAIL1 & GETDETAIL2 are likely
  • Double click on GETDETAIL2 (number at the end means the version)
  • Notice the name of the Function Module in the right hand Detail Pane.

My shoulder surfing fellow coder tells me that you could also simply have queried BAPI_CUSTOMER_* since the BAPI's you found clearly follow a pattern.

As to the snippet, you should at least try and let us know where you fail.

DATA : wa_address TYPE bapicustomer_04.

PARAMETERS p_kunnr TYPE kunnr.

CALL FUNCTION 'BAPI_CUSTOMER_GETDETAIL2'
  EXPORTING
    customerno      = p_kunnr
  IMPORTING
    customeraddress = wa_address.

WRITE: wa_address-name , wa_address-name_2 , wa_address-city , wa_address-country.
0
votes

I would use a query, try this:

report  ZKNA1.

tables: kna1.

type-pools slis.

data gt_fieldcat type slis_t_fieldcat_alv with header line.

data: gs_layout type slis_layout_alv,
g_repid type sy-repid.

types: begin of st_output,
    kunnr type kna1-kunnr,
    name1 type kna1-name1,
    city1 type adrc-city1,
    city2 type adrc-city2,
    street type adrc-street,
    tel_number type adrc-tel_number, " If you need more fields add them here and in the field catalog
end of st_output,
gt_tbl_data type standard table of st_output.

data gt_output type standard table of st_output.

data g_count type i.

data  t_heading type slis_t_listheader.

selection-screen: begin of block b2 with frame title text-t01.

select-options s_kunnr for kna1-kunnr.

select-options s_name1 for kna1-name1.

selection-screen: end of block b2.

initialization.
  g_repid = sy-repid.

start-of-selection.

  perform get_data.

  perform build_alv.

form init_layout.
  gs_layout-zebra = 'X'.
  gs_layout-detail_popup = 'X'.
endform.                    "init_layout

form get_data.
  field-symbols <wa_gt_output> type st_output.
  data zlen type i.

  select
    kna1~kunnr
    kna1~name1
    adrc~city1
    adrc~city2
    adrc~street
    adrc~tel_number
    from kna1
    left join adrc on adrc~addrnumber = kna1~adrnr
    into corresponding fields of table gt_output
    where
    kna1~kunnr in s_kunnr and
    kna1~name1 in s_name1
    order by
     kna1~kunnr
    .

  g_count = sy-dbcnt.

endform. " GET DATA

form build_alv.

* ALV required data objects.
  data: w_title   type lvc_title,
        w_comm    type slis_formname,
        w_status  type slis_formname,
        x_layout  type slis_layout_alv,
        t_event    type slis_t_event,
        t_fieldcat type slis_t_fieldcat_alv,
        t_sort     type slis_t_sortinfo_alv.

  refresh t_fieldcat.
  refresh t_event.
  refresh t_sort.
  clear x_layout.
  clear w_title.

* Layout
  perform init_layout.

* Field Catalog
  perform set_fieldcat using:
 1 'KUNNR' 'KUNNR' 'KNA1'           25 space  space  space space  space space space space space space space t_fieldcat,
 2 'NAME1' 'NAME1' 'KNA1'           25 space  space  space space  space space space space space space space t_fieldcat,
 3 'CITY1' 'CITY1' 'ADRC'           25 space  space  space space  space space space space space space space t_fieldcat,
 4 'CITY2' 'CITY2' 'ADRC'           25 space  space  space space  space space space space space space space t_fieldcat,
 5 'STREET' 'STREET' 'ADRC'         25 space  space  space space  space space space space space space space t_fieldcat,
 6 'TEL_NUMBER' 'TEL_NUMBER' 'ADRC' 25 space  space  space space  space space space space space space space t_fieldcat.

* Top of page heading
  perform set_top_page_heading using t_heading[] t_event.

* Events
  perform set_events using t_event.

* GUI Status
  w_status = ''.
  g_repid = sy-repid.

* User commands
  w_comm   = 'USER_COMMAND'.

* Order
* Example
*  PERFORM set_order USING '<field>' 'IT_DATA' 'X' space space t_sort.

  call function 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = g_repid
      is_layout                = gs_layout
      it_fieldcat              = t_fieldcat
      it_sort                  = t_sort
      i_callback_pf_status_set = w_status
      i_callback_user_command  = w_comm
      i_save                   = 'X'
      it_events                = t_event
      i_grid_title             = w_title
    TABLES
      t_outtab                 = gt_output
    EXCEPTIONS
      program_error            = 1
      others                   = 2.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

endform. " build_alv

form set_top_page_heading using t_heading type slis_t_listheader
                                t_events  type slis_t_event.

  data: x_heading type slis_listheader,
        x_event   type line of slis_t_event.

  clear x_heading.
  x_heading-typ = 'S'.
  x_heading-key = 'Count: '.
  x_heading-info = g_count.
  append x_heading to t_heading.

* Top of page event
  x_event-name = slis_ev_top_of_page.
  x_event-form = 'TOP_OF_PAGE'.
  append x_event to t_events.

endform.                    "set_top_page_heading

form set_events using t_events type slis_t_event.

  data: x_event   type line of slis_t_event.

**
* Example
* -------
*  clear x_event.
*  x_event-name = .
*  x_event-form = .
*  append x_event to t_event.
**

endform.                    "set_events

form set_order using p_fieldname p_tabname p_up p_down p_subtot
                     t_sort type slis_t_sortinfo_alv.

  data: x_sort type slis_sortinfo_alv.

  clear x_sort.
  x_sort-fieldname = p_fieldname.
  x_sort-tabname   = p_tabname.
  x_sort-up = p_up.
  x_sort-down = p_down.
  x_sort-subtot = p_subtot.
  append x_sort to t_sort.


endform.                    "set_order

form set_fieldcat using
      p_colpos          "Column position.
      p_fieldname       "Field of internal table
      p_ref_fieldname   "(Optional) Table field / data element
      p_ref_tabname     "(Optional) Table which holds the field referenced
      p_outputlen       "(Optional) Column width
      p_noout           "(Optional) If set to 'X', states that the field is not showed initially
      p_seltext_m       "(Optional) Medium label
      p_seltext_l       "(Optional) Long label
      p_seltext_s       "(Optional) Small label
      p_reptext_ddic    "(Optional) Extra small (heading) label
      p_ddictxt         "(Optional) Set to 'L', 'M', 'S' or 'R'
      p_hotspot         "(Optional) If set to 'X', this field will be used as a hotspot area for cursor
      p_showasicon      "(Optional) If set to 'X', this field will be shown as an icon
      p_checkbox        "(Optional) If set to 'X', this field will be shown as a checkbox
      p_edit            "(Optional) If set to 'X', this field will be editable.
      p_dosum           "(Optional) If set to 'X', this field will be summed (aggregation function)
      t_fieldcat type slis_t_fieldcat_alv. "Table which contains the whole fieldcat.

  data: wa_fieldcat type slis_fieldcat_alv.

  clear wa_fieldcat.

* General settings
  wa_fieldcat-fieldname = p_fieldname.
  wa_fieldcat-col_pos = p_colpos.
  wa_fieldcat-no_out = p_noout.
  wa_fieldcat-hotspot = p_hotspot.
  wa_fieldcat-checkbox = p_checkbox.
  wa_fieldcat-icon = p_showasicon.
  wa_fieldcat-do_sum = p_dosum.

  if p_ref_tabname is initial.
    wa_fieldcat-rollname =   p_ref_fieldname.
  else.
    wa_fieldcat-ref_tabname = p_ref_tabname.
    if p_ref_fieldname eq space.
      wa_fieldcat-ref_fieldname =   wa_fieldcat-fieldname.
    else.
      wa_fieldcat-ref_fieldname =   p_ref_fieldname.
    endif.
  endif.

* Set output length.
  if not p_outputlen is initial.
    wa_fieldcat-outputlen = p_outputlen.
  endif.

* Set text headers.
  if not p_seltext_m is initial.
    wa_fieldcat-seltext_m = p_seltext_m.
  endif.

  if not p_seltext_l is initial.
    wa_fieldcat-seltext_l = p_seltext_l.
  endif.

  if not p_seltext_s is initial.
    wa_fieldcat-seltext_s = p_seltext_s.
  endif.

  if not p_reptext_ddic is initial.
    wa_fieldcat-reptext_ddic = p_reptext_ddic.
  endif.

  if not p_ddictxt is initial.
    wa_fieldcat-ddictxt = p_ddictxt.
  endif.

* Set as editable or not.
  if not p_edit is initial.
    wa_fieldcat-input     = 'X'.
    wa_fieldcat-edit     = 'X'.
  endif.

  append wa_fieldcat to t_fieldcat.

endform.                   "set_fieldcat2

form top_of_page.
  call function 'REUSE_ALV_COMMENTARY_WRITE'
    exporting
*     i_logo             = <<If you want to set a logo, please,
*                          uncomment and edit this line>>
      it_list_commentary = t_heading.
endform.                    " alv_top_of_page

form user_command using r_ucomm     like sy-ucomm
                        rs_selfield type slis_selfield.

* Executes a command considering the sy-ucomm.
  case r_ucomm.
    when '&IC1'.

      if rs_selfield-fieldname eq 'KUNNR'.

        set parameter id 'KUN' field rs_selfield-value.
        call transaction 'XD03' and skip first screen. " Call transaction

      else.
        message 'Wrong column' type 'I'.
      endif.

  endcase.
endform.                    "user_command

Check that you could double click in client code and the program sends you transaction 'XD03'.