How to use Global Variable in SAP PI Mapping
What is Global Variable?
The variable which is accessible to whole program i.e. having global scope throughout a program is called as Global Variable. Here in PI we are actually referencing a definition w.r.t to JAVA as the base of our SAP PI graphical mapping is JAVA only.
What is the need of Global Variables in SAP PI mapping?
There are the cases where we need to pass\share some information or values in PI mapping among all the UDFs. Suppose for e.g. we have multiple UDFs in one graphical mapping which executes in one sequence. One UDF sets some parameters or variables based on any conditions that will be used by another UDF for sending alerts or for some internal validation in PI. These kinds of requirements can be solved by using Global Variables whose scope will be throughout the mapping or all mappings (the Information of deciding scope will be shared below)
How to define GLOBAL VARIABLE in Sap PI Mapping?
There are two ways to define a global variable in SAP PI Mapping.
Method 1: In the Mapping itself (as shown in below screenshot)
We have one ICON in the Message Mapping (Edit Java Sections) which allows us to declare Global Variables which are having scope to the same message mapping in which it is defined. All the UDFs which are created in that mapping can use those variables.
Now when you click on the ICON you will be getting a window where you can declare global variables and can initialize with a default value (as shown below)
Three variables are defined and are initialized as you see in the screenshot. Now these variuables can be used in UDFs.
Set it in one UDF like and use it in aother as
Method 2:
In second options we will be creating a JAVA Mapping which will be imported in SWCV as Imported Archives and this allows us to use a Global Variable throughout all the mappings defined in the SWCV. So in this case the scope of our variable is increased to mappings not limited to UDFs.
The only thing we need to take care of that we need to initialize the global variables everytime when you are using it in UDF otherwise it will read the old values set by any other mapping or UDF. Also you need to import class name in import section of UDF and use variable as <ClassName>.<VariableName>
The Code is:
In Imported Achieves it will be like:
In Import Section of UDF, import your class like
Nice doc, with all relavent information in one page 🙂
Thanks Ashutosh 🙂
Well documented. Learnt a new thing today. 🙂
Gud one.. Nice blog..:)
Nicely documented Ashutosh.
Very good
Thanks Ram 🙂
HI Ashu
I try to implement this by using second method.For which i have imported a archived file into ESR then add package name to import. the whole scenario is implement without any error.but at run time i m getting Exception NoClassDefFoundError.As Archived class in not available to Function Library class. Please help me to resolve it.
Thanks 🙂
Hi,
I have a sync scenario where I need use a value from request in my response message mapping. I need to use this value to filter out the response (source message) and map it to response(target). Can you guys guide me on how to achieve this?
Thanks
Hi,
You can try the links below for reference to solve your purpose.
https://blogs.sap.com/2015/08/28/copy-value-from-request-message-to-response-message-using-dynamicconfigurationbean-and-dynamic-header-fields/
https://blogs.sap.com/2010/03/04/usage-of-dynamic-configuration-in-synchronous-scenarios/
Hope it will Help.
Regard
I tried the first scenario through the dedicated button, but I have no such button on my message mappings as on the screenshots above. Is it the limitation of my version?
I have SAP PO 7.40 SP08 Java Single Stack