cancel
Showing results for 
Search instead for 
Did you mean: 

File attachment in Extended Notification

Former Member
0 Kudos

Hi all,

I have configured Extended Notifications for workflow (SWNCONFIG etc), which works just fine. I am using my own BSP for the email layout and have used custom classes to replace some of the standard handlers. So far so good.

There is a new requirement to include a PDF document as an attachment in the notification email. As far as I can see from analysing the various parameters and methods available to me, there is no way to do this. The only attachment-related functionality is related to the SAP attachment to execute a workitem etc. This is not what I need.

I can see 2 promising parameters in the settings for the subscription (SHOW_DOCUMENTS_AS & SHOW_OBJECTS_AS), but these don't seem to do much and are not mentioned in the help.

Does anybody know if this is possible and, if so, how to do it.

Thanks & Regards

Mike Speight

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Michael,

Even though you answered how to do it here, somebody else will ask the same question. When you see that question, you can just send them a link to this question. Few people actually do some research before asking a question - just ask Rick

Former Member
0 Kudos

Solved it myself.

former_member185167
Active Contributor
0 Kudos

How?

Former Member

Hi Rick,

I was hoping that someone would post this as a new issue at some point in the future,so that I could jumpin with the answer and improve my miserable tally of 2 points. Oh well..

It doesn't seem possible to add an attachment to the notification object itself. At least I couldn't find the way in.

Instead, I focused my attention on the final part of the process, namely the deliverer.

If you look right at the bottom in SWNCONFIG, you can either define your own delivery types or, if you are happy to make a change for all notifications of a given delivery type, simply modify the SAP delivery type to reference your own handler class.

I wanted to keep the standard type EMAIL_HTML, so made a copy.

The standard handler class at this level is CL_SWN_DELIVERER_BCS, which already has functionality to add the SAP standard attachments. I created my own subclass of this and redefined the IF_SWN_DELIVERER~SEND method (you could also simply redefine the ADD_ATTACHMENTS method) to call my new attachment-creating method. Simple.

Note: Be careful if you create your own new HTML delivery type as I did, you will also have to make changes wherever (for example) the constant swn1_deltype_email_html is used, since there are a couple of places in other methods, where the delivery type is explicitly checked, to work out whether SAP should send a HTML email or not. Otherwise you will only get plain text coming out. This will involve redefining some of the standard handler classes for other areas in SWNCONFIG.

Hope this helps,

Mike

former_member185167
Active Contributor
0 Kudos

Hi Mike,

Thanks! The satisfaction of helping unknown legions of future readers far outweighs any worthless points that may be gathered. If you want some easy points just answer some of the often very basic questions asked here.

I really can't imagine why anyone would want to send attachments with Extended Notifications but that doesn't stop "the business" from asking for it!

regards

Rick Bakker

hanabi technology

Former Member
0 Kudos

Hi Rikk,

FYI - in my case, "the business" wanted a PDF image of an invoice (from the archive) attached to the email.

Its part of an offline invoice approval solution. They will have the choice to approve via email (no log in), or execute the workitem via a link (log in).

Since they have extended notifications already in place, I wanted to achieve this without writing something from scratch.

Regards,

Mike

former_member185167
Active Contributor
0 Kudos

Hello,

"They will have the choice to approve via email (no log in)"

I guess security isn't a priority there. The auditors cometh.

reards

Rick Bakker

hanabi technology

KKilhavn
Active Contributor
0 Kudos

I agree in principle with how one can “harvest” points if one has the time, but for some reason you don’t always get credited as helpful, or even as the right answer, when you tell people to press F1 and read the online help - or even search for previous answers to the same question. Odd, isn’t it?

Message was edited by: Kjetil Kilhavn:

Currently being moderated? That is both a sad and a good thing. Sad that one has to resort to moderation, and good that the questions to which answers are found in the online help or Frequently Answered Questions are now (hopefully in most cases at least) rejected by moderators.

Former Member
0 Kudos

I just had the exact same business requirement for a pdf image attached to the extended notifications and this helped greatly.  Have you reached a legion yet?

