How to update BOL entity property value via ABAP code

Suppose I have one product with ID I042416 which could be found in CRM WebClient UI:

I would like to change its description from "i042416" to for example "Jerry test".

Here below is the ABAP code which uses CRM BOL API to achieve.

Execute the report, specify product ID and new description to be updated:

Execute the report, product description is updated:

Double confirm in UI to see updated description as expected:

The complete code could be found below:

REPORT PROD_UPDATE_TEST.

PARAMETERS: prd_id TYPE string,
des_val1 TYPE string,
uom_val2 TYPE string,
sta_val3 TYPE string,
icg_val4 TYPE string,
not_val5 TYPE string,
dis_txt6 TYPE string.

DATA:
      lo_core               TYPE REF TO cl_crm_bol_core,
      lo_collection         TYPE REF TO if_bol_entity_col,
      lo_root_entity        TYPE REF TO cl_crm_bol_entity,
      lo_short_text         TYPE REF TO cl_crm_bol_entity,
      lo_uom                TYPE REF TO cl_crm_bol_entity,
      lo_stat               TYPE REF TO cl_crm_bol_entity,
      lo_matb               TYPE REF TO cl_crm_bol_entity,
      lo_note               TYPE REF TO cl_crm_bol_entity,
      lo_dischain           TYPE REF TO cl_crm_bol_entity,
      lo_dischaintxt        TYPE REF TO cl_crm_bol_entity.

DATA:
      lv_view_name          TYPE crmt_view_name,
      lv_query_name         TYPE crmt_ext_obj_name,
      lt_query_parameter    TYPE crmt_name_value_pair_tab,
      ls_query_parameter    LIKE LINE OF lt_query_parameter,
      lo_transaction        TYPE REF TO if_bol_transaction_context,
      lv_success            TYPE abap_bool,
      lv_changed            TYPE abap_bool,
      lv_size               TYPE I.

* get the product
lo_core = cl_crm_bol_core=>get_instance( ).
lo_core->load_component_set( ‘PROD_ALL‘ ).
lo_transaction = lo_core->get_transaction( ).

lv_query_name = ‘ProdAdvancedSearchProducts‘.

ls_query_parameter-name = ‘PRODUCT_ID‘.
ls_query_parameter-VALUE = prd_id.
APPEND ls_query_parameter TO lt_query_parameter.

lo_collection = lo_core->query(
iv_query_name               = lv_query_name
it_query_params             = lt_query_parameter
iv_view_name                = lv_view_name ).

lv_size = lo_collection->IF_BOL_BO_COL~SIZE( ).

WRITE:/ ‘Product number found:‘ , lv_size.

ASSERT lv_size = 1.

lo_root_entity = lo_collection->get_first( ).

IF des_val1 IS NOT INITIAL.
* update product description
  lo_short_text = lo_root_entity->get_related_entity( ‘ProductShortText‘ ).

  IF lo_short_text IS INITIAL.
    " HANDLING
    lo_short_text = lo_root_entity->CREATE_RELATED_ENTITY( IV_RELATION_NAME = ‘ProductShortText‘ ).
  ENDIF.

  lo_short_text->set_property( iv_attr_name = ‘SHORT_TEXT‘ iv_value = des_val1 ).
  lo_short_text->set_property( iv_attr_name   = ‘LANGU‘    iv_value = sy-langu ).
ENDIF.

IF uom_val2 IS NOT INITIAL.
* update base unit
  lo_uom = lo_root_entity->get_related_entity( ‘ProductUom‘ ).

  IF lo_uom IS INITIAL.
    " HANDLING
    lo_uom = lo_root_entity->CREATE_RELATED_ENTITY( IV_RELATION_NAME = ‘ProductUom‘ ).
  ENDIF.

  lo_uom->set_property( iv_attr_name = ‘UNIT‘ iv_value = uom_val2 ).
ENDIF.

IF sta_val3 IS NOT INITIAL.
* update status
  lo_stat = lo_root_entity->get_related_entity( ‘ProductStat‘ ).

  IF lo_stat IS INITIAL.
    lo_stat = lo_root_entity->CREATE_RELATED_ENTITY( IV_RELATION_NAME = ‘ProductStat‘ ).
  ENDIF.

  lo_stat->set_property( iv_attr_name = ‘STAT‘ iv_value = sta_val3 ).
ENDIF.

IF icg_val4 IS NOT INITIAL.
* update item category group
  lo_matb = lo_root_entity->get_related_entity( ‘ProductMatBasic‘ ).

  IF lo_matb IS INITIAL.
    lo_matb = lo_root_entity->CREATE_RELATED_ENTITY( IV_RELATION_NAME = ‘ProductMatBasic‘ ).
  ENDIF.

  lo_matb->set_property( iv_attr_name = ‘ITEM_CAT_GROUP‘ iv_value = icg_val4 ).
ENDIF.

