Skip to Content

Excerpt from the book SAP CRM: Technical Principles and Programming by Stephen Johannes. Used with permission of SAP PRESS. All rights reserved.

 

This blog explains the process for creating new date rules in SAP CRM.

To create a new date rule, you need to perform a series of configuration and development steps:

1. Open the Date Rule customizing screen by going into the IMG and following the menu path: Customer Relationship Management > Basic Functions > Date Management > Define Date Types, Duration Types and Date Rules.

2. In the dialog structure that appears, select Date Rules, and then click the New Entries button. The screen shown below appears.

3. Enter a technical name for the rule such as “ZBOOKEX1”, and then enter a description such as “Book Example”.

4. Click Save, and you are prompted for a customizing request. If you don’t have an existing customizing request, you’ll create a new one; otherwise, you’ll select one for your work.

5. Go back to the overview screen of all existing date rules as shown below, select the date rule that you created, and then click the Details icon.

6. An overview screen appears showing all of the versions of the underlying XML that makes up the date rule as shown in the figure below. Double-click on the only row, and then you’re ready to start editing the XML of the date rule. There isn’t much documentation on this format, but to invoke an ABAP function module for date rules, you can enter a certain pattern as shown in the second figure and code below.

7. When you first open the editor, it will be in display mode. Click the Change button to insert the necessary XML code.

The Editor for Date Rules screen expects a certain pattern of XML to be entered that specifies the date rule as shown in the following code. The pattern has been derived by examining the delivered date rule in the system called “Planned Date (Activities)” with a technical key of “000000000002”.

<?xml version="1.0"?>

<SAPTimeRule>

  <ABAPTimeRule function="Z_CRM_DATE_CALC"/>

</SAPTimeRule>
8. After you’ve created the XML for the date rule that calls the ABAP function module you’ll create in the next step, save your work.

9. Before you can use this date rule in a date profile, you now must code the ABAP logic that will evaluate the date passed and return a result. Create a function module that has two importing parameters:

  • _ CONTEXT: A type reference to IF_TIMECONTEXT.
  • _ TIMEMESSAGE: A type reference to CL_TIMEMESSAGE.

Your function module also must return a reference value named ERROR that is of type XFLAG.

The context reference provides you access to the date data being manipulated in your function module. The interface allows you to retrieve the list of time events and then add back a special time event called the result, which is the result of your calculation and will populate the date type that will use this rule.

1 Function Module Logic: Date Context

To better understand this logic, let’s take a look at the following function module that calculates your date. The first call will be to get the event set from the context as shown in the code fragment:

li_eventset = context->get_eventset( ).

This is always needed to provide the result back and calculate any dates you may encounter.

Next you’ll get the GUID of the current transaction you’re processing so you can access any data needed for the calculation via the one order API. You’ll use the function module CRM_CONTEXT_GET_ACTUALGUID_OW to get this GUID.

After you have the GUID, you retrieve any other data from the business transaction using the one order OW function modules by passing in the header GUID. As you might have noticed, your function module doesn’t provide any data about the business transaction you’re working with directly.

If your calculation is based on another date, you first need to retrieve the value of that date by retrieving it from the event set. The following code fragment can be used to achieve this in your date rule function module:

li_tns_zfirstcont ?= li_eventset->get_by_name( lv_name ).

if li_tns_zfirstcont is initial.

  exit.

endif.

Note that you won’t want to process the date rule if the date you need to calculate hasn’t been entered or already calculated. Thus, you’ll exit the function module to prevent any short dumps with improper date calls.

One big difference of working with a date context as compared to standard ABAP time and date types is that you can’t perform simple add or subtract functions on the dates themselves. You instead need to create a duration that corresponds to how much you want to increment or decrement the date and then apply the duration to the date itself. To do this, create a new duration object using the class CL_TIMEDURA. You can then calculate the new date by using the static method CL_TIMECALC.

You should now be able to create new date rules within SAP CRM!

Excerpt from the book SAP CRM: Technical Principles and Programming by Stephen Johannes. Used with permission of SAP PRESS. All rights reserved.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Stephen Johannes

    I’m only commenting because well because it’s fun to see something I wrote four years ago.  I must admit it’s probably not the example I would have shared, because custom date rules via code in SAP CRM is something so obscure that I doubt most implementations have even tried applying this technique.

    I’m interested in whether any readers here have actually tried this out in their system and perhaps used it to solve a business requirement.

    Take care,

    Stephen

    (0) 

Leave a Reply