SAP CRM — Marketing — Create Campaign in Batch mode

In this post let us see how we can create a campaign in batch process. If you want to create the campaign process online click here.

Create Campaign Function / Class Moduel in Batch process:

If you have a text file or the data in a table read the information in a internal table and now we need to loop thru the internal table(this is regular abap coding I am not going to explain that over here).

Inside this loop you need to use the following class and method to create the campaign.

      check if the campaign is already created , if there is no campaign then do the following

      Class name cl_crm_mktpl_appl_base->ELEMENT_CREATE.

      then call cl_crm_mktpl_appl_base->save

      and cl_crm_mktpl_appl_base->commit of this class.

      If you have BW and  want to sync with the system then

     call cl_crm_mktpl_appl_base->after_commit.

  if the campaign is already created then

       read the campaign using

       cl_crm_mktpl_appl_base->Element_read

       cl_crm_mktpl_appl_base->Element_change

         then call cl_crm_mktpl_appl_base->save

         and cl_crm_mktpl_appl_base->commit of this class.

         If you have BW and  want to sync with the system then

         call cl_crm_mktpl_appl_base->after_commit.

end the loop.

The above is the pseudo code for the batch process.

Element_create  can be used for all types of marketing element creation like Marketing plan, Campaign, Promotion and trade promo. The important thing is you need to mention the Object_type and and Object_class and if this element has a parent then mention the parent GUI. Look at the following screen shot for the different object class and the object type with in that class

All the above information is stored in CRM_MKTPL_OBJCL table. So the above explains us the campaign has two types one is campaign and another is campaign element under campaign, this is same for other elements.

See the following source code for campaign creation or update depending on the situation.

*&———————————————————————*
*& Report  ZER_TEST_CAMPAIGN
*&———————————————————————*
REPORT  ZER_TEST_CAMPAIGN.
DATA: zobj TYPE CGPL_OBJECT_TYPE,
zmkt TYPE CRM_MKTPL_MKTELEMENT,
ext_id type CRM_BSP_MKTPL_EXT_ID,
et_campaign_id type table of CGPL_WORKLIST_ITEM,
es_campaign_id type cgpl_worklist_item,
zcpg TYPE CGPL_GUID16,
zcpt TYPE CGPL_GUID16,
rec type ref to CL_CGPL_HIERARCHY_NODE.

zobj = ‘CPG’.
ext_id  = ‘Z_TEST_SE38_013’.
zmkt-EXTERNAL_ID =  ext_id.
zmkt-TEXT1 = zmkt-EXTERNAL_ID.
zmkt-OBJECT_TYPE = ‘CPG’.
zmkt-OBJECT_CLASS = ‘CP’. “Tells us this is campaign
zmkt-LANGU = ‘EN’.

DATA: lr_appl_base TYPE REF TO cl_crm_mktpl_appl_base,
      lv_subrc           TYPE sysubrc.

lr_appl_base = cl_crm_mktpl_appl_base=>get_instance( ).

* check if the Campaign is already created with the external id
SELECT guid external_id INTO CORRESPONDING FIELDS OF TABLE et_campaign_id
   FROM cgpl_project WHERE object_type = ‘CPG’
    and external_id = zmkt-EXTERNAL_ID.

if sy-subrc = 4” No campaign found
  CALL METHOD lr_appl_base->ELEMENT_CREATE
    EXPORTING
*    IM_MKTELEMENT_GUID         =
      IM_OBJECT_TYPE             = zobj
      IM_ATTRIBUTES              = zmkt
*    IV_USE_EXTERNAL_ATTRIBUTES =
*    IM_TEXTS                   =
*    IM_PARENT                  =
*    IM_CHECK_ATTRIBUTES        =
*    IT_SETTYPE_VALUES          =
*    IV_DEFAULTING              = ‘X’
*    IV_SUPPRESS_PARDET         = SPACE
    RECEIVING
      RE_NODE                    = rec
    EXCEPTIONS
      GUID_EXISTS                = 1
      INCONSISTENT_DATA          = 2
      PARENT_OBJECT_LOCKED       = 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.
  rec->GET_GUID_OF_NODE( receiving RE_GUID = zcpg  ).
  CALL METHOD lr_appl_base->save
    EXCEPTIONS
      failed = 1
      OTHERS = 2.
  CALL METHOD lr_appl_base->commit.
