CX Works

CX Works brings the most relevant leading practices to you.
It is a single portal of curated, field-tested and SAP-verified expertise for SAP Customer Experience solutions

Overview for Implementing Custom Logic in SAP Marketing Cloud

26 min read

Overview for Implementing Custom Logic in SAP Marketing Cloud

In this article, you will learn about the available options to enhance and extend the SAP Marketing Cloud solution by implementing your own custom logic.

Table of Contents

What is Custom Logic?

Custom logic is logic that you create to enhance SAP standard delivery implementations. To accommodate customers requirements, SAP provides predefined points to extend the solutions. This enhancement implementation is also known as Business Add-Ins (BAdI).

In SAP Marketing Cloud, it is possible to extend the logic of the following business contexts:

Business Contexts Context Description
MKT_DIGITAL_ACCOUNT Marketing: Digital Account
MKT_EXPORT_DEFINITION Marketing: Export Definition
MKT_INITIATIVE Marketing: Campaign
MKT_INI_EXT_SUCCESS_DATA Marketing: Campaign Performance Actual Measure
MKT_INI_PERFORMANCE_TARGET Marketing: Campaign Performance Target Measure
MKT_INTERACTION Marketing: Interaction
MKT_INTERACTION_CONTACT Marketing: Contact and Corporate Account
MKT_LANDING_PAGE Marketing: Form
MKT_MARKETING_AREA Marketing: Marketing Area
MKT_PERMISSION Marketing: Permission
MKT_PLAN Marketing: Marketing Plan
MKT_PROGRAM Marketing: Program
MKT_RECO Marketing: Recommendations
MKT_SURVEY Marketing: Survey
MKT_CONTACT_UI Marketing: Contact
MKT_BUPA_IC_MAP Marketing: Business Partner Mapping
MKT_ACCOUNT Marketing: Corporate Account

Marketing: Marketing Location

You can find the overview of business contexts and their scope items with the Extensibility Cockpit app in SAP Marketing Cloud.

In general, this is the process to implement custom logic:

SAP Documentation:  Creating Custom Logic :

  • Create your own enhancement implementations by using  ABAP for Key Users .
  • Publish enhancement implementations to your test system.
  • Edit enhancement implementations or delete  enhancement implementations that have already been published.
  • Export custom logic implementation as software collections to the production system.

Creating custom logic is one of the options available to extend and enhance the SAP Marketing Cloud standard delivery. You can find more information in our Extensibility Guide as well as in the below video:

Video 1: Extensibility Options - SAP Marketing Cloud

An introduction in ABAP for Key Users 

ABAP for Key Users is a subset of the ABAP programming language. You can use this ABAP variant to implement business logic extensions using a web-based editor provided in SAP Marketing Cloud.

You can find more information on how to develop using ABAP for Key Users in our SAP Documentation: Introduction to ABAP for Key Users. Note: restricted ABAP syntax exists. For example, any database operation except selects from released views, tweaking new tasks, dynamic programming, obsolete ABAP statements, and code generation. Additionally, only allowlisted (released) APIs can be used.

SAP Marketing Cloud provides the following classes that can be used in custom logic:

ABAP Class Name Description
CL_CUAN_CE_IC_QUERY Search for Interaction Contact
CL_CUAN_CE_IA_HELPER  Interaction Helper
CL_CUAN_CE_IC_HELPER Interaction Contact Helper
CL_CUAN_CE_MKT_ATTR_CAT_HELPER Helper Class for Marketing Attributes Categories
CL_CUAN_CE_MKT_PERM_HELPER Helper Class for Marketing Permission
CL_CUAN_INTERACTION_HELPER Helper Methods for Interactions
CL_CUAN_LOC_HELPER  Helper Class for Marketing Location
CL_CUAN_MKT_EXEC_HELPER Marketing Execution: Helper Class
CL_CUAN_PRODUCT_HELPER Helper Class for Product


Helper Class for System Info
CL_CUAN_INTERACT_CNTCT_HELPER  Helper Class for Get Additional Contact Fields
CL_CUAN_CAMPAIGN_HELPER_API Helper Class for Campaign Data

Example 1: Use case implementation using CL_CUAN_CE_IC_QUERY class to search for an existing interaction contact.

