SAP Customer Relationship Management

Email in C4C lost embedded objects while transfer from CRM

Problem Description:

In CRM Pictures or Tables can be embedded in E-mails. When move these Emails to C4C,

Emails transferred as a plain text and embedded objects like Pictures/Tables get lost. SAP transfer Emails as plain text instead of HTML text.

Sample Mail in CRM:

In this Email, 2 images and one table embedded

Mail transferred to C4C:

As you can see Email body transferred as only plain text, Images and table formatting lost from Email Body.

Also Read: SAP CRM Certification Preparation Guide

Solution:

As a solution I have converted Email Body in HTML format and Attach HTML file as an attachment to that Email. Now when we look in C4C there will be an HTML file (Name as “Original Email.html”) attached to email and so users can view actual email as an attachment.

Here is the steps and code:

First understands how attachments files get transferred to C4C.

Function module “CRMPCD_ATTACHMENTS_TO_SOD” is used to transfer the attachment from CRM to C4C. In this function module, marked class and method is used to send attachments to C4C.

Inside this method, below marked class/method is called to execute Web Service “CO_CRMPCD_ATTACHMENT_FOLDER_RE” which actually transfer the attachments.

Double click on it above execute method, here you can see DO_EXECUTE method, Double click on it.

Here after method “export_convesion” I called a custom function module as below:

CALL FUNCTION ‘XXXXXXXXX’
TABLES
tt_email_bo                = it_email_bo
changing
cs_attachment_folder       = ls_output1.

Now here is the code written in above custom function module which will convert Email body into HTML page and attach it to email.

DATA: lt_crmpcd1          TYPE crmpcd_attachment_folder_r_tab,
ls_crmpcd1          TYPE crmpcd_attachment_folder_repli,
lt_document         TYPE crmpcd_document_tab,
ls_document         TYPE crmpcd_document,

ls_bo               TYPE sibflporb,
lt_send_request     TYPE bcsy_guid,
lo_appl_email_data  TYPE REF TO cl_crm_email_data,
ls_body             TYPE crms_email_mime_struc,
lv_send_request     TYPE os_guid,

lv_str              TYPE string,
ls_attach           TYPE crms_email_mime_struc,
lv_bd64_string      TYPE string,
lv_pathname         TYPE string VALUE ‘/usr/sap/…./Original_Mail.html’,”Application server path from your system
lv_data             TYPE xstring,

lt_header_guid      TYPE crmt_object_guid_tab,
lv_header_guid      TYPE crmt_object_guid,
lt_orderadm_h       TYPE crmt_orderadm_h_wrkt,
ls_orderadm_h       TYPE crmt_orderadm_h_wrk,
lv_activity_class   TYPE crmt_activity_class.

***Loop on all email objects
LOOP AT tt_email_bo ASSIGNING FIELD-SYMBOL(<ls_email_bo>).

CLEAR: ls_bo, lt_send_request, lv_send_request, lo_appl_email_data, ls_body.
ls_bo-instid    = <ls_email_bo>-instid.
ls_bo-typeid    = <ls_email_bo>-typeid.
ls_bo-catid     = <ls_email_bo>-catid.

***This code is only for Email business activity BUS2000126
***Also Try to check for Email
IF <ls_email_bo>-typeid NE ‘BUS2000126’.
CONTINUE.
ELSE.
” Check if the current business activity is from the type email
CLEAR: lt_header_guid.
lv_header_guid  = ls_bo-instid.
INSERT lv_header_guid INTO TABLE lt_header_guid.

” – Determine the process type
CALL FUNCTION ‘CRM_ORDER_READ’
EXPORTING
it_header_guid       = lt_header_guid
IMPORTING
et_orderadm_h        = lt_orderadm_h
EXCEPTIONS
document_not_found   = 1
error_occurred       = 2
document_locked      = 3
no_change_authority  = 4
no_display_authority = 5
no_change_allowed    = 6
OTHERS               = 7.
IF SY-SUBRC NE 0.
CONTINUE.
ENDIF.
CLEAR: lt_header_guid, ls_orderadm_h.
READ TABLE lt_orderadm_h INTO ls_orderadm_h INDEX 1.”#EC CI_SUBRC

” – Determine the activity Class. ‘G’: EMail, ‘F’: Appointment, …
CALL FUNCTION ‘CRM_ORDER_ACTIVITY_H_SELECT’
EXPORTING
iv_process_type   = ls_orderadm_h-process_type
IMPORTING
es_activity_class = lv_activity_class.
*        EXCEPTIONS
*          entry_not_found   = 1
*          OTHERS            = 2.
IF lv_activity_class NE ‘G’.
CONTINUE.
ENDIF.

ENDIF.

