You are on page 1of 12

REPORT zbp_prospect_load.

* Prospect upload program.


* Uploads data from a tab delimeted file.
TYPE-POOLS : slis.
TABLES : but000.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : p_hdr
AS CHECKBOX DEFAULT 'X'.
PARAMETERS : rb_ps
RADIOBUTTON GROUP rb USER-COMMAND sel DEFAULT 'X',
p_file TYPE string MODIF ID 002 LOWER CASE,
rb_as
RADIOBUTTON GROUP rb,
p_up_fol LIKE epsf-epsdirnam MODIF ID 001 LOWER CASE.
SELECTION-SCREEN END OF BLOCK b1.
CONSTANTS : c_delmt TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
TYPES: BEGIN OF ty_datatab,
line(1000),
END OF ty_datatab,
BEGIN OF ty_org_data,
otjid TYPE otjid,
short TYPE short_d,
END OF ty_org_data,
BEGIN OF ty_accdata,
crm_cust_no(10),
grouping(4),
role(7),
search_term1(20),
customer_name1(40),
customer_name2(40),
customer_name3(40),
customer_name4(40),
search_term2(20),
c_o_name(40),
street(60),
street2(40),
street3(40),
street4(40),
house_no(40),
location(40),
building(20),
floor(10),
room(10),
district(40),
city(40),
region(3),
post_code(10),
post_code_3(10),
country(2),
tax_jurisdiction(15),
transport_zone(10),
language(2),
telephone(30),
fax(30),
email(24),
tax_country(3),
tax_cat(3),

tax_class(1),
sales_org(4),
dist_channel(4),
division(2),
sales_district(6),
sales_office(7),
sales_group(6),
cust_price_proc(1),
cust_price_grp(2),
customer_stats_grp(1),
inco1(3),
inco2(28),
sales_currency(5),
END OF ty_accdata,
BEGIN OF ty_msgout,
slno(6) TYPE c,
bpartner(20) TYPE c,
message(225) TYPE c,
END OF ty_msgout.
DATA: gt_accdata TYPE TABLE OF ty_accdata,
wa_accdata TYPE ty_accdata,
gt_org_data TYPE TABLE OF ty_org_data,
wa_org_data TYPE ty_org_data,
gt_msgout TYPE TABLE OF ty_msgout,
wa_msgout TYPE ty_msgout.
DATA: ls_layout TYPE slis_layout_alv,
lt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: ls_cntrl_data
ls_org_data
ls_addr_data
ls_sarea
ls_sarea_data
ls_shipping_data
ls_pricing_data
gt_telf_data
wa_telf_data
gt_fax_data
wa_fax_data
gt_email_data
wa_email_data
gt_return
gt_role_ret
gt_sarea_ret
gt_shipping_ret
gt_pricing_ret
wa_return

LIKE
LIKE
LIKE
LIKE
LIKE
LIKE
LIKE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

bapibus1006_central,
bapibus1006_central_organ,
bapibus1006_address,
bapibus1006_sales_area,
bapibus1006140_org,
bapibus1006020_shipping,
bapibus1006030_pricing,
TABLE OF bapiadtel,
bapiadtel,
TABLE OF bapiadfax,
bapiadfax,
TABLE OF bapiadsmtp,
bapiadsmtp,
TABLE OF bapiret2,
TABLE OF bapiret2,
TABLE OF bapiret2,
TABLE OF bapiret2,
TABLE OF bapiret2,
bapiret2.

DATA: gv_bpartner TYPE bapibus1006_head-bpartner,


gv_role
TYPE bapibus1006_head-partnerrole.
DATA: ld_file LIKE rlgrap-filename,
wa_string(1000) TYPE c,
oref TYPE REF TO cx_root,
text TYPE string.
DATA: gt_but000 TYPE TABLE OF but000,

wa_but000 TYPE but000,


