S4HCKS43 – Generating a check digit on your invoice

A check digit to secure BPay payments

It may sometimes be necessary to add information to a document that is made up of other pieces of information (concatenate fields, truncate fields, transform fields, etc…) – that is, information that does not readily exist in your system (like a customer number for example), but can be generated or deduced.

In this snippet we look at how you can add some custom logic in your SAP S/4HANA Cloud system to generate a check digit to a BPay CRN (Customer Reference Number). That is, we want to offer our customer the BPay payment method and for that we have to add to the invoice a CRN number, that will be made of the billing document number appended by a check digit that we will generate on the fly in S/4.

The custom logic to generate the check digit

You will below find the custom logic that I used to generate this check digit (Disclaimer : my code is probably dirty – but it works). I am not checking the length of the billing document number – I have made the assumption that the billing number will always be 8 digits long.

I have also added a screenshot that shows how my custom ‘CheckDigit’ field was defined (Numerical – 1 digit).

* Logic to determine a check digit for a BPay 8 digit CRN number

DATA:
Counter       TYPE i,
lv_billdigit  TYPE i,
lv_total      TYPE i,
lv_billingdoc TYPE string,
lv_chardigit  TYPE string.

Counter = 0.
lv_billingdoc = billingdoc-billingdocument.
SHIFT lv_billingdoc LEFT DELETING LEADING '0'.

While Counter < 8.

   IF ( Counter MOD 2 = 0 ).
      lv_billdigit = lv_billingdoc+Counter(1) * 1 .
   ELSE.
      lv_billdigit = lv_billingdoc+Counter(1) * 2 .
   ENDIF.


   IF ( lv_billdigit > 9 ).
      lv_chardigit = lv_billdigit.
      lv_billdigit = ( lv_chardigit+0(1) ) + ( lv_chardigit+1(1) ).
   ENDIF.


   lv_total = lv_total + lv_billdigit.
   Counter = Counter + 1.
ENDWHILE.


IF ( lv_total MOD 10 = 0 ).
   billingdoc_extension_out-yy1_checkdigit_bdh = 0.
ELSE.
    billingdoc_extension_out-yy1_checkdigit_bdh = 10 - ( lv_total MOD 10 ).
ENDIF.

Video: Generate a - BPay - check digit on your invoices

BPay check tool for Excel

In case you are interested to have it, I am also adding the Excel file that I used to double check the check digit generation. The CRN numbers in the file and check digits were generated on the BPay generator web page. You can download the excel by clicking the icon below.