Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
BhaskarY
Participant
The blog is to explain a scenario where CPI will be receiving either a Text or a number in a particular source field from 3rd party system and to be mapped it to the Target SAP field. The challenge here is, if it is a text, it needs to map with the Text value as is to the Target field but if it is a number, need to append 0s before the number to make it 18 digits and then map it to the target.

An SAP CPI Consultant’s initial thought was to write the groovy to check if the input is a number and if it is false, will map the source value as is and if it is true, will use the formatNumber function to append the 0s to the number and map it to target.

Have written the below User defined function and done the mapping.

Code to check if the input is a number.
import com.sap.it.api.mapping.*;

def String checkNum(String input) {

return isNumber(input) ? "true" : "false";

}

def boolean isNumber(String value) {

try {

Double.parseDouble(value);

return true;

} catch (NumberFormatException e) {

return false;

}

}

 

When tested the above mapping with the number as input, it gives the desired target value.

But when the input is given as text for e.g. “TEST”, it will throw the mapping exception with the error 'Cannot cast “TEST” as decimal number.

The formatNumber will come into the picture and will not allow the string value to take forward.

To overcome this issue, it is required to handle the formatting of the number inside the User Defined function. The code will be as follows.

Code for checking if the input value is number or not and define the format:
import com.sap.it.api.mapping.*;

def String isNum(String input) {

if (isNumber(input)) {

String numberStr = input.toString();

return String.format("%018d", new BigInteger(numberStr));

} else {

return input;

}

}

def boolean isNumber(String value) {

try {

Double.parseDouble(value);

return true;

} catch (NumberFormatException e) {

return false;

}

}

You can now see the Text will get mapped as is and the number is getting formatted and mapped.

 

Hope you had a good read.

If you have any further queries on above code or if you have any other way to handle the above scenario, please feel free to drop comments below and share your feedback on the topic.

You can also post your questions on https://answers.sap.com/tags/67837800100800006801
Labels in this area