Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

SAP Screen Personas


 

Copying Table data into a variable


SAP SCREEN PERSONAS KNOWLEDGE BASE - by Sushant Priyadarshi , Regina Sheynblat , Steve Rumsby , Kranthi Kumar Muppala

Purpose

This article will show you couple of options of saving table data into a variable.

It is recommended to use the code below in a simple example first and put a debugger statement, so that you know whats going on. Play around the code sample and suggest us if you have a different way. The examples below shows how to save data in an object or an array, with and without Column names. Please feel free to adjust it based on your requirement.

Example 1

 Transaction Code : GUIT

Screen : 1000

Example 1
//Get the table
var selectedTable = session.findById("wnd[0]/usr/tblSAPM_GUITEST_PORTABLETABLECONTROL")
    //Get the Column IDs
var columns = selectedTable.columns;
//Variable where table contents will be copied
var contents = [];
if (selectedTable.rowCount > 0) {
    //Set the visible row to 0
    selectedTable.firstVisibleRow = 0;
    //Get the Max visible row number
    var topRow = selectedTable.visibleRowCount - 1;
    //Loop through all the rows
    for (var rowIndex = 0; rowIndex < selectedTable.rowCount; rowIndex++) {
        var row = {};
        if (rowIndex > topRow) {
            // Set the first visible row to the next set of rows. If the next set goes beyond the maximum rows,
            // adjust it so that the set's last row is the table's last row. 
            if (topRow + selectedTable.visibleRowCount > selectedTable.rowCount) {
                selectedTable.firstVisibleRow = selectedTable.rowCount - selectedTable.visibleRowCount;
            } else {
                selectedTable.firstVisibleRow = topRow + 1;
            }
            topRow += selectedTable.visibleRowCount;
        }
        // Populate the row information.
        for (var i = 0; i < columns.length; i++) {
            var colName = columns.elementAt(i).name;
            row[colName] = selectedTable.getCellValue(rowIndex, colName);
        }
        // Break after the first blank row - the values usually contain all underscores like "____" for a 4 character column.
        if (row[columns.elementAt(0).name] && !row[columns.elementAt(0).name].replace(/_/g, "")) {
            break;
        }
        contents.push(row);
    }
}
console.log(contents);

 

This is how the output will look like:

Example 2

If you are fancying "columnar" structure (For example, customers coming from Personas 2 should have accessed like variable[column][row]), adjust your code a bit.

Transaction : GUIT

Screen : 1000

Example 2
//Get the table
var selectedTable = session.findById("wnd[0]/usr/tblSAPM_GUITEST_PORTABLETABLECONTROL")
    //Get the Column IDs
var columns = selectedTable.columns;
//Variable where table contents will be copied
var contents = [],
    bEndOfTable = false;
if (selectedTable.rowCount > 0) {
    //Set the visible row to 0
    selectedTable.firstVisibleRow = 0;
    //Get the Max visible row number
    var topRow = selectedTable.visibleRowCount - 1;
    //Loop through all the rows
    for (var rowIndex = 0; rowIndex < selectedTable.rowCount; rowIndex++) {
        var row = {};
        if (rowIndex > topRow) {
            // Set the first visible row to the next set of rows. If the next set goes beyond the maximum rows,
            // adjust it so that the set's last row is the table's last row. 
            if (topRow + selectedTable.visibleRowCount > selectedTable.rowCount) {
                selectedTable.firstVisibleRow = selectedTable.rowCount - selectedTable.visibleRowCount;
            } else {
                selectedTable.firstVisibleRow = topRow + 1;
            }
            topRow += selectedTable.visibleRowCount;
        }
        // Populate the row information.
        for (var i = 0; i < columns.length; i++) {
            // Break after the first blank row - the values usually contain all underscores like "____" for a 4 character column.
            if (!selectedTable.getCellValue(rowIndex, columns.elementAt(0).name).replace(/_/g, "")) {
                bEndOfTable = true;
                break;
            }
            var colName = columns.elementAt(i).name;
            if (rowIndex == 0) {
                contents[i] = new Array();
            }
            contents[i][0] = colName;
            contents[i][rowIndex + 1] = selectedTable.getCellValue(rowIndex, colName);
        }
        if (bEndOfTable) {
            break;
        }
    }
}
console.log(contents);

 

This is how your output will look like: