SAP for Utilities Discussions
Connect with fellow SAP users to share best practices, troubleshoot challenges, and collaborate on building a sustainable energy future. Join the discussion.
cancel
Showing results for 
Search instead for 
Did you mean: 

Customer created from BP - Email (Note 2222635) - How to implement User Exit EXIT_SAPLES03_001 ?

0 Kudos
1,213

Hi

We're facing the issue decribed in note 2222635: when we create a BP via transaction BP or FFP1 with several email addresses, the SD customer which is automatically created only takes the default email adress.

The note says the solution is to use User Exit EXIT_SAPLES01_001 and EXIT_SAPLES03_001.

I guess EXIT_SAPLES03_001 is the one to use because I don't really see how EXIT_SAPLES01_001 can be of any help.

Anyway, we tried to implement our logic in EXIT_SAPLES03_001 but it doesn't work.

Could you please help by explaining how you used EXIT_SAPLES03_001 (and maybe Exit EXIT_SAPLES01_001) to solve this issue?

Thanks in advance

1 ACCEPTED SOLUTION

Private_Member_7726
Active Contributor
0 Kudos
579

Hi hugnc 🙂

The more I look at this IS-U BP to SD Customer "integration" in OSS and in code, the more confused I become...

I'm afraid to give false or misleading info, but it seems to me that it should die officially with SAP S4/HANA (Note 2344100):

"Consequently, the former concept of SAP IS-U Business Partner and SAP SD Customer integration in SAP ERP has been substitued by SAP Customer Vendor Integration (CVI) in SAP S/4HANA."

You should probably also consider that Note in your decision making.

In our IS-U System (now on EHP7) there is a "mini SD" activated, and the "Reference Customer solution" for creating SD Customers has long since been disabled, in favor of "hacking in" some stuff to activate and utilize SAP's CVI referred to above (normally not available in IS-U). Which means two things: 1) we may have one additional challenge when moving to S4/HANA as per Note 2344100, and 2) I can't unfortunately test your code in the BP transaction context.