The ABAP class returns the matching interaction contact.

   lt_contact 	TYPE cuan_t_qr_ce_ic_root_ext,
   ls_query 	TYPE cuan_s_q_ce_ic_root_ext,
   lt_query 	TYPE cuan_t_q_ce_ic_root_ext.

* Create Query internal table to search for existing contacts
   CLEAR ls_query.
   ls_query-name_last = 'Doe'. 		"last name
   ls_query-name_first = 'John'. 	"first name
*  ls_query-name_text = . 			"full name
*  ls_query-country = . 			"country code
*  ls_query-country_ft = . 			"country free text
*  ls_query-region = . 				"region code
*  ls_query-region_ft = . 			"region free text
*  ls_query-postcode1 = . 			"postal code
*  ls_query-city1 = . 				"city
*  ls_query-street = . 				"street
*  ls_query-house_num1 = . 			"house number
*  ls_query-title = . 				"title code
*  ls_query-title_ft = . 			"title free text
*  ls_query-search_text = 			"search text

   APPEND ls_query TO lt_query.

* search for interaction contacts
		query = lt_query " Interaction Contact Query
		contact = lt_contact " Interaction Contact Root Result

Example 2: How to use SY-DATUM?

For ABAP developers, it is common to use SYST structure (sy-datum, sy-uzeit, sy-uname and others). As you can only use allowlisted APIs, you cannot use SYST in ABAP for Keys user. Instead, you call the appropriate method of the class CL_ABAP_CONTEXT_INFO.

DATA(v_date) = cl_abap_context_info=>get_system_date( ).
DATA(v_time) = cl_abap_context_info=>get_system_time( ).
DATA v_text TYPE string.


You can select data from standard Core Data Services (CDS) views like I_Mkt_Initiative (campaign data), I_Mkt_Interaction (interactions), I_Mkt_Contact (contacts) and others.


    IF  marketing_area_id IS INITIAL.
	  SELECT SINGLE MarketingArea
        INTO @data(v_id_marketing_area)
        FROM I_Mkt_Initiative
        WHERE CampaignID = @CAMPAIGN_ID.

Or you can also select data from a specific Custom Business Object (CBO) in custom logic. The CBO generates a CDS View. Once generated, you just need to SELECT FROM "CDS View" in your custom logic.

* Select additional MARKETING AREAs from CBO: YY1_ADD_MKT_AREA

    SELECT id_mkt_area_allowed FROM yy1_add_mkt_area
      APPENDING TABLE @target_mkt_area_ids
      WHERE id_mkt_area_origin = @sorce_mkt_area_id.

Use Helper Class to read standard data. For example, to read campaign attributes, use CL_CUAN_CAMPAIGN_HELPER_API.

DATA: v_campaign_id         TYPE cuan_initiative_id,
      o_oc_header_attribute TYPE REF TO cuan_s_oc_header_attribute,
      s_campaign_root       TYPE cuan_s_campaign_root_api.

     READ TABLE header_attributes REFERENCE INTO o_oc_header_attribute 
                  WITH KEY param_name = if_cuan_mkt_orch_constants=>sc_open_channel_header_attr-campaign. " CAMPAIGN
      IF sy-subrc IS INITIAL. " Campaign identifier has been passed in parameter HEADER_ATTRIBUTES
          v_campaign_id = o_oc_header_attribute->param_value.
          s_campaign_root = cl_cuan_campaign_helper_api=>campaign_root_read( EXPORTING iv_id = v_campaign_id ).   
		  IF s_campaign_root-priority EQ cl_cuan_campaign_helper_api=>co_priority-high.
*				....
*				....

Best Practices for ABAP Coding

Area Description

Naming Conventions

The purpose of using a standardized name convention is to considerably increase the maintainability of customer-specific adaptations and expansions. Define general rules and familiarize developers to them.

Code Commenting

Make sure that the code has explicit comments about the functionality of the implemented custom logic.

Error Handling

When executing particular ABAP statements, the SAP kernel sets the value of the global variable SY-SUBRC. Generally, a SY-SUBRC value of zero indicates a successful execution of a statement.  SY-SUBRC system field is still available with ABAP for Key Users.  Always check SY-SUBRC in your ABAP custom code after READ internal table, SELECT data or CALL to internal functions. Also, always check for Exceptions when you call ABAP-class methods.