paul_pendleton
Participant
0 Kudos

Michael,

For your situation did you add attachments that were already added to the workitem itself?  Or are you creating an attachment on the fly?  I have a requirement to add a spreadsheet to the extended notification email if it exist on the workitem.  I have the attachment in my workitem.  If you are adding attachments from the workitem could you share the code for this?

This is our first use of extended notifications.  I guess I'm trying to understand how you get back to the actual workitem in order to retrieve the attachments there?

I guess I don't really understand why they would not be included in the standard code anyway.  Since it already does attachments.  At least it should include an option to do this because we do have the ability to have it on the SAP inbox work item.  Stands to reason you might want it on the email as well.  An option would have been nice so you could prevent it if you did not want it.

Anyway if you or Eric have any examples of doing this I would appreciate it. I have done workflow for a while now but actually just did my first attachment to workflow 2 days ago so not fully understanding all of it.  Not that familiar with classes either but I can muddle my way through it.

michael_speight2
Explorer
0 Kudos

Hi Paul,

you may have already solved this one yourself, but…

In my case, I was mostly reading existing attachments from other sources - an archived PDF and file attachments from a custom application for example.

The principle is, however, the same; you just need to know how to read an attachment for the workitem into a usable format for transferring to the attachment in the email – binary table for example.

Here you can start with the “workflow API” functions SAP_WAPI_*

SAP_WAPI_GET_ATTACHMENTS will read the list of attachments for a given workitem ID. It does not return data, just the header information i.e. an object type & key, which is usually a SAP Office document (object SOFM).

Using the SAP Office document ID, read the actual file data using a function such as SO_DOCUMENT_READ_API1. Search this forum for more detailed information regarding this.

The workitem ID for a notification can be determined from the message object, which is provided to the method IF_SWN_DELIVERER~SEND in parameter i_message.

Since a single message can actually relate to multiple notifications (according to the configuration of the message in SWNCONFIG), you should call method i_message->get_notifications( ) to retrieve the list.

With the usual setup of “1 notification per message”, the table will only have 1 record.

Each entry in this list is an object reference of type IF_SWN_NOTIF. This object has an attribute APP_KEY, which is actually the workitem ID related to that notification.

Once you have the data and the workitem ID, the rest is easy.

Method IF_SWN_DELIVERER~SEND has a parameter c_document. Just call the add_attachment method. Look at the standard ADD_ATTACHMENT method for assistance.

You may need to convert the data read from the workitem attachment, into the correct format for this method. Here you can look at the SCMS* functions such as SCMS_BINARY_TO_XSTRING, SCMS_XSTRING_TO_BINARY etc for help.

Regards,

Mike

P.S. Yes it would be very nice if workitem attachments could be attached to the related notification mail using a SAP standard method. At the moment, the standard ADD_ATTACHMENTS method relates only to the attachments used for executing the workitem, navigating to the inbox etc.

paul_pendleton
Participant
0 Kudos

Michael,

Thanks Michael this will help.  I have been able to get my spreadsheet attachment added to the email.  However it has no data.    But I'm confident I can get that to happen.  I just hard coded the workitem for testing purposes for the time being.

I finally put it in debug and found the attribute containing the workitem.  Without seeing the values it was hard for me to know which field or structure contained the workitem number.  I will use your input from above to try this out soon.  Once I have it working I will post how I did it.

I've done as you did and created a seperate class and method for these attachments.  The only thing I will be concerned with is your comments about other places that need to check for the delivery type.  I searched and only found reference to it in the CL_SWN_DELIVERER_BCS class.  I'm sure it is in other places just have not found them all yet.  So will still have to do that.

So far it really has been pretty easy.  But as the saying goes "anything is easy once you know how to do it."

michael_speight2
Explorer
0 Kudos

Hi Paul,

one place it is definitely used is in the DO_REQUEST method of the notification controller class (standard is CL_SWN_MESSAGE_CONTROLLER I think) . Search for attribute m_delivery_type.

