How To Read Variable Values in WAD 7.0 Using JavaScript
How To Read Variable Values in WAD 7.0 Using JavaScript
Applies to:
SAP NetWeaver 7.0
Summary
There are many occasions where we need to evaluate the variable entry made by the users or fire actions
based on the value input by the users which may not be completely possible within the scope of the SAP
functionalities directly. In that case we can make use of the JavaScript functionalities in WAD to tweak the
design to get what we need. In this How to paper, I have given a basic example of how we can read the
variable value Input by the user and fire WAD commands accordingly. The code can be customized based
on various business needs.
Author:
Author Bio
Shafi is a SAP BI Consultant working in Infosys Technologies Limited currently with around 3 years of
experience. He has extensively worked in SAP BI Developments Projects. His core skills include modelling,
reporting and planning using SAPBI 3.5, SAPBI 7.0 and SAP BI Integrated Planning.
Table of Contents
Business Scenario .............................................................................................................................................. 3
Background Info: ................................................................................................................................................. 3
Step By Step Approach: ..................................................................................................................................... 5
Appendix ........................................................................................................................................................... 13
Disclaimer and Liability Notice .......................................................................................................................... 14
Business Scenario
We have many real-time situations where we need to validate the User input, into a variable and fire
respective actions based on the same in a Web report as follows.
In all this cases, there are two parts associated with it.
Both the above two steps are not supported directly by WAD commands.
In order to make this work we seek the help of JavaScript.
Background Info:
We have the option of adding a JavaScript to the WAD layout using the scrip item. As we all know, all the
Web pages rendered via Web in SAP BI has a background Html / XHTML source code and by writing
customized JavaScript we can navigate retrieve or write back values into them.
In our example we need the details of the variables present in the data provider in the WAD layout.
For this we can use the XML item in the layout, which on execution displays the parameters and data of the
Data provider as XML codes which will not be visible to the user directly.
Based on the XML code we can use JavaScript to navigate and read the values we need from the data
provider.
Also the Script item in WAD allows us to generate automated JavaScript for WAD commands thorough
wizard, which we can change as per requirement and call in our custom JavaScript code.
Combining both we have the option to fulfill the requirements stated above with a few degree of
customization based on every requirement.
In our example we have the same two steps explained above.
1. Reading Variable values
This common JavaScript code with variable name will read the variable values.
Based on the values input by the user, we redirect the user to a different layout.
In this article we can see how this is achieved using the following objects.
1. WAD Layouts
Main Layout
i. XML item
ii. Scrip Item
iii. Text Item
iv. Data provider
Sub Layout 1
i. Text item
Sub Layout 2
i. Text item
2. Query
c.
Select the text property of the text item and then input the value as Welcome to Template
1
d. Similarly create another Template called as X_VAR_2 with a text item and with value
Welcome to Template 2.
2. Creation of Main Layout
a. Create a new Template and add the following items in it.
i. XML item
ii. Script Item
iii. Text Item
b. This is the main layout in which we are going to write the redirection JavaScript based on
user variable entry.
c.
Create a Data provider and assign a Query that has the User Entry variable that will be used
for redirection. For this Example I have a query with a single Input enabled text variable
XTESTINP.
f.
From the Pop up menu select the All Commands tab and locate the SET_TEMPLATE
command under Commands for Web Templates.
g.
From the Pop up Menu select the Web template to be redirected (in our case it is
X_VAR_1).
h.
i.
Perform similar steps for creating a JavaScript to redirect to second template X_VAR_2.
Please note that the JavaScript created again will have the same name
executeJS_SET_TEMPLATE_R. Just to make a difference between the two, rename it as
executeJS_SET_TEMPLATE_R2.
j.
Now scroll to the end of the code window and copy paste the custom code in the appendix
section.
k.
Now Click on the Text item to give a value to it as Redirecting .... Please Wait. This is to
inform the user that they arer being redirected until the sub template opens up.
l.
m. Now say Ok and click on the XHTML tab in the main layout.
n. Add the JavaScript code to invoke the redirection script on the load of the main layout as
shown.
3. Execution
a. Now run the main template and from the selection screen input the variable value as INP1
and say OK.
b.
c.
When you refresh the main Layout and change the Input as INP2 you can see teh Second
template.
d. This way based on the variable value we can fire corresponding WAD commands. And in
this example we have used SET_TEMPLATE for redirection.
4. Code Explanation
a. The XML Web item generates the XML version of the parameters of the data provider and its
data in the backend invisible to the users.
b. This can be seen, by right clicking on the browser window and selecting view Source once
the main layout is loaded without the JavaScript code added to its XHTML tab.
c.
We use standard JavaScript code to navigate between different hierarchy of XML tags
generated and read our respective variable name and values.
Appendix
function redirect_wad()
{
var varnm = "XTESTINP";
// Name of the variable to be read
root = document.childNodes[0];
// <HTML>
body = root.getElementsByTagName("BODY")[0];
// <BODY>
xml = body.getElementsByTagName("XML")[0];
// <XML>
bics = xml.childNodes[0];
// <BICS_VIEW>
vars = bics.getElementsByTagName("VARIABLES")[0]; // <VARIABLES>
varl = vars.getElementsByTagName("VARIABLE");
// gives no. of variable in DP
for ( i = 0; i <= varl.length; i++ )
// Loop one by one
{
vari = vars.getElementsByTagName("VARIABLE")[i];
varnam = vari.attributes[0].text;
// get variable name
mem = vari.getElementsByTagName("MEMBER")[0];
varval = mem.attributes.getNamedItem("name").value; // read variable value
if (varnam == varnm)
//check varname in loop needs to be
read
{
if (varval == "INP1")
// check for value1 and fire command1
{
executeJS_SET_TEMPLATE_R();
}
if (varval == "INP2")
{
executeJS_SET_TEMPLATE_R2();
}
break;
}
}
}