***Get GUID of the email
lt_send_request = cl_crm_email_utility=>get_mails_of_business_object( ls_bo ).

READ TABLE lt_send_request INTO lv_send_request INDEX 1.
IF sy-subrc EQ 0.

***Get Email Data
CALL METHOD cl_crm_email_utility=>get_mail_data_from_so
EXPORTING
iv_send_request_id = lv_send_request
RECEIVING
er_mail_data       = lo_appl_email_data
EXCEPTIONS
not_found          = 1.

***Read Email Body
READ TABLE lo_appl_email_data->body INTO ls_body WITH KEY is_attachment = ‘ ‘ mime_type   = ‘text/html’.
IF sy-subrc EQ 0.

***Make a loop on all embedded attachments and convert that embedded object to BASE64
LOOP AT lo_appl_email_data->body INTO ls_attach WHERE is_attachment = abap_true AND content_id IS NOT INITIAL. “#EC CI_NESTED
CLEAR: lv_str, lv_bd64_string.
CONCATENATE ‘cid’ ls_attach-content_id INTO lv_str SEPARATED BY ‘:’.

***Convert attached embedded object BINARY code to BASE64 code
CALL FUNCTION ‘SSFC_BASE64_ENCODE’
EXPORTING
bindata                  = ls_attach-content_bin
IMPORTING
b64data                  = lv_bd64_string
EXCEPTIONS
ssf_krn_error            = 1
ssf_krn_noop             = 2
ssf_krn_nomemory         = 3
ssf_krn_opinv            = 4
ssf_krn_input_data_error = 5
ssf_krn_invalid_par      = 6
ssf_krn_invalid_parlen   = 7
OTHERS                   = 8.
IF sy-subrc <> 0.
* Implement suitable error handling here
CONTINUE.
ENDIF.

***in HTML we have to follow this syntax to save image in HTML file itself, So replace body CID with BD64 code
***<img src=”data:image/png;base64,iVBORw0KGgoAAAANS……8bgAAAAASUVORK5CYII=”>

CONCATENATE ‘data:’ ls_attach-mime_type ‘;base64,’ lv_bd64_string INTO lv_bd64_string.
REPLACE ALL OCCURRENCES OF lv_str IN ls_body-content_ascii WITH lv_bd64_string.
ENDLOOP.

***Save file on application server
OPEN DATASET lv_pathname FOR OUTPUT IN TEXT MODE ENCODING DEFAULT. “#EC CI_USE_WANTED
IF sy-subrc NE 0.
CONTINUE.
ENDIF.

TRANSFER ls_body-content_ascii TO lv_pathname. “#EC CI_USE_WANTED

CLOSE DATASET lv_pathname. “#EC CI_USE_WANTED

***Read file again in Binary Mode
OPEN DATASET lv_pathname FOR INPUT IN BINARY MODE. “#EC CI_USE_WANTED
IF sy-subrc EQ 0.
CLEAR lv_data.
READ DATASET lv_pathname INTO lv_data. “#EC CI_USE_WANTED
IF sy-subrc EQ 0.

****Save additional attachment to email
lt_crmpcd1 = cs_attachment_folder-attachment_folder_bulk_replica-attachment_folder_replicate_re[].
READ TABLE lt_crmpcd1 INTO ls_crmpcd1 INDEX 1.
IF sy-subrc EQ 0.
lt_document = ls_crmpcd1-attachment_folder-document[].

ls_document-path_name = ‘./Original_Mail.html’.
ls_document-name = ‘Original_Mail.html’.
ls_document-file_content_binary_object-content = lv_data.
ls_document-file_content_binary_object-file_name = ‘Original_Mail.html’.
ls_document-VISIBLE_INDICATOR = ‘true’.
ls_document-VERSIONING_ENABLED_INDICATOR = ‘true’.
ls_document-CATEGORY_CODE = ‘2’.
ls_document-MIMECODE = ‘text/html’.
ls_document-ACTION_CODE = ’04’.
APPEND ls_document TO lt_document.

ls_crmpcd1-attachment_folder-document[] = lt_document[].
MODIFY lt_crmpcd1 FROM ls_crmpcd1 INDEX 1.
cs_attachment_folder-attachment_folder_bulk_replica-attachment_folder_replicate_re[] = lt_crmpcd1[].
*              ENDIF.
ENDIF.
ENDIF.
ENDIF.
CLOSE DATASET lv_pathname. “#EC CI_USE_WANTED
ENDIF.

ENDIF.

ENDLOOP.

OUTPUT/CONCLUSION:

When transferred that Email again from CRM to C4C, an attachment with name “Original_Mail.html” is attached to Email. Check below screen shot:

When Double click on attached html file, below file opens

Leave a Reply

Your email address will not be published. Required fields are marked *