How to Create User-Defined Function for Message Mapping Using NWDS
With the release of PI 7.31, the NWDS tool is becoming robust enough to replace the Swing tools used in the Enterprise Service Repository (ESR) and the Integration Directory (ID). One of the main focuses in PI, with each new SP release, is on improving the NWDS tools.
In developing an integration scenario, we may spend as much as 80-90% of our time in the development of mapping/transformation. The Swing tool provides us with Message Mapping, which also includes a graphical tool for us to create user-defined function (UDF). UDF is very important to the development of mapping programs. It is impossible to expect the default Message Mapping tool to meet 100% of our mapping requirements. As a result, UDF is there to help us fill the gaps of the special mapping needs which cannot be met with the default tool.
The ability to create UDF is available with the NWDS tool beginning in PI 7.31 SP4. But, there has been a lack of documentation of how to create an UDF. So, in this blog I will provide the steps on how to create UDF in NWDS. In addition, I will use the GUI mapping tool in NWDS to demonstrate it. This GUI mapping tool is going to be available in PI 7.31 SP6, which is scheduled for release in the beginning of 2013.
With NWDS, UDF must be created in a Function Library. In our mapping program we can specify the function library(s) we want to use. Then, the UDFs created in the Function Library will be available to us during mapping.
One big advantage of using NWDS to create UDF is the availability of the Java development environment, e.g. syntax checking, auto-display of methods in a class, etc. I think these features have been frequently requested for the Swing tool.
In this blog, I am assuming the reader is already familiar with using NWDS with ESR. If not, please reference the blog: Eclipse Tool for ESR in NW PI
To use as an example for this blog, I am going to write a simple UDF to extract the nth element of a comma-delimited string. For example, for the string “element1,element2, element3”, the 1st element is “element1”, the 2nd is “element2”, the 3rd element is “element3”.
Steps to create and use an UDF:
- Create a function library.
- Create UDF in the function library.
- In the mapping design, assign function library to the mapping.
- Design the mapping using the UDF.
Details of the steps:
- Create a function library: (or select an existing one)
- Create UDF in the function library:
- Double-click on the function library name or right-click on the function library name and select: “Open With” –> “Function Library Editor”
- Click on “Edit” in the Function Library Overview:
- Create a new method in the class. In this step, we have to create the signature for the method. In the Swing tool, this is done for us automatically. But, in java, we have to do this manually. It is possible, in the future, this will also be done automatically.
The code for the method is below. Please note:
(you will need to make the appropriate changes to your own UDF based on the above information)
You may need to resolve the undefined references by importing the following:
import com.sap.ide.esr.tools.mapping.core.Argument;
import com.sap.ide.esr.tools.mapping.core.ExecutionType;
import com.sap.ide.esr.tools.mapping.core.LibraryMethod;Java code for the method “getElement”:
@LibraryMethod(title=”getElement“, description=””, category=”MyUDF“, type=ExecutionType.SINGLE_VALUE)
public String getElement (
@Argument(title=””) String num,
@Argument(title=””) String source,
Container container) throws StreamTransformationException{
String delimiter = “,”;
int n = Integer.valueOf(num).intValue();
String[] temp;
temp = source.split(delimiter);
if (n > temp.length)
return “ERROR”;
return temp[n-1];
}Sample screenshot:
Save and close the “MyUDF.java” tab.- the name of the method is “getElement”
- there are 2 parameters: “num” and “source”
- the function library name is: MyUDF
- In the MyUDF Function Library Overview, you should see the new method:
- Double-click on the function library name or right-click on the function library name and select: “Open With” –> “Function Library Editor”
- Create a Message Mapping and assign the UDF to the Message Mapping:
- Use the UDF in Message Mapping:
Click on the “Definition” tab and use the GUI mapping tool to develop the mapping and use the UDF. This tool is virtually identical to the one in the Swing tool.(Please note on the right-hand side, the “MyUDF” with the method “getElement” is now available for mapping.)
The constant for fld_1 = 1 (to get the 1st element)
The constant for fld_2 = 2 (to get the 2nd element) - Test the mapping. Please reference the blog below on how to test Message Mapping:
How to Test Message Mapping Using the Eclipse Tool (NWDS)
From my perspective, I still have the following questions. Once I find the answers, I will update this blog:
- How to create an UDF without having to manually enter the reference and signature for the method, as described in step 2.c? (Will there be a “create” like in the Swing tool?)
- Since we are in the Java development environment, will there be a way for me to test the UDF by itself?
It does look a lot easier to develop the user defined functions, and will make it much easier to just use shared functions.
Thanks Bill for sharing..
I hope presently there is no facility to call standard functions from UDFs. This comes handy typically when u have to deal with string transformations like concatenations etc.
With such new environments and developments in this area, are you looking to give such option?
Hi,
Not sure what you meant. The standard functions are available in Message Mapping, e.g. text functions, node functions, etc.
Also, using UDF, you can use any Java classes, e.g. String, which can be used during your development.
Regards,
Bill
Is this supported for PI 7.31 SP03 dual stack?
We are unable to create function libraries or UDFs.
It is supported in PI 7.31 SP4. I will change the blog the specify the minimum SP level.
Thanks for noting this.
Regards,
Bill
in point # 4, i can see Expressions Under the properties window.
I am on SP04 but dont seem to be able to see that. Any help?
Hi,
If you are referring to the GUI mapping, this feature will be available with 7.31 SP6. With SP4, you have to use XPath expressions.
Regards,
Bill
thanks bill
Nice Blog.
Nice blog. thanks bill