EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

KB20386: Querying the ColumnHeader on a grid cell value returns null when cell value is a subtotal using the MicroStrategy Web Java API 9.4.x


Community Admin

• Strategy


This article explains how querying the ColumnHeader on a grid cell value returns null when cell value is a subtotal using the MicroStrategy Web Java API 9.4.x

The following code will execute a report in the Metadata and loop through the different attribute elements and metric values:


package com.Strategy.standalone;

import com.Strategy.web.beans.ReportBean;
import com.Strategy.web.beans.WebBeanFactory;
import com.Strategy.web.objects.EnumWebRowValueType;
import com.Strategy.web.objects.WebHeaders;
import com.Strategy.web.objects.WebIServerSession;
import com.Strategy.web.objects.WebObjectsFactory;
import com.Strategy.web.objects.WebReportGrid;
import com.Strategy.web.objects.WebRow;
import com.Strategy.web.objects.WebSessionInfo;

public class ExecuteReport {

 /**
  * @param args
  */
 public static void main(String[] args) {
  try {
   // Connect to Strategy IS
   WebObjectsFactory factory = WebObjectsFactory.getInstance(); 
   WebIServerSession session = factory.getIServerSession();
   session.setServerName("localhost");
   session.setLogin("Administrator");
   session.setPassword("");
   session.setProjectName("Strategy Tutorial");
   String sID = session.getSessionID();
   
   // Create a WebBean factory
   WebBeanFactory bFactory = WebBeanFactory.getInstance();
   ReportBean rb = (ReportBean) bFactory.newBean("report");
   rb.setSessionInfo((WebSessionInfo)session);
   
   // Instantiate a report
   rb.setObjectID("30E9353B44895723B0CEF3AA73A70530"); // Item Contribution (5 metrics)
   // Run the report
   rb.collectData();
   
   System.out.println("Data collected");
   
   WebReportGrid wrg = rb.getReportInstance().getResults().getWebReportGrid();
   
   for (int i=0; i<wrg.getGridRows().size();i++){
    
    WebHeaders webHeaders = wrg.getRowHeaders().get(i); 
    WebRow webrow = wrg.getGridRows().get(i);
    
    for (int j=0; j<webHeaders.size(); j++) {
      System.out.println(webHeaders.get(j).getTitle().getTarget().getDisplayName() + ": " + webHeaders.get(j).getDisplayName());
    }

    //Metric Values for each row
    for (int x=0;x<webrow.size();x++){
     if (webrow.get(x).getType() == EnumWebRowValueType.METRIC){
      System.out.println(webrow.get(x).getColumnHeader().getDisplayName() + ": " + webrow.get(x).getValue());
     }
     else
      
      System.out.println("Total " + wrg.getColumnHeaders().get(0).get(x).getDisplayName() + ": " + webrow.get(x).getValue());
    } 
    
   }
  
   // Bye bye
   session.closeSession(); 
  } catch (Exception e) {
   System.out.println(e.getMessage());
   e.printStackTrace(System.out);
  }


 }

}



 

The above code executing the following report:

ka0PW0000000ow5YAA_0EM440000002GxA.jpeg

Will return the following output:
Category: Total
Subcategory:
Total Revenue: $68,366,469
Total Profit: $11,557,740
Category: Books
Subcategory: Total
Total Revenue: $4,024,235
Total Profit: $890,696
Subcategory: Art & Architecture
Revenue: $859,358
Profit: $201,412
Subcategory: Business
Revenue: $589,246
Profit: $132,117
Subcategory: Literature
Revenue: $295,385
Profit: $59,491
Subcategory: Books - Miscellaneous
Revenue: $366,250
Profit: $67,148
Subcategory: Science & Technology
Revenue: $1,375,727
Profit: $310,489
.........
The code attached to this Technical Note loops through each of the rows in the grid and reads every cells value and header value to build the output:


[Cell header value]:[Cell Value]

When reading the metric cells there is something that we need to be careful with. Cell header value for cells that contain the value of a subtotal will return null. When calling the method getColumnHeader() or getRowHeader() on a metric cell of type subtotal, the method call will return null.
Metric cells can be of two different types EnumWebRowValueType.METRIC or EnumWebRowValueType.SUBTOTAL
EnumWebRowValueType
In the code we have added an if statement to check the type of metric cell value being returned

if (webrow.get(x).getType() == EnumWebRowValueType.METRIC){}

What we do to still be able to distinguish subtotal values of one or the other column (Revenue or Profit) is to query the column header of the first row instead of the current one.
So in the if statement for when the cell value is of type subtotal instead of reading the current row cell header value:

webrow.get(x).getColumnHeader().getDisplayName()

 
We read it from the top row:

wrg.getColumnHeaders().get(0).get(x).getDisplayName()



 

Additional information on the different objects contained in the grid object in Strategy can be found under:
Report Grid XML
The example provided in this document is provided “as-is” to the user and assumes that the user:

  • Can program, compile (e.g. javac, jikes, etc.), and execute Java programs
  • Can configure environment variables (e.g. JAR files, property files, etc.)
  • Have all the necessary tools to create Java applications (JDK, IDE, etc.)
  • Has access to the Strategy SDK documentation.
  • Has read the customization warning at the bottom of this document

PREREQUISITES:
For the sample to work, the Strategy Web JAR files must be accessible by the Java Runtime
Environment. The Strategy Web JAR files can be found under:

  • Strategy Web Universal (JSP): {web_root}/WEB-INF/lib directory.
  • Strategy Web (.NET): Program Files\Common Files\Strategy directory.

Note:
More elaborate programs will require additional library files and are out of the scope of this document.
ADDITIONAL INFORMATION:
The Strategy SDK allows you to customize several Strategy products and extend and integrate the Strategy business intelligence functionality into other applications. However, before changing the way Strategy products look or behave, it is helpful to understand how the application is built. For more information regarding the Strategy products or the process of customizing Strategy products, please refer to the  MicroStrategy Developer Hub.
To access the Strategy Developer Hub, you must have access to the Strategy Knowledge Base, you must have purchased the Strategy SDK, and you must be current on your Strategy maintenance agreement. If you are a US-based business and believe that you satisfy all three of these conditions but you do not have access to the Strategy Developer Hub, please contact Strategy Technical Support at support@Strategy.com or at (703) 848-8700. If you are an international business, please contact Strategy Technical Support at the appropriate email address or phone number found at https://www.Strategy.com/us/services/support/contact.
CUSTOMIZATION WARNING:
This customization is provided as a convenience to Strategy users and is only directly applicable to the version stated. While this code may apply to other releases directly, Strategy Technical Support makes no guarantees that the code provided will apply to any future or previous builds. In the event of a code change in future builds, Strategy Technical Support makes no guarantee that an updated version of this particular customization will be provided. In the event of a code change in future builds, Strategy may not be able to provide additional code on this matter even though this customization is provided at this time for this specific build. For enhancements to this customization or to incorporate similar functionality into other versions, contact your Account Executive to inquire about Strategy Consulting assistance.


Comment

0 comments

Details

Knowledge Article

Published:

June 30, 2017

Last Updated:

February 16, 2024