使用ABAP代码给SAP CRM Business object创建附件Attachment

简介:

For a complete list of all my blogs regarding content management, please see here.

I create a utility class with method CREATE_DOC. It has following four input parameters:

  1. iv_data type xstring – the binary data which you would like to store as attachment
  2. iv_bor_type type string – the BOR type of your business object. You can view view business object model in tcode SWO1
  3. iv_guid type raw16 – the guid of your business object instance
  4. iv_file_name type string – the file name which will appear in attachment assignment block.

The source code of method below: ( in fact all attributes for an attachment could be available in the input parameters of this method. For simplicity reason I
just hard code them in the sample code )

DATA:ls_bo              TYPE sibflporb,
         ls_prop            TYPE LINE OF sdokproptys,
         lt_prop            TYPE sdokproptys,
         lt_properties_attr TYPE crmt_attr_name_value_t,
         ls_file_info       TYPE sdokfilaci,
         lt_file_info       TYPE sdokfilacis,
         lt_file_content    TYPE sdokcntbins,
         lv_length          TYPE i,
         lv_file_xstring    TYPE xstring,
         ls_loio            TYPE skwf_io,
         ls_phio            TYPE skwf_io,
         ls_error           TYPE skwf_error.
    ls_prop-name = 'DESCRIPTION'.
    ls_prop-value = 'created by Tool'. " replace it with your own description for attachment
    APPEND ls_prop TO lt_prop.
    ls_prop-name = 'KW_RELATIVE_URL'.
    ls_prop-value = iv_file_name. " in the sample code I just reuse file name as relative url
    APPEND ls_prop TO lt_prop.
    ls_prop-name = 'LANGUAGE'.
    ls_prop-value = sy-langu.
    APPEND ls_prop TO lt_prop.
    lv_file_xstring = iv_data.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer        = lv_file_xstring
      IMPORTING
        output_length = lv_length
      TABLES
        binary_tab    = lt_file_content.
    ls_file_info-binary_flg = 'X'.
    ls_file_info-file_name = iv_file_name.
    ls_file_info-file_size = lv_length.
    ls_file_info-mimetype = 'image/jpeg'. "use the correct mime type for your attachment
    APPEND ls_file_info TO lt_file_info.
    ls_bo-INSTID = iv_guid.
    ls_bo-typeid = iv_bor_type.
    ls_bo-catid = 'BO'.
    CALL METHOD cl_crm_documents=>create_with_table
      EXPORTING
        business_object     = ls_bo
        properties          = lt_prop
        properties_attr     = lt_properties_attr
        file_access_info    = lt_file_info
        file_content_binary = lt_file_content
        raw_mode            = 'X'
      IMPORTING
        loio                = ls_loio
        phio                = ls_phio
        error               = ls_error. " evaluate if there is anything wrong during creation

COMMIT WORK.

I write a piece of code to test it. After report runs I could see the generated attachment.

在这里插入图片描述
在这里插入图片描述

You can also test whether the attachment is created successfully in the backend. Test class method get_info in SE24.
Specify importing parameter BUSINESS_OBJECT:


在这里插入图片描述

Execute and you should get result as below: one physical object and one logical object according to how-is-attachment-physically-stored-in-database-table-in-cm-framework.

Never forget to call COMMIT WORK in your code, since the persistence of the relationship between attachment and your business object are implemented via generic object service in a update process.You could easily find this via SAT trace on your code

在这里插入图片描述

or switch on update debugging in your debugger settings.

在这里插入图片描述

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
16天前
|
人工智能 搜索推荐 Serverless
使用金庸的著作,来测试阿里通义千问最新开放的长文档处理功能
使用金庸的著作,来测试阿里通义千问最新开放的长文档处理功能
36 7
使用金庸的著作,来测试阿里通义千问最新开放的长文档处理功能
|
1月前
|
存储 数据处理 开发者
ABAP 如何把 unicode 代码点转换成字符
ABAP 如何把 unicode 代码点转换成字符
19 0
|
1月前
|
中间件
什么是 SAP CRM 系统里的 Initial Download
什么是 SAP CRM 系统里的 Initial Download
23 0
|
1月前
|
中间件
什么是 SAP CRM Initial Download
什么是 SAP CRM Initial Download
20 0
|
1月前
|
存储 数据管理 数据处理
SAP CRM 里 Attachment 搜索的实现逻辑介绍
SAP CRM 里 Attachment 搜索的实现逻辑介绍
18 0
|
1月前
在 CRM WebClient UI Attachment 区域,创建支持 Web Service 的 Word 文档
在 CRM WebClient UI Attachment 区域,创建支持 Web Service 的 Word 文档
21 0
|
12天前
|
存储
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
19 0
|
16天前
|
数据库
SAP CRM产品主数据无法根据产品描述字段进行搜索的原因
SAP CRM产品主数据无法根据产品描述字段进行搜索的原因
15 5
|
16天前
|
Web App开发 开发者 存储
介绍一个 webp 格式转 png 格式的软件:XNConvert
介绍一个 webp 格式转 png 格式的软件:XNConvert
21 6
介绍一个 webp 格式转 png 格式的软件:XNConvert
|
16天前
什么是 SAP ABAP 里的 Subscreen
什么是 SAP ABAP 里的 Subscreen
13 1
什么是 SAP ABAP 里的 Subscreen