gt_but100 TYPE TABLE OF but100,
wa_but100 TYPE but100,
g_post(5) TYPE n,
g_tot_rec TYPE i,
g_err_rec TYPE i,
g_succ_rec TYPE i,
g_war_rec TYPE i.
DATA: lv_tabix TYPE sy-tabix.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
DATA: lv_file LIKE rlgrap-filename.
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
program_name = syst-repid
dynpro_number = syst-dynnr
field_name
= 'P_FILE'
static
= 'X'
mask
= '.text, *.txt'
CHANGING
file_name
= lv_file
EXCEPTIONS
mask_too_long = 1
OTHERS
= 2.
IF sy-subrc = 0.
p_file = lv_file.
ENDIF.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF rb_as = 'X'.
IF screen-group1 = '002'.
screen-input = 0.
MODIFY SCREEN.
CLEAR screen.
ENDIF.
ENDIF.
IF rb_ps = 'X'.
IF screen-group1 = '001'.
screen-input = 0.
MODIFY SCREEN.
CLEAR screen.
ENDIF.
ENDIF.
ENDLOOP.
LOOP AT SCREEN.
IF screen-group1 = '030'.
screen-intensified = '1'.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
AT SELECTION-SCREEN ON BLOCK b1.
IF sy-ucomm = 'ONLI'.
PERFORM validate_selection.
ENDIF.

START-OF-SELECTION.
IF rb_as = 'X'.
PERFORM read_from_as.
ELSEIF rb_ps = 'X'.
PERFORM read_from_ps.
ENDIF.
IF gt_accdata[] IS NOT INITIAL.
IF p_hdr = 'X'.
DELETE gt_accdata INDEX 1.
ENDIF.
SELECT
INTO
WHERE
AND
AND
AND
AND
AND

otjid
TABLE
plvar
otype
istat
begda
endda
langu

short FROM hrp1000


gt_org_data
= '01'
= 'O'
= '1'
<= sy-datum
>= sy-datum
= sy-langu.

CLEAR wa_msgout .
REFRESH gt_msgout.
lv_tabix = 0.
SORT gt_accdata BY crm_cust_no role.
LOOP AT gt_accdata INTO wa_accdata.
TRANSLATE wa_accdata-search_term1 TO UPPER CASE.
lv_tabix = sy-tabix.
CLEAR: gv_bpartner, ls_cntrl_data, ls_org_data, ls_addr_data, wa_return, w
a_msgout.
REFRESH: gt_telf_data, gt_fax_data, gt_email_data, gt_return, gt_role_ret,
gt_shipping_ret,gt_pricing_ret.
IF wa_accdata-search_term1 IS NOT INITIAL.
ls_cntrl_data-searchterm1 = wa_accdata-search_term1.
ENDIF.
IF wa_accdata-customer_name1 IS NOT INITIAL.
ls_org_data-name1
= wa_accdata-customer_name1.
ENDIF.
IF wa_accdata-customer_name2 IS NOT INITIAL.
ls_org_data-name2
= wa_accdata-customer_name2.
ENDIF.
IF wa_accdata-customer_name3 IS NOT INITIAL.
ls_org_data-name3
= wa_accdata-customer_name3.
ENDIF.
IF wa_accdata-customer_name4 IS NOT INITIAL.
ls_org_data-name4
= wa_accdata-customer_name4.
ENDIF.
IF wa_accdata-search_term2 IS NOT INITIAL.
ls_cntrl_data-searchterm2 = wa_accdata-search_term2.
ENDIF.
IF wa_accdata-c_o_name IS NOT INITIAL.
ls_addr_data-c_o_name = wa_accdata-c_o_name.
ENDIF.
IF wa_accdata-street IS NOT INITIAL.
ls_addr_data-street
= wa_accdata-street.
ENDIF.

IF wa_accdata-street2 IS NOT INITIAL.


