Skip to Content

SAP Design Studio 1.4 has a much needed feature that we were looking forward to – ‘Global Scripting’. Read on to find out what it is and how it has helped us, in a client scenario.

Creating Global Scripts

Global Scripting enables code reusability. Reusable scripts can be written in a ‘Global Scripts Object’. Each of these objects can have a number of script functions in them, which can be accessed using the object name – just like you would access the methods of a component. We can pass parameters to and get return values from these functions.

You can create a Global Scripts Object by right clicking on the ‘Technical Components’ folder in the Outline View, as shown.

/wp-content/uploads/2014/11/1_594801.png

/wp-content/uploads/2014/11/2_594802.png

/wp-content/uploads/2014/11/3_594845.png

/wp-content/uploads/2014/11/4_594846.png

Once the script function is created, you will be able to call the function from within other scripts in the application. The syntax is:

GLOBAL_SCRIPT_OBJECT.ScriptFunction(Parameters);

Our Business Scenario

/wp-content/uploads/2014/11/5_596367.png

As seen in the image above, we use KPI tiles in one of our dashboards for a client. KPIs such as Profit, Production Delay and Non-Defective Piece Count are calculated using scripts and displayed on the tiles.

To understand the duplication of code necessary without a global function, let me tell you what needs to be done here. In the OnApply() event of the dimension filter, we would have to get the measures from each data source, calculate the difference and set the value in the text box on the tile. This has to be repeated for every KPI tile. If there are additional dimension filters, then this whole set of code has to be written in each of the filters. What a huge overhead !

Here, comes the power of a global function that can just be written once and called as needed. This is just one typical business scenario – but using global scripts will make a big difference wherever a set of actions – like the above, or setting visibility, setting the css class etc – have to be repeated conditionally, across several components.

Please check out the code below for details of the implementation. This can be easily tweaked to accommodate change in data sources or addition of tiles.

/wp-content/uploads/2014/11/6_596368.png

The global scripts we defined were Difference_1 and SetText_1. Difference_1 calculates the difference between the appropriate values for the KPIs (for instance total sales – total cost for tile 1) and returns the value, in a string format.SetText_1 takes in a string and a text box alias and sets the text to the value taken in as parameter.


Shown below is the definition of ‘Difference_1’:

/wp-content/uploads/2014/11/7_596372.png

The second script, ‘SetText_1’ is much simpler as you can see:

/wp-content/uploads/2014/11/8_594859.png

Once we had the two scripts in place, we called the functions in the ‘onApply()’ event of the dimension filter:

//Defines the list of data sources

var DataSourceCollection = [DS_1,DS_2,DS_3];

//Defines the list of text components that need to be updated corresponding to each data source

var TextBoxCollection = [TEXT_KPI_1,TEXT_KPI_2,TEXT_KPI_3];

//Defines the list of first Measures for Difference

var FirstMeasureCollection = [“006EI4ZXZALE3K340F9IQKQ75”,“006EI4ZXZALE3K340F9IQK0WX”,“006EI4ZXZALE3K340F9IQKJVL”];

//Defines the list of Second Measures for Difference

var SecondMeasureCollection = [“006EI4ZXZALE3K340F9IQKDK1”,“006EI4ZXZALE3K340F9IQJO9T”,“006EI4ZXZALE3K340F9IQJULD”];

//Comparing array members to call the script using appropriate Data Source – Measures pair

  1. DataSourceCollection.forEach(function(ds_element, ds_index) {

TextBoxCollection.forEach(function(text_element, text_index) {

    if(text_index == ds_index)

    {

      FirstMeasureCollection.forEach(function(FMC_element, FMC_index) {

            SecondMeasureCollection.forEach(function(SMC_element, SMC_index) {

                  if(SMC_index == FMC_index && FMC_index == text_index)

                  {

                        //Global Script Function Call

                        GLOBAL_SCRIPTS_1.SetText_1(text_element,GLOBAL_SCRIPTS_1.Difference_1(ds_element, FMC_element, SMC_element));

                  }

            });

      });

    }

});

});



This works well, with the tiles updating themselves each time a different value is chosen in the filter.

/wp-content/uploads/2014/11/9_596374.png

Just as an additional note – we used arrays to define the different data sources, text boxes and measure pairs, for easier maintenance and scalability. If we had to add more KPI tiles that required the same functionality, we would just need to append:

  • Name of the Data Source used, to array “DataSourceCollection
  • Name of the Text Component to be updated, to array “TextBoxCollection
  • Name of the 1st Measure of the difference calculation, to array “FirstMeasureCalculation
  • Name of the 2nd Measure of the difference calculation, to array “SecondMeasureCalculation

Developers would only have to add the appropriate pairs as members to each of the above-mentioned arrays and they are good to go, without any change required in the script. Slight changes would be required when completely different queries are used as Data Sources.

So, in a nutshell, we wrote reusable functions to help achieve the same functionality with much fewer lines of code – which is the biggest advantage of global scripting. An extremely useful feature. Thank you, SAP!

(Major Edit on 11/28/2014: Script has been re-written for proper reusability)

Source: http://visualbi.com/blogs/design-studio/global-scripting-in-sap-design-studio-1-4/

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Audrey Koons

    Hi,

    I have a simple question. How do you find the ” [“006EI4ZXZALE3K340F9IQKQ75”,“006EI4ZXZALE3K340F9IQK0WX”,“006EI4ZXZALE3K340F9IQKJVL
    ” names for your measures?

    Thanks!!

    (0) 
    1. Karol Kalisz

      Hi Audrey,

      you can use value help (CTRL+SPACE) in some method which is requiring members. An example would be the method getData() on Data Source.

      Karol

      (0) 
  2. Jyoti Jain

    Great Blog Eshwar.

    I have one query , I have similar requirement , I have created List box filter for 3 dimensions (eg. A, B, C) and I have a  tile value (eg. sales count) that needs to be changed based on List box selection criteria,

    For some reason my tile value is not getting changed based on List box selection. can you pls suggest your valuable inputs

    (0) 

Leave a Reply