The data element for this is SWN_DELTYPEID, so you can use this to find other instances.

Regards,

Mike

paul_pendleton
Participant

Michael,

wanted to provide what I have done so far.  I have it working now. The only thing I have not done is create my own delivery type and check for all areas where that is used.  I'm confident I will get that but just have not made it that far yet.  Here is what I have done.

Create a new class based on CL_SWN_DELIVERER_BCS

In method IF_SWN_DELIVERER~SEND I added code to call my method to add attachments right after the the call to add_attachments method

*-     add attachments
      CALL METHOD me->add_attachments
        EXPORTING
          i_message  = i_message
        CHANGING
          c_document = lo_document
        EXCEPTIONS
          OTHERS     = 1.

*-     add attachments
      CALL METHOD me->add_user_attachments
        EXPORTING
          i_message  = i_message
        CHANGING
          c_document = lo_document
        EXCEPTIONS
          OTHERS     = 1.

Then in my method add_user_attachments I have the following:

DATA lt_workitems     TYPE SWNTNOTIF3.
DATA lt_head          TYPE TABLE OF SOLISTI1.
DATA wa_head          TYPE SOLISTI1.
DATA lt_lines         TYPE TABLE OF SOLISTI1.
DATA l_att_type       TYPE soodk-objtp.
DATA lt_attachments   TYPE TABLE OF swr_object.
DATA wa_attachments   TYPE swr_object.
DATA wa_folder_id     TYPE soodk.
DATA wa_object_id     TYPE soodk.
DATA wa_document_id   TYPE SOFOLENTI1-DOC_ID.
DATA wa_document_data TYPE SOFOLENTI1.
DATA lv_workitem_id   TYPE SWR_STRUCT-WORKITEMID.
DATA lv_app_key       TYPE SWN_APPKEY.

  FIELD-SYMBOLS: <workitem> TYPE SWN_NOTIFREF.

  entered( 'add_user_attachments' ).

*- loop thru the notifications
  lt_workitems = i_message->get_notifications( ).

  LOOP AT lt_workitems ASSIGNING <workitem>.
*-   retrieve workitem id
lv_workitem_id = <workitem>->app_key.

  CALL FUNCTION 'SAP_WAPI_GET_ATTACHMENTS'
    EXPORTING
      workitem_id = lv_workitem_id
    TABLES
      attachments = lt_attachments.

This really needs to be changed so that it gets all attachments but for now I only have one workflow that uses this so will not worry about it to much and just read the first entry.
  READ TABLE lt_attachments INTO  wa_attachments INDEX 1.
  wa_folder_id = wa_attachments-object_id+20(17).
  wa_object_id = wa_attachments-object_id+37(17).

  CONCATENATE wa_folder_id wa_object_id INTO wa_document_id.

  CALL FUNCTION 'SO_DOCUMENT_READ_API1'
    EXPORTING
      DOCUMENT_ID     = wa_document_id
    IMPORTING
      DOCUMENT_DATA   = wa_document_data
    TABLES
      OBJECT_HEADER   = lt_head
      OBJECT_CONTENT  = lt_lines.

  READ table lt_head into wa_head WITH KEY line+0(11) = '&SO_FORMAT='.
  l_att_type = wa_head-line+11(3).

  TRY.
      CALL METHOD c_document->add_attachment(
        i_attachment_type = l_att_type
        i_attachment_subject = wa_attachments-def_attrib
        i_attachment_size = wa_document_data-doc_size
        i_att_content_text = lt_lines
        i_attachment_header = lt_head ).
    CATCH cx_document_bcs.
*-    do nothing
  ENDTRY.
  ENDLOOP" AT lt_attach3 ASSIGNING <attach3>.

  leaving( 'add_user_attachments' ).

It is working fine.  I appreciate all your input.  Believe it or not the hardest part of it was figuring out how to get the attribute for APP_KEY.  I could not figure out the syntax of how to retrieve that from the object in my field symbol.  But it is working and just wanted to supply this in case it might be useful to any one else.  I really appreciate your input it has me up and running as I need to be.