ls_addr_data-str_suppl1 = wa_accdata-street2.
ENDIF.
IF wa_accdata-street3 IS NOT INITIAL.
ls_addr_data-str_suppl2 = wa_accdata-street3.
ENDIF.
IF wa_accdata-street4 IS NOT INITIAL.
ls_addr_data-str_suppl3 = wa_accdata-street4.
ENDIF.
IF wa_accdata-house_no IS NOT INITIAL.
ls_addr_data-house_no = wa_accdata-house_no.
ENDIF.
IF wa_accdata-location IS NOT INITIAL.
ls_addr_data-location = wa_accdata-location.
ENDIF.
IF wa_accdata-building IS NOT INITIAL.
ls_addr_data-building = wa_accdata-building.
ENDIF.
IF wa_accdata-floor IS NOT INITIAL.
ls_addr_data-floor
= wa_accdata-floor.
ENDIF.
IF wa_accdata-room IS NOT INITIAL.
ls_addr_data-room_no
= wa_accdata-room.
ENDIF.
IF wa_accdata-district IS NOT INITIAL.
ls_addr_data-district = wa_accdata-district.
ENDIF.
IF wa_accdata-city IS NOT INITIAL.
ls_addr_data-city
= wa_accdata-city.
ENDIF.
IF wa_accdata-region IS NOT INITIAL.
TRANSLATE wa_accdata-region TO UPPER CASE.
CONDENSE wa_accdata-region NO-GAPS.
ls_addr_data-region
= wa_accdata-region.
ENDIF.
IF NOT wa_accdata-country IS INITIAL.
TRANSLATE wa_accdata-country TO UPPER CASE.
ls_addr_data-country = wa_accdata-country.
ENDIF.
IF wa_accdata-country = 'US' OR wa_accdata-country = 'USA'.
CLEAR g_post.
IF NOT wa_accdata-post_code IS INITIAL.
g_post = wa_accdata-post_code.
UNPACK g_post TO g_post.
ls_addr_data-postl_cod1 = g_post.
ENDIF.
ELSE.
ls_addr_data-postl_cod1 = wa_accdata-post_code.
ENDIF.

IF wa_accdata-post_code_3 IS NOT INITIAL.


ls_addr_data-postl_cod3 = wa_accdata-post_code_3.
ENDIF.
IF wa_accdata-tax_jurisdiction IS NOT INITIAL.
ls_addr_data-taxjurcode = wa_accdata-tax_jurisdiction.
ENDIF.
IF wa_accdata-transport_zone IS NOT INITIAL.
ls_addr_data-transpzone = wa_accdata-transport_zone.
ENDIF.
IF wa_accdata-language IS NOT INITIAL.
ls_addr_data-langu
= wa_accdata-language.
ELSE.
ls_addr_data-langu = 'EN'.
ENDIF.
CLEAR: wa_telf_data.
IF wa_accdata-telephone IS NOT INITIAL.
wa_telf_data-telephone
= wa_accdata-telephone.
APPEND wa_telf_data TO gt_telf_data.
ENDIF.
CLEAR: wa_fax_data.
IF wa_accdata-fax IS NOT INITIAL.
wa_fax_data-fax
= wa_accdata-fax.
APPEND wa_fax_data TO gt_fax_data.
ENDIF.
CLEAR: wa_email_data.
IF wa_accdata-email IS NOT INITIAL.
wa_email_data-e_mail
= wa_accdata-email.
APPEND wa_email_data TO gt_email_data.
ENDIF.
break sparimi.
SELECT SINGLE * FROM but000 WHERE bu_sort1 = wa_accdata-search_term1.
IF sy-subrc EQ 0.
g_err_rec = g_err_rec + 1.
wa_msgout-slno = lv_tabix.
wa_msgout-bpartner = but000-bu_sort1.
CONCATENATE 'Customer' but000-bu_sort1 'already exists with A/c nr' but0
00-partner INTO wa_msgout-message SEPARATED BY space.
APPEND wa_msgout TO gt_msgout.
CONTINUE.
ENDIF.
CALL FUNCTION 'BAPI_BUPA_CREATE_FROM_DATA'
EXPORTING
businesspartnerextern = wa_accdata-crm_cust_no
partnercategory
= '2'
partnergroup
= wa_accdata-grouping
centraldata
= ls_cntrl_data
centraldataorganization = ls_org_data
addressdata
= ls_addr_data
duplicate_message_type = 'W'
accept_error
= 'X'
IMPORTING
businesspartner
= gv_bpartner
TABLES
telefondata
= gt_telf_data
faxdata
= gt_fax_data