SELECT clauses in LOOP should be avoided. As an alternative, use VIEWS, JOINS or the addition clause FOR ALL ENTRIES in SELECTS.


  • Make sure the internal table referenced with FOR ALL ENTRIES is not empty, otherwise all items will be selected from the datasource table.
  • Make sure there the internal table does not contain duplicate entries, otherwise duplicated records will be loaded from the datasource table. Use DELETE ADJACENT DUPLICATES clause to clean internal tables.
Field Symbols Field symbols refers to existing data. Work in a LOOP or iterations for each row from internal tables using Field Symbols. This is always faster than copying or moving the data to auxiliary work structure areas, especially if the table content needs to be changed. You should use field symbols whenever possible. 

How to Implement Custom Logic

In SAP Marketing Cloud, you access the  Custom Fields and Logic  app to implement custom logic.

A description of the custom logic procedure can be found in the Creating Custom Logic documentation.

Create an enhancement implementation by choosing a business context and BAdI. After, add an implementation description and ID and the Web-Development Editor will be enabled for ABAP custom logic implementation.

In the Editor, there are code examples to help you implement your custom code as well as detailed BAdI documentation.

Custom Logic Tracing

SAP Marketing Cloud enables you to trace BAdI implementations after publishing by the Custom Logic Tracing app. You can trace the values of the input, the actions, as well as check the execution processing time of your custom logic. You can also see which exceptions are raised and where they occur.

It is a powerful troubleshooting app to find implementation issues. It can also help you to identify performance tuning points in your custom code.

Which Enhancements are Available to Implement in SAP Marketing Cloud?

SAP Marketing Cloud offers several points to enhance the standard solution. You can find a list of available BAdIs in SAP Help.

Digital Account

Customize the access token request for digital account services.

Business Context BAdI Definition Name BAdI Description


CUAN_DA_GET_ACCESS_TOKEN Getting of Access Token

Export Definition

For campaigns using Export Files Definition, you can implement custom logic to modify files structure, add columns, hide columns by checking user authorization, filter the list of File Export Definitions in campaigns, and to modify the header, attributes or target group members during file exportation.

Business Context BAdI Definition Name BAdI Description


MKT_EXPORT_DEFINITION HPA, EXP_ADD_ATTR_FOR_EXP_FILE Add Attributes to Export Definition Content
MKT_EXPORT_DEFINITION HPA_EXP_DEF_CHECK_AUTH4COLUMN Check User Authorization for Preview Columns of Export Content 
MKT_EXPORT_DEFINITION HPA_EXP_GET_EXPORT_DEFINITIONS Configure Retrieval of Dropdown List for Export Definitions 
MKT_EXPORT_DEFINITION HPA_EXP_DEF_GET_KEYS Get Keys to Determine the CSV and Preview Content 
MKT_EXPORT_DEFINITION HPA_EXP_DEF_SET_OBJECT_HEADER Set Object Header and Content Attribute Columns in CSV File
MKT_EXPORT_DEFINITION HPA_EXP_DEF_COLUMN_NO_INI_VAL Set Flag for Specific Columns: No Initial Attribute Value from Segmentation


You can adapt and enhance campaign execution procedure based on your requirements, update attributes, change hyperlinks or filter campaigns during replication to SAP Sales and Service solutions.

Business Context BAdI Definition Name BAdI Description
MKT_INITIATIVE CUAN_CAMPAIGN_CHECK Check Attributes of Campaign Header
MKT_INITIATIVE CUAN_CAMPAIGN_UPDATE Update Attributes of Campaign Header
MKT_INITIATIVE CUAN_CMSG_ADAPT_HYPERLINKS Adjust Hyperlink Parameters While Sending Emails
MKT_INITIATIVE CUAN_CPG_EXEC_DYN_CNTNT_MTH Dynamic Content Retrieval for Campaign Execution
MKT_INITIATIVE CUAN_LM_CAMPAIGN_REPLICATION Filter for Campaigns to Be Replicated to SAP Sales and Service solutions
MKT_INITIATIVE CUAN_LM_TARGET_SYS_TYPE Lead Management: Determine Target System Type
Set Default Values for Campaign Template Header

Open Channel

Enhancements to implement open channel for Custom Action during Campaign Executions. Find more information in Open Channel documentation.

