WEEKNIGHT in Formula Builder

The ability to conditionally execute a BW Process Chain with an awareness of being in a ‘Nightly Load Window’ should be easy and already available. It should be fundamentally centered around midnight, with a time interval of midday to midday.

Formula

We should be able to easily add a Decision Process Variant with a simple formula allowing multiple process branches to be executed based upon ‘What night of the week is it?’.

“unfortunately there is no WEEKNIGHT function
available to align with a days nightly load window”

The list of available functions in the Formula Builder should look like this:

BW Formula Builder C_TIME_WEEKNIGHT1 Function

Implement this interface method to enable access to a new C_TIME_WEEKNIGHT1 function in the BW Formula Builder.

Method IF_EX_RSAR_CONNECTOR~GET

method IF_EX_RSAR_CONNECTOR~GET.
*--------------------------------------------------------------------*
* Code Name:   zlbw-cl-fb-c-time-weeknight1-if-ex-rsar-connector-get *
* Code URI:    http://lnxbw.co/dwym-cod-1                            *
* Description: Enable access to the 'Week Night (C_TIME_WEEKNIGHT1)' *
*              function in the SAP BW Formula Builder engine.        *
* Version:     1.0.0                                                 *
* Author:      John Lang                                             *
* Author URI:  https://www.johnlang.com.au/                          *
* License:     BSD-New                                               *
* License URI: http://opensource.org/licenses/BSD-3-Clause           *
*--------------------------------------------------------------------*
* Copyright (c) 2015, John Lang. All rights reserved.                *
*                                                                    *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met:                                                           *
*                                                                    *
* 1. Redistributions of source code must retain the above copyright  *
* notice, this list of conditions and the following disclaimer.      *
*                                                                    *
* 2. Redistributions in binary form must reproduce the above         *
* copyright notice, this list of conditions and the following        *
* disclaimer in the documentation and/or other materials provided    *
* with the distribution.                                             *
*                                                                    *
* 3. Neither the name of the copyright holder nor the names of its   *
* contributors may be used to endorse or promote products derived    *
* from this software without specific prior written permission.      *
*                                                                    *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS *
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,           *
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED    *
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,      *
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON  *
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,    *
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
* SUCH DAMAGE.                                                       *
*--------------------------------------------------------------------*

* This is part of a Classic BAdI implementation (Transaction SE19).

  constants:
    c_cat_tech type sfbefsym    value 'C_TIME1',
    c_cat_desc type c length 65 value 'Time Functions (Custom)',
    c_fun_tech type sfbefsym    value 'C_TIME_WEEKNIGHT1',
    c_fun_desc type c length 65 value 'Week Night (DATE_WEEKDAY1 + 12hrs)',
    c_fun_clas type seoclsname  value 'ZCL_FB_C_TIME_WEEKNIGHT1',
    c_fun_meth type seocmpname  value 'C_TIME_WEEKNIGHT1'.

  data: wa_formula type sfbeoprnd.

* Are we building the list of categories?
  if i_key = space.
 
*   Is the category already in the category list?
    read table c_operands transporting no fields
      with key tech_name = c_cat_tech.
    if sy-subrc <> 0.
 
*     No ... Add the category to the category list.
      clear: wa_formula.
      wa_formula-tech_name = c_cat_tech.
      wa_formula-descriptn = c_cat_desc.
      append wa_formula to c_operands.

  endif.
 
* Are we building the list of functions in the category?
  elseif i_key = c_cat_tech.
 
*   Is the field already in the category?
    read table c_operands transporting no fields
      with key tech_name = c_fun_tech.
    if sy-subrc <> 0.
 
*     No ... Add the function to the category.
      clear: wa_formula.
      wa_formula-tech_name = c_fun_tech.
      wa_formula-descriptn = c_fun_desc.
      wa_formula-class     = c_fun_clas.
      wa_formula-method    = c_fun_meth.
      append wa_formula to c_operands.
 
    endif.
 
  endif.
 
endmethod.

Method C_TIME_WEEKNIGHT1

method C_TIME_WEEKNIGHT1.
*--------------------------------------------------------------------*
* Code Name:   zlbw-cl-fb-c-time-weeknight1-c-time-weeknight1        *
* Code URI:    http://lnxbw.co/dwym-cod-1                            *
* Description: Execute the 'Week Night (C_TIME_WEEKNIGHT1)' function *
*              in the SAP BW Formula Builder engine.                 *
* Version:     1.0.0                                                 *
* Author:      John Lang                                             *
* Author URI:  https://www.johnlang.com.au/                          *
* License:     BSD-New                                               *
* License URI: http://opensource.org/licenses/BSD-3-Clause           *
*--------------------------------------------------------------------*
* Copyright (c) 2015, John Lang. All rights reserved.                *
*                                                                    *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met:                                                           *
*                                                                    *
* 1. Redistributions of source code must retain the above copyright  *
* notice, this list of conditions and the following disclaimer.      *
*                                                                    *
* 2. Redistributions in binary form must reproduce the above         *
* copyright notice, this list of conditions and the following        *
* disclaimer in the documentation and/or other materials provided    *
* with the distribution.                                             *
*                                                                    *
* 3. Neither the name of the copyright holder nor the names of its   *
* contributors may be used to endorse or promote products derived    *
* from this software without specific prior written permission.      *
*                                                                    *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS *
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,           *
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED    *
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,      *
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON  *
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,    *
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
* SUCH DAMAGE.                                                       *
*--------------------------------------------------------------------*

* This is part of a Classic BAdI implementation (Transaction SE19).

constants:
    c_time_9am    type systtimlo value '090000',
    c_time_midday type systtimlo value '120000',
    c_time_3pm    type systtimlo value '150000',
    c_time_6pm    type systtimlo value '180000'.

data:
  l_day  type p,
  l_time type systtimlo.

* What is the 'Nightly Load Window' time alignment within a day?
  "MANUAL MAINTENANCE: Assign the Nightly Load Window time.
  "l_time = c_time_9am.
  l_time = c_time_midday.
  "l_time = c_time_3pm.
  "l_time = c_time_6pm.

* What day of the week is it?
* Day 1 is Mon afternoon and Tue morning.
* Day 2 is Tue afternoon and Wed morning.
* Day 3 is Wed afternoon and Thu morning.
* Day 4 is Thu afternoon and Fri morning.
* Day 5 is Fri afternoon and Sat morning.
* Day 6 is Sat afternoon and Sun morning.
* Day 7 is Sun afternoon and Mon morning.
  l_day = i_date mod 7.
  if l_day > 1.
    l_day = l_day - 1.
  else.
    l_day = l_day + 6.
  endif.
  e_weeknight1 = l_day.

* Align to a specific time within a day (Not midnight).
  if i_time < l_time.
    e_weeknight1 = e_weeknight1 - 1.
    if e_weeknight1 < 1.
      e_weeknight1 = 7.
    endif.
  endif.

endmethod.

Implemented in 4 easy steps:

  1. Ensure you have access to the SYST-UZEIT system field in the Formula Builder;
    Further Reading: SYST-UZEIT in Formula Builder.
  2. Save the WEEKNIGHT1 code snippets;
  3. Create an ABAP Class for the IF_EX_RSAR_CONNECTOR interface;
    Further Reading: Transformation Library and Formula Builder.
  4. Create Business Add-In (BAdI) to call the new class interface.
    Further Reading: BAdI for Customer-Defined Functions in the Formula Builder.

How did it go? Leave a comment below.