e_maildata
= gt_email_data
return
= gt_return.
IF NOT gt_return IS INITIAL.
READ TABLE gt_return INTO wa_return WITH KEY type = 'E'.
IF sy-subrc = 0.
g_err_rec = g_err_rec + 1.
wa_msgout-slno = lv_tabix.
IF wa_accdata-search_term1 IS NOT INITIAL.
wa_msgout-bpartner = wa_accdata-search_term1.
ELSE.
wa_msgout-bpartner = wa_accdata-crm_cust_no.
ENDIF.
wa_msgout-message = wa_return-message.
APPEND wa_msgout TO gt_msgout.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
g_succ_rec = g_succ_rec + 1.
wa_msgout-slno = lv_tabix.
wa_msgout-bpartner = gv_bpartner.
wa_msgout-message = 'Account created successfully'.
APPEND wa_msgout TO gt_msgout.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
g_succ_rec = g_succ_rec + 1.
wa_msgout-slno = lv_tabix.
wa_msgout-bpartner = gv_bpartner.
wa_msgout-message = 'Account created successfully'.
APPEND wa_msgout TO gt_msgout.
ENDIF.
Adding Role
IF gv_bpartner IS NOT INITIAL AND wa_accdata-role IS NOT INITIAL.
gv_role = wa_accdata-role.
CALL FUNCTION 'BAPI_BUPA_ROLE_ADD'
EXPORTING
businesspartner
= gv_bpartner
businesspartnerrole = gv_role
TABLES
return
= gt_role_ret.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
Adding Sales Area Data
READ TABLE gt_org_data INTO wa_org_data WITH KEY short = wa_accdata-sales_

org.
IF sy-subrc = 0.
ls_sarea-sales_organization = wa_org_data-otjid.
ls_sarea-distribution_channel = '10'.
ls_sarea-division = '10'.
IF wa_accdata-sales_district IS NOT INITIAL.
ls_sarea_data-district
= wa_accdata-sales_district.
ENDIF.

IF wa_accdata-sales_office IS NOT INITIAL.


CONCATENATE 'SO_' wa_accdata-sales_office INTO wa_accdata-sales_office
.
READ TABLE gt_org_data INTO wa_org_data WITH KEY short = wa_accdata-sa
les_office.
IF sy-subrc = 0.
ls_sarea_data-sales_office = wa_org_data-otjid.
ENDIF.
ENDIF.
IF wa_accdata-sales_group IS NOT INITIAL.
CONCATENATE 'SG_' wa_accdata-sales_group INTO wa_accdata-sales_group.
READ TABLE gt_org_data INTO wa_org_data WITH KEY short = wa_accdata-sa
les_group.
IF sy-subrc = 0.
ls_sarea_data-sales_group = wa_org_data-otjid.
ENDIF.
ENDIF.
CALL FUNCTION 'BAPI_BUPA_FRG0140_ADD'
EXPORTING
businesspartner = gv_bpartner
sales_area
= ls_sarea
data
= ls_sarea_data
TABLES
return
= gt_sarea_ret.
*

Adding Pricing Data


IF wa_accdata-cust_price_proc IS NOT INITIAL.
ls_pricing_data-cust_pric_proc = wa_accdata-cust_price_proc.
ENDIF.
IF wa_accdata-cust_price_grp IS NOT INITIAL.
ls_pricing_data-price_group
= wa_accdata-cust_price_grp.
ENDIF.
IF wa_accdata-sales_currency IS NOT INITIAL.
ls_pricing_data-currency
= wa_accdata-sales_currency.
ENDIF.

ls_pricing_data-payment_terms = 'NT30'.
CALL FUNCTION 'BAPI_BUPA_FRG0030_ADD'
EXPORTING
businesspartner = gv_bpartner
sales_area
= ls_sarea
data
= ls_pricing_data
TABLES
return
= gt_pricing_ret.

Adding Shipping data


IF wa_accdata-inco1 IS NOT INITIAL.
ls_shipping_data-incoterms1
= wa_accdata-inco1.
ENDIF.
IF wa_accdata-inco2 IS NOT INITIAL.
ls_shipping_data-incoterms2
= wa_accdata-inco2.
ENDIF.
IF wa_accdata-inco1 IS INITIAL AND wa_accdata-inco2 IS INITIAL.
ls_shipping_data-incoterms1 = 'FOB'.
ls_shipping_data-incoterms2 = 'Free On Board'.

ENDIF.
CALL FUNCTION 'BAPI_BUPA_FRG0020_ADD'
EXPORTING
businesspartner = gv_bpartner
sales_area
= ls_sarea
data
= ls_shipping_data
TABLES
return
= gt_shipping_ret.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ENDIF.
CLEAR wa_accdata.
ENDLOOP.
ENDIF.
END-OF-SELECTION.
DESCRIBE TABLE gt_accdata[] LINES g_tot_rec.
IF NOT gt_msgout IS INITIAL.
PERFORM f_proc_display_msgout.
ENDIF.
*&---------------------------------------------------------------------*
*&
Form f_proc_display_msgout
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM f_proc_display_msgout .
CLEAR lt_fieldcat.
lt_fieldcat-col_pos
= '1'.
lt_fieldcat-fieldname = 'SLNO'.
lt_fieldcat-seltext_m = 'SlNo.'.
lt_fieldcat-outputlen = '06'.
lt_fieldcat-key
= 'X'.
APPEND lt_fieldcat.
CLEAR lt_fieldcat.
lt_fieldcat-col_pos
= '2'.
lt_fieldcat-fieldname = 'BPARTNER'.
lt_fieldcat-seltext_m = 'Business Partner'.
lt_fieldcat-outputlen = '20'.
lt_fieldcat-key
= 'X'.
APPEND lt_fieldcat.
CLEAR lt_fieldcat.
lt_fieldcat-col_pos
= '3'.
lt_fieldcat-fieldname = 'MESSAGE'.
lt_fieldcat-seltext_m = 'Message Text'.
lt_fieldcat-outputlen = '100'.
APPEND lt_fieldcat.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program
= sy-repid
i_callback_top_of_page = 'TOP-OF-PAGE'
is_layout
= ls_layout
it_fieldcat
= lt_fieldcat[]
TABLES
t_outtab
= gt_msgout
EXCEPTIONS
program_error
= 1
OTHERS
= 2.
ENDFORM.
" F_PROC_DISPLAY_MSGOUT

*&---------------------------------------------------------------------*
*&
Form top-of-page
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM top-of-page.
DATA: t_header TYPE slis_t_listheader,
wa_header TYPE slis_listheader.
wa_header-key =
wa_header-typ =
wa_header-info =
APPEND wa_header
CLEAR wa_header.
wa_header-key =
wa_header-typ =
wa_header-info =
APPEND wa_header
CLEAR wa_header.

'Input File Path:'.


'S'.
p_file..
TO t_header.

wa_header-key =
wa_header-typ =
wa_header-info =
APPEND wa_header
CLEAR wa_header.

'Error Records:'.
'S'.
g_err_rec.
TO t_header.

wa_header-key =
wa_header-typ =
wa_header-info =
APPEND wa_header
CLEAR wa_header.