Business Context BAdI Definition Name BAdI Description
MKT_INITIATIVE CUAN_CPG_OPN_CHNL_DEFINITION (1) Open Channel: Define Implementations: Mandatory 
MKT_INITIATIVE CUAN_CPG_OPN_CHNL_IA_TEMPL (2) Open Channel: Define Parameters for Implementation: Optional 
MKT_INITIATIVE CUAN_CPG_OPN_CHNL_PARAMETER (3) Open Channel: Define Global Settings for Execution: Optional
MKT_INITIATIVE CUAN_CPG_OPN_CHNL_PRE_PROC (4) Open Channel: Enhance Payload for Data Transfer: Optional
MKT_INITIATIVE CUAN_CPG_OPN_CHNL_PRE_TRANSF (5) Open Channel: Define Template for Outbound Interaction: optional

Campaign Performance

Enhance the data checks during import of campaigns performance data.

Business Context BAdI Definition Name BAdI Description



Adapt Campaign Success Before Import


You can modify interaction data before import or adapt interaction data displayed in the interaction history list (Contacts and Profiles app in SAP Marketing Cloud, Interaction tab).

Business Context BAdI Definition Name BAdI Description
MKT_INTERACTION CUAN_IA_REVISE_FOR_DISPLAY Revise Interaction Data for Display on the UI

BAdIs for MKT_INTERACTION are called for each interaction event. Always try to restrict additional database SELECTs (filtering by Interaction Types per example) during your custom logic implementation. Performance is critical.

You can also use the custom logic below to update your custom business objects (CBOs) based on successfully saved interactions . You get the complete package of interactions which have been imported in one request. Changing the interactions itself is currently not permitted.

Business Context BAdI Definition Name BAdI Description
MKT_INTERACTION CUAN_IA_IMPORT_FOLLOW_UP Process Follow-Up Steps After Successful Import of Interaction Data

Contact and Corporate Account

You can modify interaction contacts or golden record when importing a contact, modify a contact when importing an interaction, or modify finding-matching logic enhancing the contact merge process.

Business Context BAdI Definition Name BAdI Description
MKT_INTERACTION_CONTACT CUAN_IMPORT_IA_4_IC Review Imported Interaction Updating an Interaction Contact
MKT_INTERACTION_CONTACT CUAN_IMPORT_IA_IC_ADJUST Adjust System Behavior on Interaction Contact Import
MKT_INTERACTION_CONTACT CUAN_IMPORT_IC Review Imported Interaction Contact Data
MKT_INTERACTION_CONTACT CUAN_UPDATE_IC_ROOT Update Interaction Contact (Best Record)

Business Partner Mapping

You can adjust Business Partner data for S/4HANA integration

Business Context BAdI Definition Name BAdI Description


Business Partner mapping: Adjust relationship code values



Business Partner mapping: Determine relevant address usages



Business Partner mapping: Adjust Business Partner code values



Business Partner mapping: Adjust role mapping



Business Partner mapping: Adjust corporate account extension fields



Business Partner mapping: Adjust contact person extension fields



Business Partner mapping: Get relevant relationship categories

MKT_BUPA_IC_MAP CUAN_BUPA_MAP_MKT_AREA Business Partner mapping: Adjust Marketing Area


You can modify data in the Contact UI.

Business Context BAdI Definition Name BAdI Description
CUAN_FC_EXTFIELD_CONTACT_UI Field Control of Extension Fields in Contact UI


You can modify data in the Account UI.

Business Context BAdI Definition Name BAdI Description
MKT_INTERACTION_CONTACT CUAN_FC_EXTFIELD_ACCOUNT_UI Field Control of Custom Fierls in Spotlightin Account UI


You can change the data on the form content before publishing them during automated publication, create custom logic to determine which contacts should be updated during the forms input data and also prefill data with information of  SAP Marketing has already collected about your contacts.

Business Context BAdI Definition Name BAdI Description
MKT_LANDING_PAGE CUAN_CP_DEPLOY_ADJUST_FILES Change File Contents Before Form Page Publication
MKT_LANDING_PAGE CUAN_CP_READ_CONTACT_PREFILL Read Contact Data for Form Personalization
MKT_LANDING_PAGE CUAN_CP_SEND_CONFIRMATION Control Sending of Confirmation Email for Double Opt-In Process

