54
CRM Programming Fundamentals CRM Programming Fundamentals

CRM Programming

Embed Size (px)

Citation preview

CRM Programming Fundamentals

CRM Programming Fundamentals

CRM Programming Fundamentals

Contents1. One Order Object Model.................................................................................3

2. One Order Reporting Framework...............................................................163. Commonly used Function Modules in CRM..............................................194. Commonly used BadI’s in CRM..................................................................275. Copy Control in Business Transactions...................................................306. Transaction CRMV_EVENT.........................................................................327. Debugging in middleware...........................................................................378. Error handling in CRM................................................................................389. Overview of Customizing............................................................................4210. Helpful Reports............................................................................................43

CRM Programming Fundamentals

1. One Order Object ModelThe CRM One Order documents are created in transaction ‘CRMD_ORDER’.The Header table that is updated by the created document is ‘CRMD_ORDERADM_H’.The primary key for this table is GUID (Global Unique Identifier). For each document created, a unique number (GUID) is generated by the system which depends on the Logical name of the system. The field ‘Object ID’ of table ‘CRMD_ORDERADM_H’ contains the document number.

The Item table for One Order document is ‘CRMD_ORDERADM_I’. Each Item of a One Order document has a different GUID. The Header GUID of the Item is stored in field ‘HEADER’ of table ‘CRMD_ORDERADM_I’.

Data Model:-

Administration (Header / Item) Header identifies the order in the system Item holds the relation to the order

Extension

One-Order Object Model

CRMD_ORDERADM_H CRMD_ACTIVITY_H

CRMD_OPPORT_H

Administration header and Extension have the same GUIDAdministration header Extension

CRMD_LINK CRMD_SALES

CRMD_SHIPPINGLink handler

Set

Administration header and Set have different GUID. They are linked via the Link Handler

NoteNormally, an extension contains H or I in the name. It shows if this extension belongs to the header or to the item. Typically extensions store information which is specific to the header/item. The information which can belong to both header and item is typically stored in sets.

CRMD_ORDERADM_I CRMD_SERVICE_I

CRM Programming Fundamentals

1:1 Relation to the administration header OR to the administration item

Sets (Link Handler) 1:1 relation to the header and/or the item optimal data space because sharing of header and item data is

possible possibility to assign the same data to header and items it is only allowed to reference to an admin segment (1 level)

Data model

OrderadmOrderadm_h_h

GUIGUIDD

DatDataa

Activity_Activity_hhREF_GUIREF_GUIDD

DatDataa

OrderadmOrderadm_i_i

GUIGUIDD

DatDataa

HeadHeaderer

Opport_Opport_hhREF_GUIREF_GUIDD

DatDataa

ServiceService_i_iREF_GUIREF_GUIDD

DatDataa

ProductProduct_i_iREF_GUIREF_GUIDD

DatDataa

CRM Programming Fundamentals

Sets / Link Handler:-

Data model: Sets / Link Handler

OrderadmOrderadm_h_h

GUIGUIDD

DatDataa

OrderadmOrderadm_i_i

GUIGUIDD

DatDataa

HeadHeaderer

OrderadmOrderadm_i_i

GUIGUIDD

DatDataa

HeadHeaderer

OrgmOrgmananGUIGUIDD

DatDataa

OrgmOrgmananGUIGUIDD

DatDataa

Link HandlerLink Handler

HG01 - OG01HG01 - OG01

IG02 – OG02IG02 – OG02

CRM Programming Fundamentals

SETS <-> EXTENSIONSThe product data is an extension of the item data. As all extensions are always linked to one header or one item, it does not need its own handle or guid. This also applies for the fields table. A reference object type is also not necessary, as an extension can always be assigned either at header or item level.

Sets are data structures that should be used by several objects (reusable structures). The sales set is an example of a set. It can be assigned to several items. Therefore a link handler is necessary.

LINK-HANDLER: => DB table: CRMD_LINKFunction module: CRM_LINK_GET_OW

Example: Order 5041922Header-Guid: 7BD735B78D3F60499A4797FE7B56922B(search CRMD_ORDERADM_H with object_id = 5041922)Item-Guid: 4EC37010164E7846AAE59325700181CA(search CRMD_ORDERADM_I with header = Header-Guid)

Product data => EXTENSION(search CRMD_Product_I with Guid = Item-Guid)

