Skip to Content
Technical Articles

Validate a Tax Number

This blog post describes a sample business scenario for validating tax numbers and how to do such an extension on the SAP S/4HANA Cloud.

Note: Information provided in this document is as per features available in SAP S/4HANA Cloud release 1905. There might be variations to screen and field names in other release versions.

Prerequisites

You have administrative access to SAP S/4HANA Cloud and have implementation experience on the system. Coding experience is also necessary, since this extensibility solution requires implementation of a coding logic.

Authorizations

Business Role Business Role ID
Configuration Expert – Business Network Integration SAP _ BR _ CONF _ EXPERT _ BUS _ NET _ INT
Master Data Specialist – Business Partner Data MASTER _ SPECIALIST

Implementation

For example, you want to validate the format of the tax identification number (TIN) for a country and display an error message in case of an invalid format. You can do so by implementing a cloud BAdI to validate the tax number.  You will also have to create repository to store custom error messages.

Implement the validation Logic

  1. Create a New Enhancement Implementation using the Custom Logic option in the Custom Fields and Logic app. 
  2. Write the implementation logic for tax validation in the Draft Logic area and publish the New Enhancement Implementation.

Note: Enter a solution specific to the purpose of this implementation in the code logic. This is necessary because the BAdI does not have an option to set a filter.  

Sample Code
Country XXX
Requirement

To validate a VAT Number for country ‘XXX’

The VAT number should start with an Alphabet (C = company or I=individual ) followed by 11 digits. The first six digits should be the same as the taxpayer’s income tax number.

Cloud BAdI Implement the BAdI under the Business Partner Core View ->Validate BP BAdI
Result When a user enters the tax number, the system checks for the correct format and displays an error message in case of an invalid format.

Sample Code

DATA lsbp LIKE LINE OF itbproot. 

DATA lsmessage LIKE LINE OF ct_validationmessage.
DATA: lstaxnum LIKE LINE OF itbptaxnum, lvlentax TYPE i, lvtaxdigits TYPE string, lvdigits TYPE string VALUE '0123456789',
lvtaxna7 TYPE string,
lvnaformaterr TYPE c, lvmessage TYPE string.

LOOP AT itbptaxnum INTO lstax_num.

*TAX Number validation for Country 'XXX'where YYY is the tax type 
  IF lstaxnum-bptaxtype = 'YYY' OR lstaxnum-bptaxtypeforedit = 'YYY'. 
    IF lstaxnum-bptaxnumber(1) <> 'C' AND lstaxnum-bptaxnumber(1) <> 'I'.
*You need to create the repository of error messages 
    CLEAR lvmessage. 
    SELECT SINGLE FROM yy1messageclassbw FIELDS description WHERE code = '001' INTO @lv_message.

      IF sy-subrc = 0.
        ls_message-msgv1 = lv_message.
      ELSE.
        ls_message-msgv1 = 'The Tax Number must start with "C" or "I"'.
      ENDIF.
      ls_message-msgty = 'E'.

      APPEND ls_message TO ct_validationmessage.
                
    ELSE.
      lv_len_tax = STRLEN( ls_tax_num-bptaxnumber ).
      IF lv_len_tax <> 12.
        CLEAR lv_message.
        SELECT SINGLE FROM yy1_message_class_bw FIELDS description WHERE code = '002' INTO @lv_message.
        IF sy-subrc = 0.
          ls_message-msgv1 = lv_message.
        ELSE.
          ls_message-msgv1 = 'The length of the Tax Number must be 12. '.
        ENDIF.
        CLEAR lv_message.
        SELECT SINGLE FROM yy1_message_class_bw FIELDS description WHERE code = '003' INTO @lv_message.
        IF sy-subrc = 0.
          ls_message-msgv2 = lv_message.
        ELSE.
          ls_message-msgv2 = 'It must start with "C" or "I" + 11 digits'.
        ENDIF.         
        ls_message-msgty = 'E'.
        APPEND ls_message TO ct_validationmessage.
      ELSE.
        lv_tax_digits = ls_tax_num-bptaxnumber+1.
        IF NOT lv_tax_digits CA lv_digits.
          CLEAR lv_message.
          SELECT SINGLE FROM yy1_message_class_bw FIELDS description WHERE code = '001' INTO @lv_message.
                
          IF sy-subrc = 0.
            ls_message-msgv1 = lv_message.
          ELSE.
            ls_message-msgv1 = 'The Tax Number must start with "C" or "I" '.
          ENDIF.
                
          CLEAR lv_message.
          SELECT SINGLE FROM yy1_message_class_bw FIELDS description WHERE code = '004' INTO @lv_message.
                
          IF sy-subrc = 0.
            ls_message-msgv2 = lv_message.
          ELSE.
            ls_message-msgv2 = 'and followed by 11 digits'.
          ENDIF.
                
          ls_message-msgty = 'E'.
                
          APPEND ls_message TO ct_validationmessage.
        ENDIF.
      ENDIF.
                
    ENDIF.          
  ENDIF.

ENDLOOP.

Create a Repository to Store Custom Messages

  1. Create a repository for customer messages by creating a Code List using the Custom Reusable Elements app.
  2. Specify the translation language and enter the translated text for the custom messages. Do this step only if translation is required.

Since system does not translate the message, you manually enter the translated text for the message in the language, in which, the system must display the message. For example, if the translated text is to be in German, enter the translated message in German. 

Test the BAdI Implementation

  1. Open Customer List in the edit mode on the Custom Master Data app, and select a customer.
  2. Add a new tax number and save it. If the new tax number is in an incorrect format, the system will display the error message.

When an end user enters a tax number, the system will check for the format relevant for the country. If the format is incorrect, it will give the user an error message.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.