IF not_val5 IS NOT INITIAL.
* update notes
  lo_note = lo_root_entity->get_related_entity( ‘ProductLongtext‘ ).

  IF lo_note IS INITIAL.
    lo_note = lo_root_entity->CREATE_RELATED_ENTITY( IV_RELATION_NAME = ‘ProductLongtext‘ ).
  ENDIF.

  lo_note->set_property( iv_attr_name = ‘CONC_LINES‘ iv_value = not_val5 ).
ENDIF.

IF dis_txt6 IS NOT INITIAL.
* update sales area text
  lo_dischain = lo_root_entity->get_related_entity( ‘ProductDistrChain‘ ).

  IF lo_dischain IS INITIAL.
    lo_dischain = lo_root_entity->CREATE_RELATED_ENTITY( IV_RELATION_NAME = ‘ProductDistrChain‘ ).
  ENDIF.

  lo_dischaintxt = lo_dischain->GET_RELATED_ENTITY( ‘ProductDcLongtext‘ ).

  IF lo_dischaintxt IS INITIAL.
    lo_dischaintxt = lo_dischain->CREATE_RELATED_ENTITY( IV_RELATION_NAME = ‘ProductDcLongtext‘ ).
  ENDIF.

  lo_dischaintxt->set_property( iv_attr_name = ‘CONC_LINES‘ iv_value = dis_txt6 ).
ENDIF.

* execute modification
lo_core->modify( ).
lv_changed = lo_transaction->check_save_needed( ).

CHECK lv_changed EQ abap_true.
lv_success = lo_transaction->save( ).
IF lv_success = abap_true.
  lo_transaction->commit( ).
  WRITE:/ ‘Product changed Successfully‘.
ELSE.
  lo_transaction->rollback( ).
ENDIF.

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

原文地址:https://www.cnblogs.com/sap-jerry/p/10113991.html

时间: 2024-07-31 13:54:39

How to update BOL entity property value via ABAP code的相关文章

mappedBy reference an unknown target entity property解决方法

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleDaoImpl': Injection of resource methods failed; nested exception is org.springframework.beans.factory.BeanCreationException

Entity Framework With Mysql 之Code First

Entity Framework 4.0现在也可以支持Mysql数据库了,这篇文章将向你展示如何用Code First的方式来实现. 1.首先新建一个项目,在项目中用NuGet添加如下引用: 2.在web.config文件中添加如下配置: 1 <connectionStrings> 2 <add name="MyTestContext" connectionString="Data Source=127.0.0.1;port=3306;Initial Cat

MySQL中进行update/delete操作时,发生 Error Code: 1175

问题为个人遇到记录,各位大牛免喷. 在MYSQL的一张表进行 update 操作时,发现不能操作,发生如下错误. 在对 对于此问题, 原因:MYSQL的安全机制造成的,默认是不允许随意进行 删改 数据. 解决办法 : 将安全机制降低, 执行如下语句即可  set sql_safe_updates = 0; 如图:可成功执行update语句

Entity Framework应用:使用Code First模式管理数据库创建和填充种子数据

一.管理数据库连接 1.使用配置文件管理连接之约定 在数据库上下文类中,如果我们只继承了无参数的DbContext,并且在配置文件中创建了和数据库上下文类同名的连接字符串,那么EF会使用该连接字符串自动计算出数据库的位置和数据库名.比如,我们的数据库上下文定义如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity; 4 using System.Linq; 5 using System.

ubuntu 系统 sudo apt-get update遇到问题sub-process returned an error code

解决办法:输入以下命令: 1. 2. 3. 4.重新输入 原文地址:https://www.cnblogs.com/wxd136/p/9742695.html

entity framework 公共类

public abstract class BaseService<T> where T : class { private static readonly DatabaseContext db = new DatabaseContext(); public static T Add(T entity) { db.Entry<T>(entity).State = EntityState.Added; db.SaveChanges(); return entity; } public

Propagation of Visual Entity Properties Under Bandwidth Constraints

1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) released in September 2004 throughout Europe and North America, localised in 3 languages so far. It has been developed by Nevrax since 2000, and was taken ov

EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

EntityFramework Core 1.1方法理论详解 当我们利用EF Core查询数据库时如果我们不显式关闭变更追踪的话,此时实体是被追踪的,关于变更追踪我们下节再叙.就像我们之前在EF 6.x中讨论的那样,不建议手动关闭变更追踪,对于有些特殊情况下,关闭变更追踪可能会导致许多问题的发生. 实体状态 对于EF Core 1.1中依然有四种状态,有的人说不是有五种状态么,UnChanged.Added.Modified.Deleted.Detached.如果我们按照变更追踪来划分的话,实际

Entity Framework Fluent API

Entity Framework Fluent API - Configuring/Mapping Properties & Types Skip to main content Data Developer Center Sign in United States (English) Home Library Learn Downloads Support Community Forums Documentation Videos Articles Books Hands-on Labs We