Get BOL attachments from CRM document

Problem: you want to get a list of BOL objects related to attachments (documents) assigned to a CRM document.

Solution: the connection between CRM document and CM BO object, which is responsible for attachments, is rather straight forward. The CM BO GUID is the same as CRM document’s.

However, to be perfectly sure you won’t miss any attachments here is a bit more complex code.

DATA:
    ls_document TYPE crmst_adminh_btil,
    ls_cmbo_properties TYPE crmt_cmic_rfolder_attr,
    lo_core TYPE REF TO cl_crm_bol_core,
    lo_cm_bo TYPE REF TO cl_crm_bol_entity,
    lo_cm_bos TYPE REF TO if_bol_entity_col,
    lo_attachments TYPE REF TO if_bol_bo_col,
    lo_document TYPE REF TO cl_crm_bol_entity,
    lo_qs TYPE REF TO cl_crm_bol_query_service,
    lo_tmp_collection TYPE REF TO if_bol_bo_col.
 
  lo_core = cl_crm_bol_core=>get_instance( ).
  lo_core->load_component_set( 'ONEORDER' ).
  lo_document = lo_core->get_root_entity(
    iv_object_name = 'BTOrder'
    iv_object_guid = GUID_OF_A_CRM_DOCUMENT
  ).
  CHECK lo_document IS BOUND.
  lo_document = lo_document->get_related_entity( iv_relation_name = 'BTOrderHeader' ).
 
  CHECK lo_document IS BOUND.
  lo_document->get_properties( IMPORTING es_attributes = ls_document ).
 
  CREATE OBJECT lo_attachments TYPE cl_crm_bol_entity_col.
 
  lo_qs = cl_crm_bol_query_service=>get_instance( cl_crm_cm_genil_comp=>gc_query_bo_link ).
 
  ls_cmbo_properties-instid = ls_document-guid.
  ls_cmbo_properties-typeid = ls_document-object_type.
  ls_cmbo_properties-catid = 'BO'.
 
  lo_qs->set_properties( ls_cmbo_properties ).
  lo_cm_bos = lo_qs->get_query_result( ).
  lo_cm_bo ?= lo_cm_bos->get_first( ).
DATA:
    ls_document TYPE crmst_adminh_btil,
    ls_cmbo_properties TYPE crmt_cmic_rfolder_attr,
    lo_core TYPE REF TO cl_crm_bol_core,
    lo_cm_bo TYPE REF TO cl_crm_bol_entity,
    lo_cm_bos TYPE REF TO if_bol_entity_col,
    lo_attachments TYPE REF TO if_bol_bo_col,
    lo_document TYPE REF TO cl_crm_bol_entity,
    lo_qs TYPE REF TO cl_crm_bol_query_service,
    lo_tmp_collection TYPE REF TO if_bol_bo_col.

  lo_core = cl_crm_bol_core=>get_instance( ).
  lo_core->load_component_set( 'ONEORDER' ).
  lo_document = lo_core->get_root_entity(
    iv_object_name = 'BTOrder'
    iv_object_guid = GUID_OF_A_CRM_DOCUMENT
  ).
  CHECK lo_document IS BOUND.
  lo_document = lo_document->get_related_entity( iv_relation_name = 'BTOrderHeader' ).

  CHECK lo_document IS BOUND.
  lo_document->get_properties( IMPORTING es_attributes = ls_document ).

  CREATE OBJECT lo_attachments TYPE cl_crm_bol_entity_col.

  lo_qs = cl_crm_bol_query_service=>get_instance( cl_crm_cm_genil_comp=>gc_query_bo_link ).

  ls_cmbo_properties-instid = ls_document-guid.
  ls_cmbo_properties-typeid = ls_document-object_type.
  ls_cmbo_properties-catid = 'BO'.

  lo_qs->set_properties( ls_cmbo_properties ).
  lo_cm_bos = lo_qs->get_query_result( ).
  lo_cm_bo ?= lo_cm_bos->get_first( ).

Having the CM BO object you can use CMBODocumentRefRel and CMBODocumentRel relations to retrieve collection of attachment objects.

  WHILE lo_cm_bo IS BOUND.
    lo_tmp_collection = lo_cm_bo->get_related_entities( iv_relation_name = cl_crm_cm_genil_comp=>gc_rel_bo_document_ref ).
    lo_attachments->add_collection( lo_tmp_collection ).
    lo_tmp_collection = lo_cm_bo->get_related_entities( iv_relation_name = cl_crm_cm_genil_comp=>gc_rel_bo_document ).
    lo_attachments->add_collection( lo_tmp_collection ).
 
    lo_cm_bo ?= lo_cm_bos->get_next( ).
  ENDWHILE.
  WHILE lo_cm_bo IS BOUND.
    lo_tmp_collection = lo_cm_bo->get_related_entities( iv_relation_name = cl_crm_cm_genil_comp=>gc_rel_bo_document_ref ).
    lo_attachments->add_collection( lo_tmp_collection ).
    lo_tmp_collection = lo_cm_bo->get_related_entities( iv_relation_name = cl_crm_cm_genil_comp=>gc_rel_bo_document ).
    lo_attachments->add_collection( lo_tmp_collection ).

    lo_cm_bo ?= lo_cm_bos->get_next( ).
  ENDWHILE.

Komentowanie jest zakończone.