But I would not even attempt to do anything in those user exits - they are of absolutely no help IMO... Edit in: the complete target state of IS-U BP for later processing in EXIT_SAPLES03_001 can be saved in "customer memory" via EXIT_SAPLES01_001, so that part is ok. So strike this: [They don't even pass the complete internal state of IS-U BP currently being edited, and TX_ADSMTP is empty, at least on our system!] There is still the challenge - what to do with "extra E-mails" in create mode, when the SD Customer's adddress is not yet created...

Since what you are trying to do involves calling FM STARTING NEW TASK , I'd do this Integration "loosely", via CREATED and CHANGED events of BOR Object ISUPARTNER. It's not a perfect solution by any means, but at least you'd be reading committed states of BP/KNA1 there...

I came up with this, using your code as starting point:

FUNCTION z_jb_tst.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     REFERENCE(Y_PARTNER) TYPE  BU_PARTNER
*"  EXCEPTIONS
*"      ERROR_MESSAGE
*"----------------------------------------------------------------------
  TYPES:
    tyt_emails_bp TYPE STANDARD TABLE OF fkk0adsmtp WITH DEFAULT KEY.

  DATA : ls_kna1                 TYPE kna1,
         addr1_complete_kna1     TYPE szadr_addr1_complete,
         addr1_complete_kna1_out TYPE szadr_addr1_complete,
         lv_addrnumber           TYPE ad_addrnum,
         lv_persnumber           TYPE ad_persnum,
         lt_error                TYPE TABLE OF addr_error.

  FIELD-SYMBOLS: <fs_adsmtp_line> TYPE szadr_adsmtp_line.

*------------------------------------------------------------------------

  SELECT SINGLE * FROM  kna1 INTO ls_kna1
         WHERE  kunnr  = y_partner.
  IF sy-subrc = 0.
    CALL FUNCTION 'ADDR_GET_COMPLETE'
      EXPORTING
        addrnumber              = ls_kna1-adrnr
      IMPORTING
        addr1_complete          = addr1_complete_kna1
      EXCEPTIONS
        parameter_error         = 1
        address_not_exist       = 2
        internal_error          = 3
        wrong_access_to_archive = 4
        OTHERS                  = 5.
    IF sy-subrc = 0.
      DATA(ls_ekun) = VALUE isu01_ekun( ).
      DATA(lt_emails_bp) = VALUE tyt_emails_bp( ).
      CALL FUNCTION 'ISU_DB_PARTNER_SINGLE'
        EXPORTING
          x_partner                 = y_partner
          x_valdt                   = sy-datlo "'99991231'
          x_valdt_tech              = sy-datlo "'99991231'
          x_operation               = 'XXDFLT'
          x_xemail_mult             = abap_true
        IMPORTING
          y_but000                  = ls_ekun
        TABLES
          ty_email                  = lt_emails_bp
        EXCEPTIONS
          partner_not_found         = 1
          partner_in_role_not_found = 2
          internal_error            = 3
          OTHERS                    = 4.
      IF sy-subrc = 0.
        lv_addrnumber = ls_ekun-addrnumber.
        lv_persnumber = ls_ekun-persnumber.
      ELSE.
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
          RAISING error_message .
      ENDIF.
    ELSE.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ENDIF.
  ELSE.
    RETURN .
  ENDIF.

* Traitement
  "Check if diffrences
  DATA(lt_emails_kna1) = VALUE isu_adsmtp_tab(
    FOR <ls_adsmtp_line> IN addr1_complete_kna1-adsmtp_tab[]
    ( <ls_adsmtp_line>-adsmtp )
  ).
  SORT lt_emails_kna1 BY consnumber .

  DATA(lt_emails_bp1) = VALUE isu_adsmtp_tab(
    FOR <ls_email_line> IN lt_emails_bp
    ( CORRESPONDING #( <ls_email_line> ) )
  ).
  SORT lt_emails_bp1 BY consnumber .

  IF lt_emails_bp1[] EQ lt_emails_kna1[] .
    RETURN .
  ELSE.
*-----------
* transfert des emails
    SORT addr1_complete_kna1-adsmtp_tab[] BY adsmtp-consnumber.
    LOOP AT addr1_complete_kna1-adsmtp_tab ASSIGNING <fs_adsmtp_line>.
      <fs_adsmtp_line>-adsmtp-updateflag = 'D'.
      DATA(l_consnumber_max) = <fs_adsmtp_line>-adsmtp-consnumber.
    ENDLOOP.

    LOOP AT lt_emails_bp ASSIGNING FIELD-SYMBOL(<ls_email_bp>) .
      APPEND VALUE szadr_adsmtp_line(
        BASE VALUE #( adsmtp = CORRESPONDING #( <ls_email_bp> ) )
        date_from = <ls_email_bp>-valid_from
        adsmtp-consnumber = l_consnumber_max + sy-tabix
        adsmtp-updateflag = 'I'
        adsmtp-remark = 'Test only!!!' "Remove after Testing
      ) TO addr1_complete_kna1-adsmtp_tab .
    ENDLOOP .

    CALL FUNCTION 'ADDR_MAINTAIN_COMPLETE'
      EXPORTING
        updateflag         = 'U'
        addr1_complete     = addr1_complete_kna1
        address_group      = 'BP'
        check_address      = ' '
      IMPORTING
        addr1_complete_out = addr1_complete_kna1_out
      TABLES
        error_table        = lt_error
      EXCEPTIONS
        parameter_error    = 1
        address_not_exist  = 2
        handle_exist       = 3
        internal_error     = 4
        OTHERS             = 5.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ELSE.
      CALL FUNCTION 'ADDR_MEMORY_SAVE'
        EXPORTING
          execute_in_update_task = ' '
        EXCEPTIONS
          address_number_missing = 1
          person_number_missing  = 2
          internal_error         = 3
          database_error         = 4
          reference_missing      = 5
          OTHERS                 = 6.
      IF sy-subrc = 0.
        COMMIT WORK AND WAIT.
      ELSE.
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
          RAISING error_message .
      ENDIF.
    ENDIF.

    CALL FUNCTION 'ADDR_MEMORY_CLEAR'
      EXCEPTIONS
        unsaved_data_exist = 1
        internal_error     = 2
        OTHERS             = 3.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ENDIF.

  ENDIF.
ENDFUNCTION.<br>

The only thing I'm still not sure about - are the SD customer adresses really always of type 1..? Meaning, is it ok to use ADDR_MAINTAIN_COMPLETE, always?

This synchro Function I wrapped in BOR Event Handler function:

FUNCTION z_event_receiver.
*"--------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(EVENT) LIKE  SWETYPECOU-EVENT
*"     VALUE(RECTYPE) LIKE  SWETYPECOU-RECTYPE
*"     VALUE(OBJTYPE) LIKE  SWETYPECOU-OBJTYPE
*"     VALUE(OBJKEY) LIKE  SWEINSTCOU-OBJKEY
*"     VALUE(EXCEPTIONS_ALLOWED) LIKE  SWEFLAGS-EXC_OK DEFAULT SPACE
*"  EXPORTING
*"     VALUE(REC_ID) LIKE  SWELOG-RECID
*"  TABLES
*"      EVENT_CONTAINER STRUCTURE  SWCONT
*"  EXCEPTIONS
*"      TEMP_ERROR
*"      ANY_ERROR
*"--------------------------------------------------------------------

  CALL FUNCTION 'Z_JB_TST'
    EXPORTING
      y_partner     = CONV bu_partner( objkey )
    EXCEPTIONS
      error_message = 1
      OTHERS        = 2.
  CASE sy-subrc.
    WHEN 1 .
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING temp_error .
    WHEN 2.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING any_error .
  ENDCASE.
  
ENDFUNCTION.<br>

The Event handler linkage is set up in Transaction SWETYPV:

And it seems to work.. You'd also need to set up linkage for CREATED, if you choose this way.

There are pitfalls to this as well... Event delivery and handling via unqueued tRFCs can overwehlm system if someone starts doing "mass changes" on BPs. Queuing event delivery on the other hand means - E-mail changes would not appear in SD for minutes (or even hours during mass processing), depending on how the Event Queue processing Batch job is set up. tRFCs need to be monitored, at least in theory...

But it beats trying to read uncomitted BP/KNA1 states and waiting in a do loop, I believe 🙂

cheers,
Janis

View solution in original post

8 REPLIES 8

Private_Member_7726
Active Contributor
0 Kudos
579

What have you tried thus far, out of curiosity?

The "resolution" in this KBA almost feels like some attempt at "gas lighting" or something, after looking at the code... I see no way to do it within the "data flow" of IS-U integration of SD customer, and it starts already at FM SD_CUSTOMER_MAINTAIN_ALL, which is how the data ends up in SD - I suspect, without having tried to debug it. The only way to pass address, with a single E-mail address to that FM is... via structure I_BAPIADDR1/2 , depending on whether BP is a "company" or "consumer".

I really have no idea what do they even mean by:

"User exit EXIT_SAPLES01_001 and EXIT_SAPLES03_001 can be used to transfer further email adresses."

Somehow map the "extra" (or even all) E-mail addresses to write them directly to BAS(CAM), and do so in EXIT_SAPLES03_001? In the hope the SD_CUSTOMER_MAINTAIN_ALL will not overwrite.., and SAP Logic will do the requisite Commit/Rollback? What about during creation of BP..?

Cheers,
Janis

0 Kudos
579

Hi Janis

Thanks a lot for looking into my issue.

We both agree that what SAP proposes lacks a concrete solution. It kind of looks like an easy way to get rid of a serious issue.

Here is the link to the code we wrote in the exit to try to solve the issue: EXIT_SAPLES03_001 - Test

It doesnt work.

Private_Member_7726
Active Contributor
0 Kudos
580

Hi hugnc 🙂

The more I look at this IS-U BP to SD Customer "integration" in OSS and in code, the more confused I become...

I'm afraid to give false or misleading info, but it seems to me that it should die officially with SAP S4/HANA (Note 2344100):

"Consequently, the former concept of SAP IS-U Business Partner and SAP SD Customer integration in SAP ERP has been substitued by SAP Customer Vendor Integration (CVI) in SAP S/4HANA."

You should probably also consider that Note in your decision making.

In our IS-U System (now on EHP7) there is a "mini SD" activated, and the "Reference Customer solution" for creating SD Customers has long since been disabled, in favor of "hacking in" some stuff to activate and utilize SAP's CVI referred to above (normally not available in IS-U). Which means two things: 1) we may have one additional challenge when moving to S4/HANA as per Note 2344100, and 2) I can't unfortunately test your code in the BP transaction context.

But I would not even attempt to do anything in those user exits - they are of absolutely no help IMO... Edit in: the complete target state of IS-U BP for later processing in EXIT_SAPLES03_001 can be saved in "customer memory" via EXIT_SAPLES01_001, so that part is ok. So strike this: [They don't even pass the complete internal state of IS-U BP currently being edited, and TX_ADSMTP is empty, at least on our system!] There is still the challenge - what to do with "extra E-mails" in create mode, when the SD Customer's adddress is not yet created...

Since what you are trying to do involves calling FM STARTING NEW TASK , I'd do this Integration "loosely", via CREATED and CHANGED events of BOR Object ISUPARTNER. It's not a perfect solution by any means, but at least you'd be reading committed states of BP/KNA1 there...

I came up with this, using your code as starting point:

FUNCTION z_jb_tst.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     REFERENCE(Y_PARTNER) TYPE  BU_PARTNER
*"  EXCEPTIONS
*"      ERROR_MESSAGE
*"----------------------------------------------------------------------
  TYPES:
    tyt_emails_bp TYPE STANDARD TABLE OF fkk0adsmtp WITH DEFAULT KEY.

  DATA : ls_kna1                 TYPE kna1,
         addr1_complete_kna1     TYPE szadr_addr1_complete,
         addr1_complete_kna1_out TYPE szadr_addr1_complete,
         lv_addrnumber           TYPE ad_addrnum,
         lv_persnumber           TYPE ad_persnum,
         lt_error                TYPE TABLE OF addr_error.

  FIELD-SYMBOLS: <fs_adsmtp_line> TYPE szadr_adsmtp_line.

*------------------------------------------------------------------------

  SELECT SINGLE * FROM  kna1 INTO ls_kna1
         WHERE  kunnr  = y_partner.
  IF sy-subrc = 0.
    CALL FUNCTION 'ADDR_GET_COMPLETE'
      EXPORTING
        addrnumber              = ls_kna1-adrnr
      IMPORTING
        addr1_complete          = addr1_complete_kna1
      EXCEPTIONS
        parameter_error         = 1
        address_not_exist       = 2
        internal_error          = 3
        wrong_access_to_archive = 4
        OTHERS                  = 5.
    IF sy-subrc = 0.
      DATA(ls_ekun) = VALUE isu01_ekun( ).
      DATA(lt_emails_bp) = VALUE tyt_emails_bp( ).
      CALL FUNCTION 'ISU_DB_PARTNER_SINGLE'
        EXPORTING
          x_partner                 = y_partner
          x_valdt                   = sy-datlo "'99991231'
          x_valdt_tech              = sy-datlo "'99991231'
          x_operation               = 'XXDFLT'
          x_xemail_mult             = abap_true
        IMPORTING
          y_but000                  = ls_ekun
        TABLES
          ty_email                  = lt_emails_bp
        EXCEPTIONS
          partner_not_found         = 1
          partner_in_role_not_found = 2
          internal_error            = 3
          OTHERS                    = 4.
      IF sy-subrc = 0.
        lv_addrnumber = ls_ekun-addrnumber.
        lv_persnumber = ls_ekun-persnumber.
      ELSE.
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
          RAISING error_message .
      ENDIF.
    ELSE.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ENDIF.
  ELSE.
    RETURN .
  ENDIF.

* Traitement
  "Check if diffrences
  DATA(lt_emails_kna1) = VALUE isu_adsmtp_tab(
    FOR <ls_adsmtp_line> IN addr1_complete_kna1-adsmtp_tab[]
    ( <ls_adsmtp_line>-adsmtp )
  ).
  SORT lt_emails_kna1 BY consnumber .

  DATA(lt_emails_bp1) = VALUE isu_adsmtp_tab(
    FOR <ls_email_line> IN lt_emails_bp
    ( CORRESPONDING #( <ls_email_line> ) )
  ).
  SORT lt_emails_bp1 BY consnumber .

  IF lt_emails_bp1[] EQ lt_emails_kna1[] .
    RETURN .
  ELSE.
*-----------
* transfert des emails
    SORT addr1_complete_kna1-adsmtp_tab[] BY adsmtp-consnumber.
    LOOP AT addr1_complete_kna1-adsmtp_tab ASSIGNING <fs_adsmtp_line>.
      <fs_adsmtp_line>-adsmtp-updateflag = 'D'.
      DATA(l_consnumber_max) = <fs_adsmtp_line>-adsmtp-consnumber.
    ENDLOOP.

    LOOP AT lt_emails_bp ASSIGNING FIELD-SYMBOL(<ls_email_bp>) .
      APPEND VALUE szadr_adsmtp_line(
        BASE VALUE #( adsmtp = CORRESPONDING #( <ls_email_bp> ) )
        date_from = <ls_email_bp>-valid_from
        adsmtp-consnumber = l_consnumber_max + sy-tabix
        adsmtp-updateflag = 'I'
        adsmtp-remark = 'Test only!!!' "Remove after Testing
      ) TO addr1_complete_kna1-adsmtp_tab .
    ENDLOOP .

    CALL FUNCTION 'ADDR_MAINTAIN_COMPLETE'
      EXPORTING
        updateflag         = 'U'
        addr1_complete     = addr1_complete_kna1
        address_group      = 'BP'
        check_address      = ' '
      IMPORTING
        addr1_complete_out = addr1_complete_kna1_out
      TABLES
        error_table        = lt_error
      EXCEPTIONS
        parameter_error    = 1
        address_not_exist  = 2
        handle_exist       = 3
        internal_error     = 4
        OTHERS             = 5.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ELSE.
      CALL FUNCTION 'ADDR_MEMORY_SAVE'
        EXPORTING
          execute_in_update_task = ' '
        EXCEPTIONS
          address_number_missing = 1
          person_number_missing  = 2
          internal_error         = 3
          database_error         = 4
          reference_missing      = 5
          OTHERS                 = 6.
      IF sy-subrc = 0.
        COMMIT WORK AND WAIT.
      ELSE.
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
          RAISING error_message .
      ENDIF.
    ENDIF.

    CALL FUNCTION 'ADDR_MEMORY_CLEAR'
      EXCEPTIONS
        unsaved_data_exist = 1
        internal_error     = 2
        OTHERS             = 3.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ENDIF.

  ENDIF.
ENDFUNCTION.<br>

The only thing I'm still not sure about - are the SD customer adresses really always of type 1..? Meaning, is it ok to use ADDR_MAINTAIN_COMPLETE, always?

This synchro Function I wrapped in BOR Event Handler function:

FUNCTION z_event_receiver.
*"--------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(EVENT) LIKE  SWETYPECOU-EVENT
*"     VALUE(RECTYPE) LIKE  SWETYPECOU-RECTYPE
*"     VALUE(OBJTYPE) LIKE  SWETYPECOU-OBJTYPE
*"     VALUE(OBJKEY) LIKE  SWEINSTCOU-OBJKEY
*"     VALUE(EXCEPTIONS_ALLOWED) LIKE  SWEFLAGS-EXC_OK DEFAULT SPACE
*"  EXPORTING
*"     VALUE(REC_ID) LIKE  SWELOG-RECID
*"  TABLES
*"      EVENT_CONTAINER STRUCTURE  SWCONT
*"  EXCEPTIONS
*"      TEMP_ERROR
*"      ANY_ERROR
*"--------------------------------------------------------------------

  CALL FUNCTION 'Z_JB_TST'
    EXPORTING
      y_partner     = CONV bu_partner( objkey )
    EXCEPTIONS
      error_message = 1
      OTHERS        = 2.
  CASE sy-subrc.
    WHEN 1 .
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING temp_error .
    WHEN 2.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING any_error .
  ENDCASE.
  
ENDFUNCTION.<br>

The Event handler linkage is set up in Transaction SWETYPV:

And it seems to work.. You'd also need to set up linkage for CREATED, if you choose this way.

There are pitfalls to this as well... Event delivery and handling via unqueued tRFCs can overwehlm system if someone starts doing "mass changes" on BPs. Queuing event delivery on the other hand means - E-mail changes would not appear in SD for minutes (or even hours during mass processing), depending on how the Event Queue processing Batch job is set up. tRFCs need to be monitored, at least in theory...

But it beats trying to read uncomitted BP/KNA1 states and waiting in a do loop, I believe 🙂

cheers,
Janis

0 Kudos
579

Regarding this:

"They don't even pass the complete internal state of IS-U BP currently being edited, and TX_ADSMTP is empty, at least on our system!"

That is actually entirely normal - TX_ADSMTP is for "Address Independent" E-mail addresses, meaning - for this stuff:

cheers
Janis

0 Kudos
579

Just for the fun of it, I also tried to explore, whether it's feasible to just "grab" the state of IS-U BP being edited/saved using FM ISU_PARTNER_MEMORY_GET during EXIT_SAPLES03_001... 🙂 No, it isn't, - at least not in the create mode, because, how shall I put it... the Exit is called in the middle of the save and by then the call fails in BAS (CAM).

Maybe the state grab could be safely made in a custom BDT Event DTAKE function module, but that just makes everything more complex.

Just stay away from EXIT_SAPLES03_001, at least when trying to "transfer further email adresses" to SD Customer 🙂

0 Kudos
579

Ok, EXIT_SAPLES01_001, which is called during BDT Event DCHECK, could be used to take over complete checked IS-U BP state being saved (no need to try to grab nothing)... Yet it is still of no help in Create mode, because... where exactly to "transfer" the extra E-mails.., when the SD Customer and its Address do not exist yet? 🙂

0 Kudos
579

Thanks a lot for your help Jãnis.

I will test this as soon as I find a little bit of time.

579

Hi

Thanks a lot Jãnis. Your solution works great.

We had to adapt the code because we're not in ABAP 7.40 yet and because we had an issue with consnumber doing +1 each time you modified the Business Partener.

Here is the code we implemented :

FUNCTION ZF_SD_COPIE_MAIL_BP_CLIENT.
*"----------------------------------------------------------------------
*"*"Interface locale :
*"  IMPORTING
*"     VALUE(EVENT) LIKE  SWETYPECOU-EVENT
*"     VALUE(RECTYPE) LIKE  SWETYPECOU-RECTYPE
*"     VALUE(OBJTYPE) LIKE  SWETYPECOU-OBJTYPE
*"     VALUE(OBJKEY) LIKE  SWEINSTCOU-OBJKEY
*"     VALUE(EXCEPTIONS_ALLOWED) LIKE  SWEFLAGS-EXC_OK DEFAULT SPACE
*"  EXPORTING
*"     VALUE(REC_ID) LIKE  SWELOG-RECID
*"  TABLES
*"      EVENT_CONTAINER STRUCTURE  SWCONT
*"  EXCEPTIONS
*"      TEMP_ERROR
*"      ANY_ERROR
*"----------------------------------------------------------------------

  TYPES: tyt_emails_bp TYPE STANDARD TABLE OF fkk0adsmtp WITH DEFAULT KEY.

  DATA : ls_kna1                 TYPE kna1,
         addr1_complete_kna1     TYPE szadr_addr1_complete,
         addr1_complete_kna1_out TYPE szadr_addr1_complete,
         lv_addrnumber           TYPE ad_addrnum,
         lv_persnumber           TYPE ad_persnum,
         lt_error                TYPE TABLE OF addr_error,
         lt_emails_kna1          TYPE isu_adsmtp_tab,
         ls_ekun                 TYPE isu01_ekun,
         lt_emails_bp            TYPE tyt_emails_bp,
         lv_partner              TYPE BU_PARTNER.

  FIELD-SYMBOLS: <fs_adsmtp_line> LIKE LINE OF addr1_complete_kna1-adsmtp_tab,
                 <fs_emails_kna1> LIKE LINE OF lt_emails_kna1,
                 <fs_emails_bp>   LIKE LINE OF lt_emails_bp.
*------------------------------------------------------------------------

move objkey to lv_partner.

  SELECT SINGLE * FROM  kna1 INTO ls_kna1
         WHERE  kunnr  = lv_partner.

  IF sy-subrc = 0.
    CALL FUNCTION 'ADDR_GET_COMPLETE'
      EXPORTING
        addrnumber              = ls_kna1-adrnr
      IMPORTING
        addr1_complete          = addr1_complete_kna1
      EXCEPTIONS
        parameter_error         = 1
        address_not_exist       = 2
        internal_error          = 3
        wrong_access_to_archive = 4
        OTHERS                  = 5.
    IF sy-subrc = 0.

      CALL FUNCTION 'ISU_DB_PARTNER_SINGLE'
        EXPORTING
          x_partner                 = lv_partner
          x_valdt                   = sy-datlo
          x_valdt_tech              = sy-datlo
          x_operation               = 'XXDFLT'
          x_xemail_mult             = abap_true
        IMPORTING
          y_but000                  = ls_ekun
        TABLES
          ty_email                  = lt_emails_bp
        EXCEPTIONS
          partner_not_found         = 1
          partner_in_role_not_found = 2
          internal_error            = 3
          OTHERS                    = 4.
      IF sy-subrc = 0.
        lv_addrnumber = ls_ekun-addrnumber.
        lv_persnumber = ls_ekun-persnumber.
      ELSE.
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
          RAISING error_message .
      ENDIF.
    ELSE.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ENDIF.
  ELSE.
    RETURN .
  ENDIF.

* Suppression de tous les mails de la fiche client
  SORT addr1_complete_kna1-adsmtp_tab[] BY adsmtp-consnumber.
  LOOP AT addr1_complete_kna1-adsmtp_tab ASSIGNING <fs_adsmtp_line>.
    <fs_adsmtp_line>-adsmtp-updateflag = 'D'.
  ENDLOOP.

  LOOP AT lt_emails_bp ASSIGNING <fs_emails_bp>.

* Mise à jour avec les adresses mail du BP qui existaient déjà
    READ TABLE addr1_complete_kna1-adsmtp_tab ASSIGNING <fs_adsmtp_line> WITH KEY adsmtp-consnumber = <fs_emails_bp>-consnumber.
    IF sy-subrc = 0 AND <fs_adsmtp_line> IS ASSIGNED.
      MOVE-CORRESPONDING <fs_emails_bp> TO <fs_adsmtp_line>-adsmtp.
      <fs_adsmtp_line>-adsmtp-updateflag = 'U'.
    ELSE.
* Création des nouvelles adresses mail
      APPEND INITIAL LINE TO addr1_complete_kna1-adsmtp_tab ASSIGNING <fs_adsmtp_line>.
      MOVE-CORRESPONDING <fs_emails_bp> TO <fs_adsmtp_line>-adsmtp.
      <fs_adsmtp_line>-adsmtp-updateflag = 'I'.
    ENDIF.

    CLEAR: <fs_adsmtp_line>-adsmtp-valid_from.  " Il n'y a pas de Valid_from sur la fiche client SD

  ENDLOOP .

  CALL FUNCTION 'ADDR_MAINTAIN_COMPLETE'
    EXPORTING
      updateflag                     = 'U'
      addr1_complete                 = addr1_complete_kna1
      address_group                  = 'BP'
      check_address                  = ' '
      consider_consnumber_for_insert = abap_true
    IMPORTING
      addr1_complete_out             = addr1_complete_kna1_out
    TABLES
      error_table                    = lt_error
    EXCEPTIONS
      parameter_error                = 1
      address_not_exist              = 2
      handle_exist                   = 3
      internal_error                 = 4
      OTHERS                         = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
      RAISING error_message .
  ELSE.
    CALL FUNCTION 'ADDR_MEMORY_SAVE'
      EXPORTING
        execute_in_update_task = ' '
      EXCEPTIONS
        address_number_missing = 1
        person_number_missing  = 2
        internal_error         = 3
        database_error         = 4
        reference_missing      = 5
        OTHERS                 = 6.
    IF sy-subrc = 0.
      COMMIT WORK AND WAIT.
    ELSE.
      MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        RAISING error_message .
    ENDIF.
  ENDIF.

  CALL FUNCTION 'ADDR_MEMORY_CLEAR'
    EXCEPTIONS
      unsaved_data_exist = 1
      internal_error     = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
      RAISING error_message .
  ENDIF.

ENDFUNCTION.

And we implemented 2 events via transaction SWETYPV : Created and Changed

Thanks again Janis