'Warning Records:'.
'S'.
g_war_rec.
TO t_header.

wa_header-key =
wa_header-typ =
wa_header-info =
APPEND wa_header
CLEAR wa_header.

'Success Records:'.
'S'.
g_succ_rec.
TO t_header.

'Total Records:'.
'S'.
g_tot_rec.
TO t_header.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'


EXPORTING
it_list_commentary = t_header.
ENDFORM.
"top-of-page
*&---------------------------------------------------------------------*
*&
Form read_from_as
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM read_from_as .
OPEN DATASET ld_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc EQ 0.
DO.
CLEAR: wa_string, wa_accdata.
TRY .
READ DATASET ld_file INTO wa_string.
IF sy-subrc NE 0.
EXIT.
ENDIF.
CATCH cx_sy_conversion_codepage INTO oref.

text = oref->get_text( ).
WRITE :/ sy-index, text.
CHECK 1 = 2.
ENDTRY.
**

Split fields at Delimiter


SPLIT wa_string AT c_delmt INTO
wa_accdata-crm_cust_no
wa_accdata-grouping
wa_accdata-role
wa_accdata-search_term1
wa_accdata-customer_name1
wa_accdata-customer_name2
wa_accdata-customer_name3
wa_accdata-customer_name4
wa_accdata-search_term2
wa_accdata-c_o_name
wa_accdata-street
wa_accdata-street2
wa_accdata-street3
wa_accdata-street4
wa_accdata-house_no
wa_accdata-location
wa_accdata-building
wa_accdata-floor
wa_accdata-room
wa_accdata-district
wa_accdata-city
wa_accdata-region
wa_accdata-post_code
wa_accdata-post_code_3
wa_accdata-country
wa_accdata-tax_jurisdiction
wa_accdata-tax_jurisdiction
wa_accdata-transport_zone
wa_accdata-language
wa_accdata-telephone
wa_accdata-fax
wa_accdata-email
wa_accdata-tax_country
wa_accdata-tax_cat
wa_accdata-tax_class
wa_accdata-sales_org
wa_accdata-dist_channel
wa_accdata-division
wa_accdata-sales_district
wa_accdata-sales_office
wa_accdata-sales_group
wa_accdata-cust_price_proc
wa_accdata-cust_price_grp
wa_accdata-customer_stats_grp
wa_accdata-inco1
wa_accdata-inco2
wa_accdata-sales_currency.
IF NOT wa_accdata IS INITIAL.
APPEND wa_accdata TO gt_accdata.
CLEAR wa_accdata.
ENDIF.
ENDDO.

ENDIF.
CLOSE DATASET ld_file.
ENDFORM.

" READ_FROM_AS

*&---------------------------------------------------------------------*
*&
Form read_from_ps
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM read_from_ps .
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename
= p_file
filetype
= 'ASC'
has_field_separator
= 'X'
TABLES
data_tab
= gt_accdata
EXCEPTIONS
file_open_error
= 1
file_read_error
= 2
no_batch
= 3
gui_refuse_filetransfer = 4
invalid_type
= 5
no_authority
= 6
unknown_error
= 7
bad_data_format
= 8
header_not_allowed
= 9
separator_not_allowed = 10
header_too_long
= 11
unknown_dp_error
= 12
access_denied
= 13
dp_out_of_memory
= 14
disk_full
= 15
dp_timeout
= 16
OTHERS
= 17.
ENDFORM.
" READ_FROM_PS
*&---------------------------------------------------------------------*
*&
Form validate_selection
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM validate_selection.
IF rb_as = 'X' AND p_up_fol IS INITIAL.
MESSAGE e899(sd) WITH 'Please enter File name (with path)'.
ELSEIF rb_ps = 'X' AND p_file IS INITIAL.
MESSAGE e899(sd) WITH 'Please Select a file'.
ENDIF.
ENDFORM.
"VALIDATE_SELECTION