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: 

Use CL_SAPUSER_BCS=>CREATE to send email to multiple users

walkerist
Participant
0 Kudos

I'm trying to maintain the work of previous abaper. Currently the setup of send report via email is this:

cl_sapuser_bcs=>create( s_inbox-low )

The problem is it is only sending the email to only one user which is the first one(JUNIORDEV). I would like to send the email to all the users from junior dev all the way to the client.

10 REPLIES 10

RAF
Active Contributor
0 Kudos

Hi,

select bname form usr21 into table @data(lt_user) where bname in s_inbox.

loop at lt_user assigning field-symbol(<ls_user>).

cl_sapuser_bcs=>create( <ls_user> )

endloop´.

matt
Active Contributor

Or use something like this to send one email to multiple people.


data(send_request) = cl_bcs=>create_persistent( ).
data(document) = cl_document_bcs=>create_document( i_type = mail_type i_text = body i_subject = subj ).
send_request->set_document( document ).
send_request->set_subject( subject ).
LOOP AT recipients...
send_request->add_recipient( ... ).
ENDLOOP.

0 Kudos

matthew.billingham Sorry, this is my first to use this class. But what does this mean?

send_request->add_recipient( ... ).

Will the users be defined inside the (...)?

EDIT:

Also, the code existing code is like this. I just need to enhance it:

 lo_sender = cl_sapuser_bcs=>create( s_inbox-low ).
lo_send_request->add_recipient( lo_sender ).

matt
Active Contributor
0 Kudos

walkerist

recipients contains the recipients. The code will be like this:

LOOP AT recipients INTO FIELD-SYMBOL( <recipient> ).
send_request->add_recipient( <recipient> ).
ENDLOOP.

I know what your existing code does. If you enhance it to use a loop, then you'll be sending the same email multiple times. This is inefficient. I'm suggesting that you send the email once to multiple recipients. You can use BCC if you don't want the email addresses to be visible.
I leave the definition and population of recipients to the reader. There are plenty of blogs detailing how to use the CL_BCS classes. It really is very easy to learn to use.

0 Kudos

matthew.billingham If I get my recipients via s_inbox. Can I send an email to multiple recipients using s_inbox?

matt
Active Contributor
0 Kudos

I don't know what s_inbox is.

0 Kudos

matthew.billingham is the S_INBOX is the field in the selection screen. It is structured into <lfs_inbox> and the recipient is selected via <lfs_inbox>-low therefore, other users were not able to receive the email

matt
Active Contributor
0 Kudos

Look at s_inbox in debug. You'll see it is an internal table of structure

SIGN

OPTION

LOW

HIGH

Write some validation so that if SIGN is not EQ, or OPTION is not 'I' or HIGH is not empty, then it's an error. (I.e. allow only I EQ <value> in the internal table).

Low contains the email address entered by the user.

LOOP AT s_inbox[] ASSIGNING FIELD-SYMBOL(<select_entry>).
data(recipient) = cl_sapuser_bcs=>create( <select_entry>-low ). send_request->add_recipient( recipient ).
ENDLOOP.

matthew.billingham

~~Thanks. But isn't the <select_entry>-low only selects one user? I want all the users in <select_entry> receives the email. Also, I have seen this in the program but will debug it first. Thank you again.

Does this FM affects the code? Because it seems like this code only selects the first email in s_email and not the rest?~~

 LOOP AT s_email.<br>    CALL FUNCTION 'BAPI_USER_GET_DETAIL'<br>      EXPORTING<br>        username  = s_email-low<br>      IMPORTING<br>        address   = XXXXX<br>      TABLES<br>        return    = XXXXX.

EDIT: Ok I get it now. The s_inbox-low is where the users are stored. However, all users in s_inbox are still not getting the email. Only the first user in the list.

      LOOP AT s_inbox[] ASSIGNING FIELD-SYMBOL(<s_inbox>).<br>       lo_sender = cl_sapuser_bcs=>create( <s_inbox>-low ).<br>      ENDLOOP.

EDIT #2: It it working now. Forgot to include this in the loop:

Thank you!

 send_request->add_recipient( recipient ).

Sandra_Rossi
Active Contributor
0 Kudos

walkerist matthew.billingham Better post these comments under separate answer, because the current answer where you are commenting doesn't talk about ADD_RECIPIENT.