Marketing Area

You can allow additional marketing areas for target groups or switch to a different marketing area during the Campaign Execution.

Business Context BAdI Definition Name BAdI Description
MKT_MARKETING_AREA CUAN_MKT_EXEC_MARKETING_AREA Marketing Area Separation for Campaign Execution

Allowed Marketing Areas

You can find more information regarding Marketing Area custom logic in the Help SAP documentation.

Marketing Permission

Implement your own logic to handle marketing permissions, newsletter subscriptions and suppression rules.

Business Context BAdI Definition Name BAdI Description

Marketing Plan

Enhancements to check and update attributes for marketing plan before saving it.

Business Context BAdI Definition Name BAdI Description
MKT_PLAN CUAN_MARKETING_PLAN_CHECK Check Attributes of Marketing Plan Header
MKT_PLAN CUAN_MARKETING_PLAN_UPDATE Update Attributes of Marketing Plan Header
MKT_PLAN CUAN_FC_EXTFIELD_MKTPLN_UI Change the behaviour of custom fields in the Marketing Plans UI

Marketing Program

Enhancements to check and update attributes for marketing program before saving it.

Business Context BAdI Definition Name BAdI Description
MKT_PROGRAM CUAN_PROGRAM_CHECK Check Attributes of Program Header
MKT_PROGRAM CUAN_PROGRAM_UPDATE Update Attributes of Program Header
MKT_PROGRAM CUAN_FC_EXTFIELD_PROGRAM_UI Change the behaviour of custom fields in the Programs UI

Corporate Account

Change the behaviour of custom fields in the Spotlighting Accounts user interface.

Business Context BAdI Definition Name BAdI Description
MKT_ACCOUNT CUAN_FC_EXTFIELD_ACCOUNT_UI Field Control of Custom Fields in Spotlighting Account User Interface

Marketing Location

Change the behaviour of extension fields in the Marketing Locations UI.

Business Context BAdI Definition Name BAdI Description
MKT_LOCATION_UI CUAN_FC_EXTFIELD_LOCATION_UI Field Control of Extension Fields in Marketing Locations UI


Implement custom logic to modify recommendations parameters before the Recommendation Scenario is calculated, and also to modify the impressions of (mass) recommendations.

Business Context BAdI Definition Name BAdI Description
MKT_RECO RECO_AGGREGATE_IMPRESSIONS Aggregate or Transform Runtime Recommendation Impressions
MKT_RECO RECO_AGGREGATE_MASS_IMPRESSNS Aggregate or Transform Mass Runtime Recommendation Impressions
MKT_RECO RECO_ENHANCE_RUNTIME_PARAMETER Enhance Recommendation Runtime Parameters


Implement custom logic to check campaign spends before to save campaigns

Business Context BAdI Definition Name BAdI Description
MKT_SPEND CUAN_CAMPAIGN_SPEND Custom checks on the campaign spend before a campaign is saved


Implement custom logic to modify survey data before import load.

Business Context BAdI Definition Name BAdI Description
MKT_SURVEY CUAN_SVY_REVISE_IA_FOR_IMPORT Revise Interaction Data for Survey Before Import

Implementation Examples

In this section, you will find some BAdI implementation examples to handle different customer requirements. 

Use-Case example 1: Customer requirement: to add new validation status to a specific ID_ORIGIN for a contact by using the BAdI CUAN_UPDATE_IC_ROOT.

* Sample logic setting the IC ROOT validation status *
*  c_anonymous         =  '  '
*  c_self_identified   =  '10'
*  c_qualified         =  '20'
*  c_business_partner  =  '30'

    LOOP AT lt_ia_facet REFERENCE INTO lr_ia_facet.
      CASE lr_ia_facet->id_origin.

        WHEN cl_cuan_ce_ic_helper=>gc_crm_bupa           OR  "SAP CRM Business Partner
             cl_cuan_ce_ic_helper=>gc_erp_bupa           OR  "SAP ERP Business Partner
             cl_cuan_ce_ic_helper=>gc_erp_contact        OR  "SAP ERP Contact
             cl_cuan_ce_ic_helper=>gc_erp_customer       OR  "SAP ERP Customer
             if_cuan_ce_c=>co_origin_sap_erp_company     OR  "SAP ERP Company
             if_cuan_ce_c=>co_origin_sap_c4c_indiv_cust.     "SAP C4C Individual Customer
                 es_ic_new-validation_status = c_business_partner.


