Creating a class to use the functionality of the persistent class CL_BCS:
I was searching for a way to use the newer class based methods for sending e-mail instead of the old function module "CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'".
I searched Google, SDN, SAP etc, and came up with a blog by Thomas Jung that came real close to doing what I was looking for. He actually created a function module to encapsulate the persistent class and then a special user interface to call it with which was real good. You can see his blog here.
After looking over his blog and getting a grasp on how the e-mail class worked, I thought I would put together something similar but keep it in its own class that my fellow developers could leverage in some of their requirements where e-mailing would be required. I developed a global class named ZCL_EMAIL_CLASS which can then send the e-mail with or without an attachment.
I started by going to SE24 (Class builder) and creating me a new class which I named ZCL_EMAIL_CLASS and then gave it the following attributes:
We start off the coding in our function module with a few class definitions.
I started by declaring my first attribute called ca_x - level constant - visibility private type char1 with a value of abap_true. It looked like this:
I then pressed the right arrow:
which took me to the definitions page where I did some old fashioned coding which turned out to be pretty cool.
I typed in the remaining constants, data types and tables here and it looked like this:
Press 'save' and the class builder will setup your attributes for you:
Now comes the fun part. Create three methods. One is the main method to call from your program and the other two are to setup the email message and optional attachment.
Set up importing and returning parameters in the main method.
These are the tables and variables that are to be passed to the class from your programs to be e-mailed.
Now let's put some code into the methods:
And for the other two as well:
Now in the attachment method I put the "if initial" for the table statement so I don't create a blank attachment if I only wanted to send my data in the body of the e-mail.
The hard part is done. Save and activate your class.
It's time to play with a program to use it. I created a sample program called ZTST_EMAIL_CLASS_USAGE_OO to use as an example of how this will all work together.
Execute the program and you will see the options that are available for e-mailing with. If the 'ready to send' check box is not checked the program will not send the e-mail and if the 'send data as an attachment' is not checked no attachment will be sent. If all three are checked you will get both data in the e-mail body and in the attachment. You are completely flexible in this as the attachment table, the attachment title, the email subject and the message body tables are all separate and you can load them as you wish in your program.
With the send data in e-mail body, you get an email that looks like this:
The table passed to the class (I_EMAIL_BODY) had ten lines in it as text_wa0 - text-wa9.
A good way to pass information to the class is to concatenate variable_1 through variable_~ into your work area (type line of table) respecting space (of needed) separated by c_tab (data element abap_char1). This will put your data in columns as a string to be passed to the table and laid out accordingly.
The e-mail that was returned with the attachment checkbox, and the "data in the email box checked" looks like this:
So you would get a message in the e-mail and an attachment.
(They are the same because I was coding a simple test using the same data.)
And with just the attachment only checked you would get the same as above but without anything in the message body.
If you have any positive suggestion, or even constructive criticism that will be helpful in making this better and maybe even simpler post a comment.
Thanks
Recent Comments