Validating a tax ID format requires you to implement the validation logic using a business add-in (BAdI), create a repository of messages, and test the implementation. For example, if you want to validate the format of the tax identification number (TIN) for a country/region and display an error message in case of an invalid format, you can do so by implementing a cloud BAdI to validate the format and by creating a repository to store custom error messages.
The goal in our example is to validate a VAT number for country code XX. The VAT number should start with a letter (C = company or I=individual) followed by 11 digits. The first six digits should be the same as the taxpayer’s income tax number.
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.
To successfully accomplish our objective, we must undertake the following essential steps:
- Creating a Repository to Store Custom Messages: Create a repository to store messages that can be displayed to the user if the specified tax ID is not in the required format.
- Implementing Validation Logic Using BAdI: Create a validation logic using business add-ins to ensure that the tax ID specified by users adheres to the required format.
Creating a Repository to Store Custom Messages
When the Tax ID undergoes validation and an error is detected, an error message will be displayed to inform the business user about the issue with the ID.
For generating these error messages, we will utilize a custom code list with tailored text strings. This approach is designed to clearly communicate to the user what specific element is missing or incorrect.
The following exercise provides a step-by-step guide on how to create your own custom code list effectively.
Implementing Validation Logic Using BAdI
We can now continue with creating a validation logic using business add-ins to make sure that the tax ID specified by users conforms to the required format.
To implement the code logic, your user needs to be assigned with the role Extensibility Specialist (BR_EXTENSIBILITY_SPEC) and Configuration Expert (BP_BPC_EXPERT).
This exercise will lead you through the necessary steps to implement the validation logic. Following the exercise, you'll find the example implementation code that is utilized within the exercise for reference.
Here you can find the sample implementation code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
DATA lsbp LIKE LINE OF it_bp_root.
DATA ls_message LIKE LINE OF ct_validationmessage.
DATA ls_tax_num LIKE LINE OF it_bp_tax_num.
DATA lv_digits TYPE string VALUE '0123456789'.
DATA lv_message TYPE string.
DATA: lv_len_tax TYPE i, lv_tax_digits TYPE string.
LOOP AT it_bp_tax_num INTO ls_tax_num.
*TAX Number validation for Country 'XX'
*where YYY is the tax type
IF ls_tax_num-bptaxtype = 'YYY' OR ls_tax_num-bptaxtypeforedit = 'YYY'.
IF ls_tax_num-bptaxnumber(1) <> 'C' AND ls_tax_num-bptaxnumber(1) <> 'I'.
*You need to create the repository of error messages
CLEAR lv_message.
SELECT SINGLE FROM yy1_message_XX 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_XX 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_XX 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_XX 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_XX 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.