Use-Case example 2: Customer requirement: to define an Interaction, use the same Marketing Area from a Contact assigned to the interaction during Import Interaction by using the BAdI CUAN_IA_REVISE_FOR_IMPORT.

DATA: lv_marketing_area type c length 40.

IF interaction_data-mkt_area_id is initial 
OR interaction_data-mkt_area_id EQ 'GLOBAL'.

SELECT SINGLE InteractionContactMktgArea FROM I_MKT_IntactnCntctMktgArea
WHERE InteractionContactOrigin EQ @interaction_data-id_origin
AND InteractionContactId EQ @interaction_data-id
AND InteractionContactUUID EQ @interaction_data-contact_key
INTO @lv_marketing_area.

*   Set Marketing Area into Interaction
interaction_data-mkt_area_id = lv_marketing_area.


Use-Case example 3: Customer requirement: to add custom attributes to the Open Channel Campaign when sending data to the external system by using the BAdI  CUAN_CPG_OPN_CHNL_PARAMETER.

  DATA: v_campaign_id        TYPE cuan_initiative_id,
        s_header_Attributes  LIKE LINE OF header_attibutes,
        s_campaign_root      TYPE cuan_s_campaign_root_api.

    package_size = 100. " change the number of target group members processed in one package and transferred in the odata payload
    CHECK_PERMISSION = abap_false.

    read table HEADER_ATTRIBUTES assigning FIELD-SYMBOL(<fs_head>) WITH KEY param_name = 'CAMPAIGN'.
    if sy-subrc eq 0.
       v_campaign_id = <fs_head>-param_value.

   s_campaign_root = cl_cuan_campaign_helper_api=>campaign_root_read( EXPORTING iv_id = v_campaign_id ).

    clear s_header_attributes.
    s_header_attributes-param_name = 'Z_NEW_FIELD'.
    s_header_attributes-param_value = s_campaign_root_api-yy1_new_field.
    append s_header_attributes to HEADER_ATTRIBUTES.

Use-Case example 4: Customer requirement: Count the number of likes issued by a consumer in a custom CBO if the interaction is flagged as a like by using the BAdI  CUAN_IA_IMPORT_FOLLOW_UP.

DATA: ls_like TYPE yy1_likesofconsumer,
      lv_key  TYPE yy1_kr_likesofconsumer. 

  " Work only on all successfully saved interactions which are flagged as likes
  " For performance reasons always
  "   - try to find a proper where condition and
  "   - if possible avoid a loop in a loop
  LOOP AT interaction_data ASSIGNING FIELD-SYMBOL(<ls_interaction_data>) WHERE yy1_like_mia IS NOT INITIAL.
        "Fill the key (in this scenario it is only a variable)
        lv_key = <ls_interaction_data>-contact_key.
        "Search the CBO for the contact
        DATA(lo_like) = io_cbo_write->get_root(
          business_object_id = 'YY1_LIKESOFCONSUMER'
          key                = lv_key
        "If it is available, get the exisiting CBO data
        lo_like->get( IMPORTING data = ls_like ).

        "Increase the value by 1
        ls_like-likes = ls_like-likes + 1.

        "Now update the CBO data
        lo_like->update( CHANGING data = ls_like ).

      CATCH cx_cbo_write_not_existing cx_cbo_write_locked cx_cbo_write_not_authorized.

        "If the contact is not yet saved on the database create a first record with the first Like value
        ls_like = VALUE yy1_likesofconsumer( contactkey = <ls_interaction_data>-contact_key
                                                     likes      = 1 ). "Fill the data structure
            "Create the record for this contact
            lo_like = io_cbo_write->create_root(
              business_object_id = 'YY1_LIKESOFCONSUMER'
              data               = ls_like
          CATCH cx_cbo_write_not_authorized cx_cbo_write_locked.


This article introduced you to an overview for implementing custom logic in SAP Marketing Cloud. Now, you know what to consider to extend the standard solution, the steps to start your implementation code, and which options are available for enhancement. 

To find out more about extensibility, go to: Extensibility Overview for SAP Marketing Cloud