In any ERP system such as SAP which interacts with users and other integrated systems need certain checks to validate the data flowing to the system. The validations could be master data validation, data type validation or other custom validations. Numeric check falls under the category data type validation. Applications can use numeric or number validation to filter out garbage data or invalid data inputted into the system.
Use Cases of ABAP Numeric Check
We need to implement data cleansing logic and validations such as ABAP numeric check when,
Users input data manually to SAP applications.
File-based upload programs used to import data.
Inbound interfaces transfer legacy system data information to ABAP programs, BAPIs, and iDocs.
In these data upload or transmission procedures, we need to validate the input data values. Numeric data fields should not contain other data values such as alphanumeric, boolean or string values. If the correct data type is not passed to SAP applications, it could result in inconsistencies in the application behavior or even short dumps in the SAP system.
Use case 1 of Numeric Validation
For example, let’s assume we have an inbound proxy interface that calls a BAPI that creates invoices in SAP. The invoice amount should be a numeric or decimal value in the correct format. If the ABAP processing logic does not validate the input data of the amount field of the inbound proxy, proxy will pass an incorrect value to the amount field of the BAPI. Hence, the interface processing will end up with a short dump.
Use case 2 of Numeric Check
If you have an ABAP program to create sales orders in SAP via an Excel or file-based upload program, you must validate the sales order quantity values transferred from Excel file before calling the sales order creation BDC or BAPI. quantity of the sales order cannot be alphanumeric, and it should be an integer value.
Therefore, the first stage of the upload program should be to validate the data elements of the Excel file. ABAP upload program can use either the application log or the output of the ABAP program to log the invalid data values identified by the validation.
Different Number Formats
Globally, we use different number formats based on the region. While North American countries use comma (,) as the thousand separators, Norway uses the decimal point (.) as the thousand separators.
For example, the value represented as 5.321.978.693,000 in Norway is essentially the same as 5,321,978,693.00 in the United States.
Both of these values represent the same numeric value. Hence, it is important to understand numeric check validation should also take into consideration the number format of the input.
Different Methods to Perform Numeric Validation in SAP
In SAP we have a couple of methods to perform the numeric data validation. As we discussed before, the data validation method you pick should match the requirement you have.
Is it a simple integer check? Or Should the ABAP logic also validate the number format of the input data?
The numeric check in SAP can be performed using,
Standard Function Module NUMERIC_CHECK or,
A custom ABAP logic using keywords CN or CO
Standard Function Module NUMERIC_CHECK
Function Module (FM) validates if the input is an integer value or not. You can only validate whole numbers using this function. Hence, using this function module you cannot validate numbers in different formats with decimal values or thousand separators. This is one of the disadvantages of NUMERIC_CHECK function module.
FM accepts values through input parameter STRING_IN and returns the data type of the input using export parameter HTYPE. Data type output can be either CHAR or NUMC which are self-explanatory.
The behavior of NUMERIC_CHECK FM
Let’s look at some examples of how the function modules behave for different inputs such as integers, decimals, thousand separated values, alphanumeric values, etc.
Integer or Number Values
Integer values are correctly identified as numbers by the FM.
Decimal inputs are still identified as CHAR.
Thousand Separated Values
Thousand separated values also cannot be identified as numeric values using the FM NUMERIC_CHECK.
String and Alphanumeric Values
Alphanumeric or string values can be validated using the NUMERIC_CHECK FM.
Based on the above analysis of the behavior of FM NUMERIC_CHECK, we can conclude that the usability of the FM for numeric validation is limited to only certain cases. However, NUMERIC_CHECK FM can certainly identify if the value contains only numbers or not. Also, you can do a negative validation using the FM.
Hence, in most of the custom ABAP programs, you might need to build your own logic to validate numeric values.
ABAP Keyword CN and CO
Keywords CO and CN come in really handy when it comes to validating input data.
CO – Contains Only
Syntax of CO: <Parameter 1>CO<Parameter 2>
Keyword CO (Contains Only) compares two values and output Boolean values true or false. If the first parameter only contains characters from the second parameter, return output would be true. The function CO takes case-sensitivity of the parameters when comparing them.
The behavior of this ABAP program is similar to the program I have built using keyword Contains Only (CO)
ABAP Program to Validate Numbers and Decimal Values
I have developed this ABAP program to validate numeric and decimal values without a thousand separator. The first step of the validation is to check if there are repeating decimal points in the input value. Then using ABAP keyword CO, I have validated if the input only contains numeric values.
ABAP Numeric Check for Numbers and Decimal Values
*Perform numeric check function
*Check if input contains more than one decimal character
Hi, I am Isuru Fernando, Senior SAP Integration Consultant with 10 years of SAP full-cycle implementation and support project experience.
From the early days, I had a passion for coding, software development, and everything tech-related. I started my carrier as an ABAP developer and soon found my love for system integration when I learned SAP XI 3.0 in 2008. Playing a variety of roles from an offshore technical consultant (ABAP, PI/PO, BW, BOBJ) at the beginning of my career to a technical lead managing a team of consultants in different countries, I have gained immense experience in SAP project implementation life cycle.
Having the opportunity to work on SAP implementation projects in USA, EU, and Asia, I learned valuable ins and outs of global business processors in Sales and Distribution (SD), Material Management, Retail, Customer Relationship Management (CRM), and Finance and Controlling (FICO).
Through this blog, I want to share my expertise in SAP technical areas such as SAP ABAP, PI/PO, AIF, and Basis. I also want to provide a platform for others with similar ambitions who would like to share their SAP technical expertise with the world!