*– after commit ——————————————————-
  CALL METHOD lr_appl_base->after_commit
    IMPORTING
      ev_bw_subrc = lv_subrc.
    CALL METHOD lr_appl_base->commit.
*– after commit ——————————————————-
    CALL METHOD lr_appl_base->after_commit
      IMPORTING
        ev_bw_subrc = lv_subrc.
  write ‘ Marketing Campaign created’.

  endif.
else.
  write ‘Aleady exist’.
* raise CPG_Create_ERROR.
* Read the element and change the data.
  Data: l_attributes type CRM_MKTPL_MKTELEMENT.
  read table et_campaign_id into es_campaign_id index 1.
  CALL METHOD lr_appl_base->ELEMENT_READ
    EXPORTING
      IM_MKTELEMENT_GUID         = es_campaign_id-GUID
*    IM_CHANGE_MODE             = ‘X’
*    IM_RELOAD                  =
*    IM_READ_ARCHIVE            = ‘X’
*    IM_RAISE_EVENT             = ‘X’
*    IV_USE_EXTERNAL_ATTRIBUTES =
  IMPORTING
     EX_ATTRIBUTES              = l_attributes
*    EX_R3_ATTRIBUTES           =
*    EX_PARENT_GUID             =
*    EX_TEXTS                   =
*    EX_NODE                    =
*    ET_SETTYPE_VALUES          =
*    EX_EEW                     =
*    EX_CHANGE_MODE_FAILED      =
  EXCEPTIONS
    NOT_FOUND                  = 1
    others                     = 2.
* chagne the data 
Here I am just changing the description
  concatenate l_attributes-TEXT1 ‘_Test01’ into l_attributes-TEXT1 .
  CALL METHOD lr_appl_base->ELEMENT_CHANGE
      EXPORTING
        IM_MKTELEMENT_GUID          = l_attributes-guid
        IM_ATTRIBUTES               = l_attributes
*      IV_USE_EXTERNAL_ATTRIBUTES  =
*      IM_TEXTS                    =
*      IT_SETTYPE_VALUES           =
*      IV_DEACTIVATE_CUST_DEFAULTS = SPACE
*      IV_SUPPRESS_PARDET          = SPACE
      EXCEPTIONS
        NOT_FOUND                   = 1
        OBJECT_LOCKED               = 2
        others                      = 3.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
  CALL METHOD lr_appl_base->save
    EXCEPTIONS
      failed = 1
      OTHERS = 2.
  CALL METHOD lr_appl_base->commit.
endif.

If you have any question please let me know.  

6 thoughts on “SAP CRM — Marketing — Create Campaign in Batch mode

  1. Thanks you !!!!!!!!!!!!!!!!!!!!

    It’s great you published this sample!!!!!!!!!!!!!!!!!!!!!!!

    It’s exactly what i need to do so thanks you again.

  2. Hi,

    I have a requirement(CRM 7.0 Loyalty Management) in which i have to automate the below steps.Can u plz provide me with some inputs for the same:
    1.Automate the process of creation of segments then
    2.Automate the process of creation of campaigns then
    3.Automate the process of creation of reward rules

    Thanks,
    Ugandhar

  3. hello, I’ve already donde the example that you described here and I’m getting some error during save method, code CRM_MKTPL(034). Do you have any tips about it. Thanks in advance for any help.

  4. I have to thank you for this great sample, this is exactly what I needed. I did have to change the RE_NODE to rv_new_guid because it did not exist in our version of this method. I could not have asked for it to do more. I started out trying to use the MKT_ELEMENT_CREATE fm but this was much better.
    Thanks again,
    Donna

  5. I used this example but added the camp type to the zmkt, it does not update the camp_type in table CRM_MKTPL_ATTR, the record does get created. Any advice, thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s