CRM & Sales

Creating New Date Rules in SAP CRM

Date rules in SAP CRM are ways to dictate time. For example, you may set one date rule for the beginning of a contract and another date rule for when that contract’s duration has passed.


Therefore, it’s important to know how to actually create date rules in SAP CRM. To do so, follow this outline which provides the 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.


New Entry


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.


Change View Date Rules


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.


Date Rule Version Selection


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"?>


<ABAPTimeRule function="Z_CRM_DATE_CALC"/>



Date Rule Editor


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.

  • 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: 1) CONTEXT, a type reference to IF_TIMECONTEXT. 2) 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.


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.




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.



With the information outlined in this blog post, you should be able to create new date rules in SAP CRM. What will you use them for?


Editor’s note: This post has been adapted from a section of the book SAP CRM: Technical Principles and Programming by Stephen Johannes.


SAP CRM: Technical Principles and Programming
SAP CRM: Technical Principles and Programming

If you've ever had to work on the SAP CRM backend, this book is the resource you've been waiting for. You’ll establish a working knowledge of SAP CRM architecture, and then learn how to perform daily programming tasks in SAP CRM. Get up to speed with the most common enhancements to customize the backend for many different industry requirements.

Learn More

SAP PRESS is the world's leading SAP publisher, with books on ABAP, SAP S/4HANA, SAP CX, intelligent technologies, SAP Business Technology Platform, and more!