Tuesday 20 June 2017

SAP Screen Personas for Web Dynpro ABAP – Cross Session Scripting

Introduction


With SAP Screen Personas there is now the option to add client-side scripting to existing Webdynpro for ABAP applications. For a first introduction to scripting please have a look at the Overview Blog of Matthias Heitmann.

With scripting it’s now possible to automate re-occurring or complex tasks. However there is a technical restriction: The script is always tied to the (technical) application. There is no possibility to define a script, which runs across different applications or even different browser windows. This heavily limits the applicability of scripting for automation.

Even worse is the fact, that there exists a lot of applications which are running in 2 windows – technically these are 2 instances of the same application. Such applications can’t be automated. Examples are basically all FPM applications based on the OVP Floorplan, which are using the Initial screen feature. A typical example is the demo application S_EPM_FPM_PD:

Web Dynpro ABAP, SAP ABAP Tutorials and Materials, SAP ABAP Certifications

This application consists of an initial page, allowing you to search for products and a main page where single products are displayed and edited. By clicking a product name in the search result list, the application will launch the main page for this product in a separate window.

Now let’s assume we want to add a “discount” feature to this application – the user shall have the option to reduce the price of the selected products by a certain percentage. With the standard functionality this is a bit cumbersome: For each product to be discounter the user has to
  1. Click the product name
  2. Wait for the new window to be opened
  3. Press the ‘Edit’ button in the new window
  4. Change the Price
  5. Press ‘Save’

Much easier for the user would be a solution where there is a ‘Discount’ button on the initial screen. After clicking this button an input dialog would appear where the user can enter the percentage value and on closing this dialog everything else will be done automatically.

With a single script automation isn’t possible. Nevertheless since SAP_UI 7.50 SP03 there is a mechanism in place allowing to solve such tasks.

Concept


The idea is simple: Instead of enabling scripts to run in 2 windows, Personas now offers the possibility to
  • start a script in the first session,
  • and specify a follow-up and a completion script when navigating to the 2nd window.
  • When the 2nd window is launched the follow-up script will be executed and
  • On completion of the script in the 2nd window the completion script will be executed in the first window


Example


I will now show and explain the scripts needed to add such a discount functionality to demo application S_EPM_FPM_PD.

We will need 3 scripts:

1. A start script which will prompt for the discount rate and then trigger the discounting for all selected products
2. The follow-up script which will be executed once for each product on the application’s main page. This script will calculate, change and save the new product price
3. The complete script which will restart the search, to show the updated values

Start Script


//Determine number of visible rows (this script will only work on visible values)
var visibleRowCount = session.findById("EPM_PD_OVP_SEARCH_LIST_CFG.00.LIST_VIEW.DYNAMIC_TABLE").visibleRowCount;
var rowCount = session.findById("EPM_PD_OVP_SEARCH_LIST_CFG.00.LIST_VIEW.DYNAMIC_TABLE").rowCount;
var firstVisibleRow = session.findById("EPM_PD_OVP_SEARCH_LIST_CFG.00.LIST_VIEW.DYNAMIC_TABLE").firstVisibleRow;
var visibleRows = Math.min(visibleRowCount,rowCount-firstVisibleRow);
//If there are no visible rows return
if (visibleRows == 0) {
         session.utils.alert("No products selected!");
         return; 
}
//Create a followup object, which will be passed to the next browser session
var followup = {scriptId:"wnd[0]/scrptPersonas_1"};
//Prompt for the discount rate and store it in the followup object

followup.parameters = session.utils.prompt("Enter discount rate: ","5");
//Check if user entered a valid value
if(followup.parameters==null || isNaN(followup.parameters)==true) {
         session.utils.alert("Discount value is not a valid Number!");
         return;
}
//Loop for all visible rows and click the product name link
for(var i=firstVisibleRow;i<firstVisibleRow+visibleRows;i++)
{
         if (i==firstVisibleRow+visibleRows-1) {
                   //When the last selected line is processed, specify the complete script
                   followup.onCompleteScriptId = "wnd[0]/scrptPersonas_2";
         }
         //Click the product name and pass the followup object
         session.findById("EPM_PD_OVP_SEARCH_LIST_CFG.00.LIST_VIEW.PRODUCT_NAME_FPM_CE[" + i +"]").Activate( false, false, followup);
};

In line 14 the followup object is created which is used to pass the name of the followup and the complete script, as well as the input parameters of the followup script to the next session.  In line 34 the navigation is triggered and the followup object is passed

Follow-up Script


var discountRate = 5;
//Check if discount rate has been passed, if not prompt the user. This makes it possible to
//run and test the script standalone
if(typeof followUpParameter === 'undefined' || followUpParameter === null)
         discountRate = session.utils.prompt("Enter discount rate: ","5");
else
    discountRate = session.utils.parseFormattedNumber(followUpParameter);
debugger;
//Switch to Edit Mode
if (session.findById("S_EPM_UX_PD_OVP.00.PAGE_HEADER.FPM_CA_TOOLBAR_FPM_EDIT_1").enabled)
                   session.findById("S_EPM_UX_PD_OVP.00.PAGE_HEADER.FPM_CA_TOOLBAR_FPM_EDIT_1").Press();
//Get the current price
var priceString = session.findById("EPM_PD_OVP_HEADER_FORM_CFG.00.V_FORM.FGRP_12").value;
//Calculate new price...
var priceNumber = session.utils.parseFormattedNumber(priceString);
priceNumber = (100 - discountRate) * priceNumber / 100;
//round price to 2 decimals
priceNumber = Math.round(100*priceNumber)/100;
var newPriceString = session.utils.getFormattedNumber(priceNumber);
//...and write it back to the price input field

session.findById("EPM_PD_OVP_HEADER_FORM_CFG.00.V_FORM.FGRP_12").value = newPriceString;
// save
session.findById("S_EPM_UX_PD_OVP.00.PAGE_HEADER.FPM_CA_TOOLBAR_SAVE").Press();

Complete Script


//After all prices have been updated re-execute the search to see the discounted
//prices in the result list

session.findById("FA35119EB8954786093E7A3C90C71FB8.07.V_SELECT_OPTIONS.BTN_SEARCH").Press();
After creating the 3 scripts there is only one thing missing: We have to add a Script Button to start the first script

Web Dynpro ABAP, SAP ABAP Tutorials and Materials, SAP ABAP Certifications

No comments:

Post a Comment