Sales data => SET(search CRMD_LINK with GUID_HI = Item-GuidGuid_SET = 55F37D59A6DE8F40996EBFDB6ED5C582 OBJTYPE_SET (Sales = 11, see CRMC_OBJECTS)Then search CRMD_SALES with Guid = GUID_SET.

Working with Business Transactions – SAP GUI

Maximum / minimal display

Display or hide

locator

Display business partner cockpit

Application log: Displays error

messagesWork with several

documents in parallelCompress header data

Settings

CRM Programming Fundamentals

A Service Order looks as follows:

Service ProcessService process has leading transaction category BUS2000116.Transaction to maintain service process: CRMD_BUS2000116Assigned transaction categories: Service, Sales, Business activity.Service process can be distributed into R/3.Main partner function category: Sold-to partyAdditional customizing for Service Process:

Subject profile (for codes)Transaction type for confirmation

Service process also contains parts of customizing from Business Activity and Sales document.Information available in Service process:

Header dataService dataSubject + IBaseStatusActivity dataObjectsPricingConditions

ShippingBillingProductsPartnerActionsOrganizational dataDatesTexts

CRM Programming Fundamentals

The Follow-up documents like, Confirmation from the Service Order can be created as shown below:

Service Process IIService process has a specific UI different from sales document or activity. Fast entry, Item detail, Transaction data buttons are available and can be used to switch between screens.

Service process can contain different items:Sales items

For sales items a sales order can be created in R/3 for complete delivery process

Service itemsTo identify the customer wishes, contains planning-relevant data

Spare partsMaterial which is required in the context of a service item

BADI for Service Process: CRM_SERVICE_I_BADI for item data (no service-specific header data)

CRM Programming Fundamentals

Then, copy the items as needed. Select the Item’s & Copy:

Service ConfirmationService Confirmation has leading transaction category BUS2000117.Transaction to maintain service confirmation: CRMD_BUS2000117Assigned transaction categories: Service, Sales, Business activity.Working times and spare parts from service conformation are posted to R/3. Main partner function category: Sold-to party.Additional customizing for Service Confirmation:

Subject profile (for codes)Confirmation also contains parts of customizing from Business Activity and Sales document.Information available in Service Confirmation:

Header dataService dataSubject StatusActivity dataObjectsPricingTexts

Conditions BillingProductsPartnerActionsOrganizational dataDates

CRM Programming Fundamentals

Similarly, we can create Billing Requests from Confirmation.All these documents update the same database tables. But are differentiated from each other by PROCESS_TYPE and OBJECT_TYPE in table CRMD_ORDERADM_H.

Service Confirmation IIService confirmation like a service process has a specific UI different from sales document or activity. Fast entry, Item detail, Transaction data buttons are available and can be used to switch between screens.

Service confirmation can contain different items. These items are normally copied from the service process for which the confirmation is created:Service items

Working time is distributed to payroll data of HR system and costs to CO.Spare parts

Goods issue is posted to MM and costs to CO.

In order to create a confirmation from a service process the process should have status “Released” both on header and item level. As soon as confirmation is saved, the service process status is set to “Confirmed”.

CRM Programming Fundamentals

Architecture of the systems (CRM & R/3):-

DB tables

CRMD_SCHEDLINVBEPCRMD_PRODUCT_IMAEPV/MAAPVCRMD_PRICINGVBKDPRCD_CONDKONVCRM_JEST*VBUK/VBUP

CRMD_ORDERADM_IVBAPCRMD_ORDERADM_HVBAK

CRMR/3

CRMD_SCHEDLINVBEPCRMD_PRODUCT_IMAEPV/MAAPVCRMD_PRICINGVBKDPRCD_CONDKONVCRM_JEST*VBUK/VBUP

CRMD_ORDERADM_IVBAPCRMD_ORDERADM_HVBAK

CRMR/3

Most of the tables have the GUID as a key fielde.g. 0F53ADA4ADA3A440A3E5288A3055F739

(GUID: Universal Unique Identifier, data type CRMT_OBJECT_GUID, domain SYSUUID, RAW 16)

Workareas:

- lu_orderadm_h_wrk old data (like *VBAK)- ls_orderadm_h_wrk local data- gs_orderadm_h_wrk global data

* For an overview on the different status see Include CRM_STATUS_CON

Middleware

CRM system OLTP R/3

CRM Online

R/3 Adapter

BW Adapter

Plug-In

BW APO

IPC

CRM------------>BDOC------------>R/3Middleware RFC

CRM Programming Fundamentals

Layers of programming model in business transactions:-

User interface layer (UI)

Each layer should communicate only to directly linked layers: UI to IL, IL to UI and OW (OB), OW (OB) to IL or DB, DB to OW (OB)

Interaction layer (IL)

Object layer

Object Buffer (OB) Object Work area (OW)

Database layer

Database Buffer (DB) Database Update (DU)

User Interface

Interaction Layercommon

Object LayerApplicationlogic

API (def. Methods)

BAPI

Database Layer

Methode Methode Methode Methode

GUI-Screens / Web / ...

Communication between UI and Business Logic,Transport and conversion of data,Screen sequence control,

Methods to create, read and changeWell defined interfacesDefinition of business rules Interpretation of the customizingMerge and checks

Read and save data from/to database

For Screen For WEB

CRM Programming Fundamentals

CRM Layer model II

Database Layer

- DB -Original

Data- DU -Update

task

Object Layer

- OW -Business

logic- OB -Modified

Data

Customizing - CD -Dialo

g- CB -Method

s

DB

Interaction Layer

- IL -

User Interface

- UI - „read“„save“

CRM Programming Fundamentals

User Interface in Business Transaction:-User Interface in Business Trans.

User interface in business transactions consists of all screens/subscreens and screen sequence controlling customizing that is provided by SAP and should be changed by the customer very carefully.Screens that are shown in transaction CRMD_ORDER are located in the function groups ending with _UI. Example: CRM_OPPORT_UI.Example: screen 7111.Typical flow logic of the CRM screen:PBO

get data from Interaction Layer* get data from Interaction Layer MODULE DATA_7110_GET.perform field checks* Modify Screen Fields MODULE DATA_7110_FIELDCHECK.

PAIput data to Interaction Layer MODULE DATA_7110_PUT ON CHAIN-REQUEST.

User Interface in Business Trans. IIScreen sequence controlling customizing defines which the screens should be displayed, in which order, etc. This customizing defines the way how business transaction is displayed.

Transaction to maintain screen sequence

controlling: CRMV_SSC

CRM Programming Fundamentals

Customer Extensions in Business Transact.Customers can extend standard CRM business transactions with their own fields/screens. In order to enable this, two special extensions were added into CRM business transactions object model. These extensions are CUSTOMER_H and CUSTOMER_I.IMG documentation: Customer Relationship Management -> Basic Functions -> System Modifications -> Business Add-in for Customer Enhancements on Header Level.The structure to be extended with customer fields on header level:CRMT_CUSTOMER_H_EXT. Structure CRMT_CUSTOMER_H_EXT is already included into all important structures and database tables.

Logic of CRM_ORDER

CRM_ORDER_READ_OW

CRM_ORDER_SAVE_OW CRM_ORDERADM_H_SAVE_OBCRM_ORDERADM_I_SAVE_OBCRM_LINK_SAVE_OWCRM_ORDER_UPLOAD_SINGLE

CRM_ORDER_READ

CRM_ORDER_MAINTAIN

CRM_ORDER_SAVE

CRM_ORDER_MAINTAIN_MULTI_OW

CRM_ORDER_MAINTAIN_SINGLE_OW

CRM_ORDER_H_MAINTAIN_OWCRM_ORDERADM_H_MAINTAIN_OW

CRM_ORDER_I_MAINTAIN_OW CRM_ORDERADM_I_MAINTAIN_OW

I II III

CRM Programming Fundamentals

Overview of Naming conventions used by SAP:

2. One Order Reporting FrameworkOne Order Reporting Framework is used to retrieve the documents for particular Selection criteria. Direct Selection from database table is not advised.

As an example we will retrieve Documents, based on the ‘Process type’ & ‘Sales organization’. Let us compare with a SELECT statement to make it more understandable. Assume, the documents are retrieved from table XYZ, so our SELECT statement will be as follows:

SELECT <document no>FROM TABLE XYZWHERE PROCESS_TYPE = … Block 1AND SALES_ORG = … Block 3

Block 2 In One Order Reporting Framework we will select Documents as follows:

Naming conventions

Development classes (packages):CRM_ + object nameExample: CRM_ACTIVITY_H

Function groups:CRM_ + object name + layerExample: CRM_ACTIVITY_H_OB

Function modules:CRM_ + object name + function name + layerExample: CRM_ACTIVITY_H_PUT_OB

DDIC structures:CRMT_ + object name + structure typeExample: CRMT_ACTIVITY_H_WRK

DDIC tables:CRMD_ + object nameExample: CRMD_ACTIVITY_H

Variables:lv_ + variable name / gv_ + variable nameExample: lv_guid, gv_guid

Structures:ls_ + structure name / gs_ + structure nameExample: ls_orderadm_h_wrk

Internal tables:lt_ + table name / gt_ + table nameExample: gt_orderadm_h_wrk

CRM Programming Fundamentals

First of all we will build the ‘WHERE’ clause as mentioned above for the SELECT statement. It is build using classes & methods. Class ‘CL_CRM_REPORT_QUPART’ and Method ‘GET_QUPART_BY_TOKEN’ is used for this purpose.Block 1 is built as follows:

CALL METHOD cl_crm_report_qupart=>get_qupart_by_token EXPORTING iv_entityname = 'CL_CRM_REPORT_EXT_ORDERADM_H' iv_fieldname = 'PROCESS_TYPE' iv_token = 'RAN' it_rangetab = it_type[] IMPORTING ev_qupart = iv_qupart-querypart.

The parameter, ‘iv_entityname’ contains the name of the Segment for which the data will be retrieved. The parameter values can be found in program ‘CRM_OBJECT_NAMES_CON’. The parameter, ‘iv_fieldname’ contains the name of the field for which the SelectionClause is built.‘iv_token’, is used to let the system know the type of operand you are passing in the parameter ‘it_rangetab’. In our example, ‘RAN’ means Range Table. Go to domain ‘CRM_REPORT_TOKEN’ to see all possible values. Parameter ‘it_rangetab’ contains the range table we are passing to the method. This range table contains the values the user has entered in the selection screen for ‘Process Type’. Do not use the method call if it_type[] is initial.The parameter ‘ev_qupart’, contains final query built by the method for Block 1.‘iv_qupart’ is of type ‘CRMT_REPORT_QUERYPART_TA’.

We need to store all these queryparts in an internal table which can be passed to the method which does the selection based on these queryparts (WHERE CLAUSE). So, an internal table say, ‘it_qupart’ of type ‘CRMT_REPORT_QUERYPART_TA’ is created, where the parameter ‘iv_qupart’ returned by the method is appended.

Now, before building the query part for ‘Sales organization’ (Block 3), we need to build Block 2. (Because there is more than one field in WHERE clause).So, how to build Block 2 now:-Same methods are used as follows: CALL METHOD cl_crm_report_qupart=>get_qupart_by_token EXPORTING iv_token = 'AND' IMPORTING ev_qupart = iv_qupart_and-querypart.

Now, ‘iv_qupart_and’ is appended to internal table ‘it_qupart’.

CRM Programming Fundamentals

Block 3 has to be built now same like Block 1 and appended to internal table ‘it_qupart’. CALL METHOD cl_crm_report_qupart=>get_qupart_by_token EXPORTING iv_entityname = 'CL_CRM_REPORT_SET_ORGMAN' iv_fieldname = 'SALES_ORG' iv_token = 'RAN' it_rangetab = it_sales_org[] IMPORTING ev_qupart = iv_qupart-querypart.

Now, we do not need Block 2 anymore as we do not have any further fields for selection.Internal table ‘it_qupart’ contains the complete WHERE CLAUSE. We will see how this is used to retrieve the Documents.

Create Object:

CREATE OBJECT cl_question “ Constructor EXPORTING it_query = it_qupart[] iv_accessrule_name = 'CL_CRM_REPORT_ACCRULE_ONEORDER'.

cl_question refers to Class ‘CL_CRM_REPORT_QUESTION’.It_qupart is the internal table that we populated above.'CL_CRM_REPORT_ACCRULE_ONEORDER' refers to the One Order Reporting Framework.

CALL METHOD cl_question->refresh.

CALL METHOD cl_question->get_ext_object EXPORTING iv_name = gc_objectname_reporting-locatorlist IMPORTING ev_instance = l_list.

lv_locatorlist ?= l_list.

Where,l_list TYPE REF TO cl_crm_report_ext.lv_locatorlist TYPE REF TO cl_crm_report_ext_locatorlist.

Finally, call the following Method:

CALL METHOD lv_locatorlist->get_extension EXPORTING it_requested_columns = lt_requ_columns

CRM Programming Fundamentals

IMPORTING et_extension = lt_list.

Where,

lv_locatorlist TYPE REF TO cl_crm_report_ext_locatorlist.

lt_requ_columns TYPE crmt_report_requ_columns_ta. (For the Columns you require.For example:ls_requ_columns-column = 'GUID'.ls_requ_columns-requested = abap_true.APPEND ls_requ_columns TO lt_requ_columns. ls_requ_columns-column = 'OBJECT_TYPE'.ls_requ_columns-requested = abap_true.APPEND ls_requ_columns TO lt_requ_columns.

ls_requ_columns-column = 'OBJECT_ID'.ls_requ_columns-requested = abap_true.APPEND ls_requ_columns TO lt_requ_columns.

ls_requ_columns-column = 'PROCESS_TYPE'.ls_requ_columns-requested = abap_true.APPEND ls_requ_columns TO lt_requ_columns.)

lt_list TYPE crmt_report_locatorlist_ta. This contains the final list of selection.

3. Commonly used Function Modules in CRMThe function module ‘OWN_LOGICAL_SYSTEM_GET’ can be used to get the name of the Logical System.

The function module ‘CRM_STATUS_READ’ is used to read the status of the document.

The function module ‘CRM_STATUS_CHANGE_FOR_ACTIV_OW’ is used to change the Status of documents, both Header & Item. Even function module 'CRM_STATUS_MAINTAIN_OW' can be used to change the status of a document.For example:

ls_status_com-activate = 'X'. ls_status_com-ref_guid = ls_guid. ls_status_com-ref_kind = ''. ls_status_com-status = 'E0029'.

CRM Programming Fundamentals

INSERT ls_status_com INTO TABLE lt_status_com. CLEAR lt_input_field_names. INSERT gc_fieldname_activate INTO TABLE lt_input_field_names.

CALL FUNCTION 'CRM_STATUS_MAINTAIN_OW' EXPORTING it_status_com = lt_status_com* IT_STATUS_COMINT = iv_check_only = ''* IV_TRACE =

* IV_NO_1O_MAINTAIN = FALSE* IV_FORCE_1O_MAINTAIN = FALSE* IV_GET_ACTUAL_EXCPN = FALSE CHANGING ct_input_field_names = lt_input_field_names EXCEPTIONS not_allowed = 1 error_occurred = 2 warning_occurred = 3 OTHERS = 4 . IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.

The function module ‘CRM_ORDER_CHECK_AUTHORITY_GEN’ is used to check the modification authorization for a document.

The function module ‘NREL_GET_NEIGHBOURHOOD’ is used to read the document flow.For example:

CALL FUNCTION 'NREL_GET_NEIGHBOURHOOD' EXPORTING is_object = v_borident* IT_ROLERANGE = it_relrange = v_relrange[] i_depth = '00'* BYPASS_BUFFER = ' '* IMPORTING* EP_EXIST_LINKS = TABLES links = it_links

CRM Programming Fundamentals

* ROLES = .

The structure ‘v_borident’ should contain the object GUID, Object type and logical system name.

The structure ‘v_relrange’ should be like this : v_relrange-sign = 'I'.v_relrange-option = 'EQ'.v_relrange-low = 'VONA'.v_relrange-high = ''.APPEND v_relrange.CLEAR v_relrange.

The internal table ‘it_links’ is returned by the function module which contains all the documents linked with the object GUID passed in the structure ‘v_borident’. The function module ‘CRM_SRV_UPDATE_DOCFLOW’ is used for creating Document Flow. This function module can be used to create document flow between R/3 & CRM documents as well.Even function module ‘CRM_ORDER_MAINTAIN’ can be used to create document flow.For example:

ls_guid = ls_documents-guid. INSERT ls_guid INTO TABLE it_guid.

* Fill input fields for maintain ls_input_field_names-fieldname = 'OBJKEY_A'. INSERT ls_input_field_names INTO TABLE lt_input_field_names. ls_input_field_names-fieldname = 'OBJTYPE_A'. INSERT ls_input_field_names INTO TABLE lt_input_field_names. ls_input_field_names-fieldname = 'OBJKEY_B'. INSERT ls_input_field_names INTO TABLE lt_input_field_names. ls_input_field_names-fieldname = 'OBJTYPE_B'. INSERT ls_input_field_names INTO TABLE lt_input_field_names. ls_input_field_names-fieldname = 'VONA_KIND'. INSERT ls_input_field_names INTO TABLE lt_input_field_names. ls_input_field_names-fieldname = 'BREL_KIND'. INSERT ls_input_field_names INTO TABLE lt_input_field_names. ls_input_field_names-fieldname = 'BREL_MODE'. INSERT ls_input_field_names INTO TABLE lt_input_field_names. ls_input_field_names-fieldname = 'RELTYPE'. INSERT ls_input_field_names INTO TABLE lt_input_field_names.

CRM Programming Fundamentals

ls_input_field-ref_guid = ls_guid. ls_input_field-ref_kind = gc_object_kind-orderadm_h. ls_input_field-objectname = 'DOC_FLOW'. ls_input_field-field_names[] = lt_input_field_names[]. ls_input_field-logical_key = '1'. APPEND ls_input_field TO lt_input_field. ls_doc_link-objkey_a = ls_guid. ls_doc_link-objtype_a = gc_object_type-service. ls_doc_link-objkey_b = ls_documents-material_document. ls_doc_link-objtype_b = 'BUS2017'. ls_doc_link-brel_mode = gc_object_kind-orderadm_h. ls_doc_link-logsys_b = ls_documents-logical_system. ls_doc_link-reltype = 'VONA'. ls_doc_link-brel_kind = gc_object_kind-orderadm_h. ls_doc_link-log_key = '1'. INSERT ls_doc_link INTO TABLE ls_doc_flow-doc_link.

ls_doc_flow-ref_guid = ls_guid. ls_doc_flow-ref_kind = gc_object_kind-orderadm_h. INSERT ls_doc_flow INTO TABLE lt_doc_flow.

CALL FUNCTION 'CRM_ORDER_MAINTAIN' CHANGING ct_input_fields = lt_input_field ct_doc_flow = lt_doc_flow EXCEPTIONS error_occurred = 1 document_locked = 2 no_change_allowed = 3 no_authority = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.

The function module ‘CRM_CREATE_BREL_TO_CONTRACT_OW’ is used to link document flow to a contract. This function module is used to update Released Value in a contract. The function module ‘CRM_DELETE_BREL_TO_CONTRACT_OW’ is used to reverse the document flow, in case of deletion.

The function module ‘CRM_INTLAY_GET_PROCESS_TYPE’ is used to determine Process Type for a document.

CRM Programming Fundamentals

The function module which contains the string ‘INTLAY’ returns data from the Interaction Layer. For example, if you are processing a document, and you need the HEADER GUID of the document which is not available in your program, you can use those FM’s which will return the desired data. Because, the Interaction Layer is populated when we use function modules like CRM_ORDER_READ etc.

The function module ‘BBP_PD_ALL_GET_OBJECT_ID’ is used to get the Object ID from the Object GUID.

The function module ‘CRM_STATUS_BUFFER_REFRESH’ is used to refresh the buffer. This function module should be used to refresh the buffer after any change of status of the document.

The function module ‘CRM_1O_CALL_FOREIGN’ is used to call transaction ‘CRMD_ORDER’. You have to pass the document number which you have to open. You can open the document either in Change/Display mode.

The function module ‘CRM_ORDERADM_H_GUID_GET_OW’ is used to get the GUID from Object ID.

The function module ‘CRM_HEADER_GUID_GET_OBJ_ID’ is used to get the Object ID from GUID.

The function module ‘CRM_ORDER_GET_HEADER_GUID’ is used to get Header GUID from Item GUID.

The function module 'IB_CONVERT_INTO_TIMESTAMP' can be used to convert to timestamp.

The function module 'CRM_ACTIVITY_H_MAINTAIN_OW' to Create/Change Activity Header Data. For example:

* Check whether Activity Header Category Data exists for this Order CALL FUNCTION 'CRM_ACTIVITY_H_READ_OB' EXPORTING iv_guid = iv_header_guid IMPORTING es_activity_h_wrk = lx_act_h_wrk EXCEPTIONS parameter_error = 1 record_not_found = 2 at_least_one_record_not_found = 3 OTHERS = 4. IF sy-subrc <> 0.

CRM Programming Fundamentals

* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

MESSAGE i022(zcrm_dev). PERFORM update_message USING iv_header_guid. EXIT. ENDIF.* If no Activity Header Exists, then Create (MODE = A)* If it exists then change with (MODE = B) IF lx_act_h_wrk IS INITIAL. lx_act_h-mode = 'A'. ELSE. lx_act_h-mode = 'B'. ENDIF. lx_act_h-ref_guid = iv_header_guid. "Head GUID lx_act_h-category = 'ZR1'.* Input Fields to be changed lx_input_field_names-fieldname = 'CATEGORY'. INSERT lx_input_field_names INTO TABLE lit_input_field_names. lx_input_field_names-fieldname = 'MODE'. INSERT lx_input_field_names INTO TABLE lit_input_field_names.* Create/Change Activity Header Category Data CALL FUNCTION 'CRM_ACTIVITY_H_MAINTAIN_OW' EXPORTING is_activity_h_com = lx_act_h CHANGING ct_input_field_names = lit_input_field_names EXCEPTIONS header_create_error = 1 header_change_error = 2 parameter_error = 3 admin_h_reference_error = 4 OTHERS = 5. IF sy-subrc <> 0.* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. MESSAGE i022(zcrm_dev). PERFORM update_message USING iv_header_guid. EXIT. ENDIF.

Use of function module CRM_ORDER_READ:-The function module CRM_ORDER_READ is used to read the Segment Data of a One Order Document.

CRM Programming Fundamentals

The function module is used to read Header as well as Item details (all segments) by passing the Header GUID of the document in the parameter “IT_HEADER_GUID” of the function module.To read only Item specific detail, pass the Item GUID of the document in the parameter “IT_ITEM_GUID” of the function module.

You can read more than one document details by passing more than one GUID in the parameter “IT_HEADER_GUID”.

The function module CRM_ORDER_READ returns data of all the Segments of the document. To read only specific Segment data, pass the Segment Name in the parameter “IT_REQUESTED_OBJECTS” of the function module, to enhance performance.

The function module first try to read required data from OW (work area), if not available looks at the OB which is the Object Buffer. If still fails, finally it retrieves the data from Database. So, if you look at standard CRM function modules, you will find, suffixes as …_OW / …_OB/ …_DB. Based on your requirement from where you want to read the data, you need to use the respective function module.

Use of Function Module CRM_ORDER_MAINTAIN:-The function module is used to Maintain/Change One Order Document.Depending on the Segment data you are modifying, you need to pass the changed data in the respective parameter of the Segment.For example, if you are modifying Activity data of the Header, you need to pass parameter IT_ACTIVITY_H, similarly, for Activity Item you need to pass data in IT_ACTIVITY_I.It is mandatory to populate the Changing Parameter CT_INPUT_FIELDS. This parameter contains the name of the Segment you are modifying. You can modify as many segments you want to. But you need to pass all the Segments name in the parameter CT_INPUT_FIELDS. You can get the name of the Segment’s in the table CRMC_OBJECTS. If you are modifying any Segment, for which need to populate the field ‘LOGICAL_KEY’ in the parameter CT_INPUT_FIELDS, you need to generate the LOGICAL_KEY each time you use. For example, function module CRM_BILLPLAN_FIND_LOGICAL_KEY is used to generate the LOGICAL_KEY to be used to modify the Segment for BILLING PLAN.You need the LOGICAL_KEY to modify the Segment’s BILLING PLAN, CUMULATED_I, Product Details, and to CANCEL a document.

Do not forget to use the function module ‘CRM_ORDER_SAVE’ to save the changes followed by COMMIT/ROLLBACK work.

Finally, call the function module ‘CRM_ORDER_INITIALIZE’ to refresh the buffer. Care must be taken for locking/unlocking the document before making any changes.

Few points to remember about the function module:-

CRM Programming Fundamentals

Function CRM_ORDER_MAINTAINFunction module CRM_ORDER_MAINTAIN is used for any action performed for business transaction.Recursive calls are not permittedAll extensions and sets are passed to this function moduleMultiple documents can be maintained with one callInput fields (parameter CT_INPUT_FIELDS) are used to identify which fields of the corresponding set/extension should be changedThe function calls CRM_ORDER_MAINTAIN_MULTI_OW to maintain multiple documentsCRM_ORDER_MAINTAIN_MULTI_OW separates the documents and calls CRM_ORDER_MAINTAIN_SINGLE_OW for each of them in case of creation/change/deletion and CRM_ORDER_COPY_SINGLE_OW in case of copying or creating follow-up documentCRM_ORDER_MAINTAIN_SINGLE_OW function calls CRM_ORDER_H_MAINTAIN_OW to maintain header information and CRM_ORDER_I_MAINTAIN_MULTI_OW to maintain item informationCRM_ORDER_H_MAINTAIN_OW calls in a certain order maintenance function modules for extensions and sets belonging to headerAt the end CRM_ORDER_H_COMPLETE_OW is called and events planned for the end of header processing are executed

CRM Programming Fundamentals

Sequence of function modules called within CRM_ORDER_MAINTAIN:-

4. Commonly used BadI’s in CRMThe BadI ‘CRM_ORDER_STATUS’ can be used to process any logic when there is a change of STATUS in the document at the Header/Item level.In the method ‘BEFORE_CHANGE’ you can put your logic/validation etc while the status is changed. And in case of error, raise the exception ‘NOT_ALLOWED’. So, the status change will not be allowed. In the method ‘AFTER_CHANGE’ you can use your logic that needs to be processed after the status is changed. You cannot raise any exceptions. You can put filters for which Status change the BadI should be triggered.Note: - The parameter ‘CHECK_ONLY’ is used in two ways.If you want to throw error messages when the user is trying to change the Status, then trigger the BadI when the ‘CHECK_ONLY’ parameter is initial.If you want to hide the Status, then trigger the BadI when the ‘CHECK_ONLY’ parameter contains ‘X’.

The BadI ‘CRM_ORDER_FIELDCHECK’ is used to dynamically make a field INPUT OFF in transaction CRMD_ORDER.The method ‘FIELDCHECK’ is used for the same.Example: -

CRM_ORDER_MAINTAINCRM_ORDER_MAINTAIN_MULTI_OW

CRM_ORDER_MAINTAIN_SINGLE_OWCRM_ORDER_H_MAINTAIN_OW

CRM_ORDER_H_PREPARE_OWCRM_ORDERADM_H_MAINTAIN_OWCRM_<OBJECT>_MAINTAIN_OW.........CRM_ORDER_H_COMPLETE_OW

CRM_ORDER_I_MAINTAIN_MULTI_OWCRM_ORDER_I_MAINTAIN_SINGLE_OW

CRM_ORDERADM_I_MAINTAIN_OWCRM_<OBJECT>_MAINTAIN_OW.........CRM_ORDER_I_COMPLETE_SINGLE_OW

CRM_ORDER_I_COMPLETE_MULTI_OWCRM_ORDER_COMPLETE_SINGLE_OW

CRM_ORDER_COMPLETE_MULTI_OWCRM_ORDER_FINAL_CALLS_OW

CRM Layer model IV

CRM Programming Fundamentals

METHOD if_ex_crm_order_fieldcheck~fieldcheck.

INCLUDE crm_direct.

DATA: lv_status_completed TYPE crmt_boolean, ls_input_field_names TYPE crmt_input_field_names, ls_pre_status_completed TYPE crmt_status_prestate, ls_sales_wrk TYPE crmt_sales_wrk, lv_mode TYPE crmt_mode, lv_object_type TYPE swo_objtyp, ev_header TYPE crmt_object_guid, lt_crmt_status_tab TYPE crmt_status_tab, wa_crmt_status TYPE crmt_jstat, l_set, crmd_orderadm_h TYPE crmd_orderadm_h.

ev_header = is_fieldcheck_com-ref_guid.

CALL FUNCTION 'CRM_ORDERADM_H_READ_OW' EXPORTING iv_orderadm_h_guid = ev_header IMPORTING ev_mode = lv_mode ev_object_type = lv_object_type EXCEPTIONS admin_header_not_found = 0.

IF lv_object_type IS INITIAL.

CALL FUNCTION 'CRM_ORDERADM_I_READ_OW'

EXPORTING iv_guid = ev_header iv_include_deleted_item = ' ' IMPORTING ev_header_guid = ev_header EXCEPTIONS item_not_found = 1 OTHERS = 2.

CALL FUNCTION 'CRM_ORDERADM_H_READ_OW' EXPORTING iv_orderadm_h_guid = ev_header IMPORTING

CRM Programming Fundamentals

ev_mode = lv_mode ev_object_type = lv_object_type EXCEPTIONS admin_header_not_found = 0.

ENDIF.

CHECK ( lv_object_type = gc_object_type-service ).

*** Make the CATEGORY field Display ==> Header IF iv_object_name = gc_object_name-activity_h. LOOP AT ct_input_field_names INTO ls_input_field_names. IF ls_input_field_names-fieldname = 'CATEGORY'. ls_input_field_names-changeable = gc_changeable-no. MODIFY ct_input_field_names FROM ls_input_field_names TRANSPORTING changeable. ENDIF. ENDLOOP. ENDIF.

ENDMETHOD.

CRM Programming Fundamentals

5. Copy Control in Business Transactions

CRM Programming Fundamentals

Copy Control in Business TransactionsCopy control is a functionality that controls copying business transactions and creating follow-up documents.Copy control is set up for transaction types and for item categories. The first one controls the copying of header data and the second one – item data.Customizing of copy control: IMG -> Customer Relationship Management -> Transactions -> Basic Settings -> Copy Control for Business Transactions.

By default only a part of the information is copied from preceding to follow-up transaction:Transaction description (copied by default)Partners (controlled by the partner determination procedure and access sequence)Texts (controlled by text procedure and access sequence)Items (if specific criteria are met)

The rest of the information can be copied only using a BADI CRM_COPY_BADI.

Copy Control in Business Transactions IICopy control also defines which transaction types are available in the drop-down menu to create a follow-up transaction. By default copying from Business Activity and Task is available into all transaction types. The same applies when Business Activity or Task is created as a follow-up for any transaction.

For other transaction types (not Activity or Task) the document should not contain errors in order to create a follow-up transaction.

Copying from/to Business Activity or Task can be explicitly restricted with BADI CRM_COPY_BADI. But this will not remove Activities and Tasks from the drop-down list of available transaction types. They will still be available in the menu, but copying will be restricted after selection.

If one document is created as a follow-up to another, the organizational data in the follow-up document is not changeable. This is controlled in function module CRM_ORGMAN_SALES_AREA_FC. A modification should be done in order to enable organizational data maintenance in a follow-up transaction.

CRM Programming Fundamentals

Method COPY can be used to restrict copying if some criteria are not met.Other methods are used to copy necessary information from a preceding to a follow-up document. They have all necessary information as the parameters.

6. Transaction CRMV_EVENT

BADI for Copy ControlCopy control has a special BADI CRM_COPY_BADI to enable the customers to influence the copy process. The BADI is filter-dependant. Several implementations with different filter values can be programmed. Filter value is then specified in the customizing as a “copy routine” field.The following methods are available:

SHIPPING Method for Object SHIPPINGCOPY Method for COPY object (copying requirement)ORDERADM_I Method for ORDERADM_I objectPRODUCT_I Method for PRODUCT_I objectORDERADM_H Method for ORDERADM_H objectPRICING Method for object PRICINGSALES Method for SALES objectACTIVITY_H Method for ACTIVITY_H objectORDPRP_I Method for Object ORDPRP_I (Contracts Assortment)BILLPLAN Method for BILLPLAN objectBILLING Method for BILLING objectOPPORT_H Method for OPPORT_H objectLEAD_H Method for Object LEAD_HSCHEDLIN_I Method for Object SCHEDLIN_IFINPROD_I Method for Object FINPROD_ISERVICE_I Method for Object SERVICE_IPRICING_I Method for Object PRICING_IPAYPLAN Method for Object PAYPLANCUSTOMER_I Method for Object CUSTOMER_ICUSTOMER_H Method for Object CUSTOMER_HDATES Method for object DATESCANCEL Method for object CANCELDOC_FLOW Method for Object DOC_FLOW

CRM Programming Fundamentals

In transaction CRMV_EVENT you can configure your own function module which will be triggered based on the configuration.

Event Handler

Maintenance Transaction: CRMV_EVENT (Table with all events)Function Modules: CRM_EVENT_SET_EXETIME_OW CRM_EVENT_PUBLISH_OW

Example: Event: END_ORDERADM_H Function Module: CRM_ORDERADM_H_MAINTAIN_OW

Table with all events that are registered on event „end_orderadm_h“ are processed now

PERFORM EXECUTE_CALLBACKS_PLANNED USING LV_GUID_HDR IV_EXETIME LT_PLAN_EXETS <= table with CallBacks CHANGING LV_CALL_RCODE LT_EVENT_RCODE.

CRM Programming Fundamentals

As an example see below:-

As mentioned above, the function module ‘/ASML1/CRM_BILLREQ_SET_BLOCK’ will be triggered at the end of the billing request when a billing request is created with

Transaction: CRMV_EVENT for Header processing

Event is published by function module „CRM_EVENT_PUBLISH_OW“

Distinguish between Header and Item

CRM Programming Fundamentals

reference. And it will trigger for the HEADER segment. You can set whether the function module should be triggered for header level or at item level. Also, you can set other attributes as shown above. Please go through the transaction to explore further options.

Have a look at the parameters that can be used in the function module. The parameters are filled based on the EVENT.

The function module should be updated in the table ‘CRMC_EVENT_CALL’.

Note: - If you want to update any data in the database table, please take care whether your function module is handling data at the database level or object buffer level. If you are working at the object layer but you need to update database table, do not try to update the tables using CRM_ORDER_MAINTAIN or anything else. Just populate the object buffer with the data you want to be updated in the database. System will take care of the update.

CRM Programming Fundamentals

Event Handler IVThe execution of a specific event is normally done in a following order:Events are planned for execution in a CHANGE function module. All necessary parameters are provided (old/new data, etc.). This is done in function CRM_EVENT_PUBLISH_OW.In MAINTAIN function module the execution time of the event is set and all events planned for that execution time are executed. This is done in function CRM_EVENT_SET_EXETIME_OW.When the event is executed it is removed from the internal table with planned events.

Event table is delivered by SAP and normally should not be changed by the customers. Only in rare cases when certain functionality requires an additional event to be added or an existing event to be changed this can be done after prior consulting with responsible developer.Events influence the whole transaction processing and may cause performance problems if they are not properly set up.

CRM Programming Fundamentals

When & where the EVENT is triggered:-

Event HandlerCRM_ORDER_MAINTAIN

CRM_ORDER_MAINTAIN_MULTI_OWCRM_ORDER_MAINTAIN_SINGLE_OW

CRM_ORDER_H_MAINTAIN_OWCRM_ORDER_H_PREPARE_OWCRM_<OBJECT>_MAINTAIN_OW

CRM_<OBJECT>_PUBLISH_OWCRM_EVENT_SET_EXETIME_OW (end_<OBJECT>)

.........CRM_ORDER_H_COMPLETE_OW

CRM_EVENT_SET_EXETIME_OW (end_header_maintain)CRM_ORDER_I_MAINTAIN_MULTI_OW

CRM_ORDER_I_MAINTAIN_SINGLE_OWCRM_<OBJECT>_MAINTAIN_OW

CRM_<OBJECT>_PUBLISH_OWCRM_EVENT_SET_EXETIME_OW (end_<OBJECT>)

.........CRM_ORDER_I_COMPLETE_SINGLE_OW

CRM_EVENT_SET_EXETIME_OW (end_item_maintain)CRM_ORDER_I_COMPLETE_MULTI_OW

CRM_ORDER_COMPLETE_SINGLE_OWCRM_EVENT_SET_EXETIME_OW (end_order_maintain)

CRM_ORDER_COMPLETE_MULTI_OWCRM_EVENT_EXETIME_MULTI_OW (end_order_multi_maintain)

CRM_ORDER_FINAL_CALLS_OW

Event Handler

Orderadm_i_publish_owAfter_change (old- / new values)

schedlin_i_publish_owAfter_change (old- / new values)

.

.

.

Event handlerPlan (End_Item_Maintain)CRM_PRODUCT_I_Unit_EC

Plan (End_Order_Maintain)CRM_PRIDOC_UPDATE_EC

Event consumer ......CRM_PRODUCT_I_Unit_ECCRM_PRIDOC_UPDATE_ECCRM_PARTNER_DETERM_EC

CRM_EVENT_SET_EXETIME_OW (end_Item_maintain)

.

.

.

CRM_EVENT_SET_EXETIME_OW (end_Order_maintain)

.

.

.

Event handlerPlan (End_Item_Maintain)CRM_PRODUCT_I_Unit_EC

Plan (End_Order_Maintain)CRM_PRIDOC_UPDATE_EC

CRM_PRODUCT_I_Unit_ECAfter_change Orderadm_i(old- / new values)

CRM_PRIDOC_UPDATE_ECAfter_change Orderadm_i(old- / new values)

CRM Programming Fundamentals

7. Debugging in middlewareIt is important to know how to debug the middleware, since in CRM the documents are distributed in R/3.

In CRM, before you SAVE the document go to transaction ‘SMQS’ and De-Register the R/3 destination. Then SAVE the document.Now go to transaction ‘SMQ1’. You will see the document available in the Outbound Queue. You can debug the document now. This is the scenario when the document is transferred from CRM R/3.

For R/3 CRM:While creating the document in CRM, SAVE the document with error (have an error in the document while SAVING it). Go to transaction ‘SMQR’. You will get the document number in this transaction. Select the document and De-Register it. Now, the document has been processed in R/3 and is in the Inbound Queue. Go to transaction ‘SMQ2’ and debug the document.

If you want to see the BDoc messages go to transaction ‘SMW01’. If the document has not been processed (yellow traffic light), you can reprocess the document.

CRM Programming Fundamentals

8. Error handling in CRMApplication Log and Messages

Application log is a Basis functionality used in CRM to store error messages in CRM documents. Application log is saved together with corresponding business transaction.The application log can be displayed in CRMD_ORDER transaction for a currently opened document. An alternative way to display application is to use transaction SLG1. The object is CRM_DOCUMENT.SLG1 can be used to monitor for example order upload errors (CRM -> R/3).In the application log the user can see 3 types of messages:

E – error messages (stored in the application log after saving)W – warning messages (deleted from application log after saving)I – information messages (deleted from application log after saving)

Messages in the application log are identified by message class and message number. There’s a possibility to display detailed information about the message and to navigate to the corresponding field in CRMD_ORDER transaction.SE91 transaction can be used to display message information (in case the customer provides only a number of the message and not the text).

CRM Programming Fundamentals

The function module 'CRM_MESSAGES_CHECK_FOR_ERRORS' is used to check whether any error occurred in the transaction ‘CRMD_OREDR’ while processing.The function module 'CRM_MESSAGES_SEARCH' is used to search for error messages if any error has occurred.The function module 'BAL_LOG_MSG_READ' is used to read the error messages that has occurred while processing the transaction ‘CRMD_ORDER’.

Example:-FORM check_object_for_errors USING iv_obj_guid TYPE crmt_object_guid iv_obj_kind TYPE crmt_object_kind CHANGING p_t_msg TYPE bapiret2_t p_error_occured TYPE crmt_boolean.

DATA: lt_msg_handle TYPE bal_t_msgh,

ls_log_msg_handle TYPE balmsghndl, ls_log_msg TYPE bal_s_msg.

*/ (re)init CLEAR p_error_occured.

Application Log and Messages IICRM messages have also so-called level or degree. The level of the messages defines if this message will be shown in the application log for a specific user.In the user master record (SU01) there’s a parameter CRM_USER_LEVEL. This one specifies the level of the user.The standard settings for message levels (degrees) can be found in transaction CRMC_MSGS.The customer can overwrite system settings with transaction CRMC_MSGC.Restriction: message type cannot be lowered (for example from E to W), it can only be set to higher one (W to E).After setting message level to 9 it will not be shown in the application log.

Messages are collected in the function module CRM_MESSAGE_COLLECT, deleted in CRM_MESSAGES_DELETE.CRM_MESSAGES_SEARCH can be used to search for messages.

CRM Programming Fundamentals

*/ check object for errors CALL FUNCTION 'CRM_MESSAGES_CHECK_FOR_ERRORS' EXPORTING iv_ref_object = iv_obj_guid iv_ref_kind = iv_obj_kind EXCEPTIONS errors_exist = 1 OTHERS = 2. IF sy-subrc EQ 0. "no errors EXIT. ENDIF.

*/ search for error messages CALL FUNCTION 'CRM_MESSAGES_SEARCH' EXPORTING iv_ref_object = iv_obj_guid iv_ref_kind = iv_obj_kind IMPORTING et_msg_handle = lt_msg_handle EXCEPTIONS appl_log_error = 1 error_occurred = 2 OTHERS = 3. IF sy-subrc NE 0. "error PERFORM msg_add USING msgty_s sy-msgid sy-msgno sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 CHANGING p_t_msg. EXIT. ENDIF.

* fill message tab LOOP AT lt_msg_handle INTO ls_log_msg_handle. CALL FUNCTION 'BAL_LOG_MSG_READ' EXPORTING i_s_msg_handle = ls_log_msg_handle IMPORTING e_s_msg = ls_log_msg EXCEPTIONS log_not_found = 1 msg_not_found = 2 OTHERS = 3. IF sy-subrc <> 0. PERFORM msg_add USING msgty_s sy-msgid sy-msgno

CRM Programming Fundamentals

sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 CHANGING p_t_msg. ELSE. PERFORM msg_add USING ls_log_msg-msgty ls_log_msg-msgid ls_log_msg-msgno ls_log_msg-msgv1 ls_log_msg-msgv2 ls_log_msg-msgv3 ls_log_msg-msgv4 CHANGING p_t_msg. IF ls_log_msg-msgty EQ msgty_e OR ls_log_msg-msgty EQ msgty_a.

p_error_occured = abap_true. ENDIF. ENDIF.

ENDLOOP.

ENDFORM. " check_object_for_errors

Note:- The function module ‘CRM_MESSAGE_COLLECT’ will add message to display in the window at the top of transaction CRMD_ORDER.

You can use function module *INTLAY* to get the GUID for the document you are processing. Because the Interaction Layer is populated when there is Function Module processing like CRM_ORDER_READ, CRM_ORDER_MAINTAIN, etc…

CRM Programming Fundamentals

9. Overview of Customizing

Transactions

Item categories

Copying control

Pricing

CRM Programming Fundamentals

10. Helpful Reports

Report Function

CRM_MESSAGES You can use this report to influence the following parameters:

Degree of usage (degree of detail)The degree of detail specifies which user receives what range of messages. The F4 help provides you with the possible values for selection. (9 = BDOCs are shown in the order)

CRM_ORDER_READ Read orders with CRM_ORDER_READ

CRM_ORDER_DELETE Delete orders with CRM_ORDER_DELETE

CRM_ORDER_MAINTAIN Maintain orders with CRM_ORDER_MAINTAIN

CRM_ODE_CHECK_DOC CRM data exchange of customer orders: Check documents concerning inconsistency