> Task 3: | Conditionally increment the counter in the Row.onCreate( ) method

> To count the number of customers with the string Mini in their names, you must examine each customer’s name and add one to the counter for every occurrence. A logical place to perform this task is in the Row.onCreate( ) method, which executes with every retrieval of a row of data from the data source.

> 1 | In Layout, select the Row and choose Script.

> 2 | Pull down the list of methods at the top of the script window and select onCreate, as shown in Figure 23‑8.

> Figure 23-8 | onCreate( ) in the script window

> 3 | Enter the following lines of JavaScript code in the Script window:

> countOfMinis = reportContext.getPersistentGlobalVariable("cmKey");
> row_Data = this.getRowData();
> Notice that when you enter the period after this, a pop-up appears containing all the available methods and properties, including getRowData. This line of code gets an instance of IRowData, which has a method, getExpressionValue( ), to get the contents of a column of the row.

> 4 | Type the following line of JavaScript below the lines you just entered:

> CustName=row_Data.getExpressionValue( "row[CUSTOMERNAME]" );
> This line of code returns the contents of the table column that comes from the CUSTOMERNAME column in the data set.

> 5 | Type the following lines of code to conditionally increment the counter you created in Create and initialize a counter in the Table.onCreate( ) method.

> if(CustName.indexOf("Mini") != -1){
> mCount = countOfMinis.intValue();
> mCount += 1;
> countOfMinis = new Packages.java.lang.Integer(mCount);
> reportContext.setPersistentGlobalVariable("cmKey", countOfMinis);
> }
> You can use the JavaScript palette to insert each of the following elements in the preceding lines:

> n | indexOf( )

> Select Native ( JavaScript ) Objects->String Functions->indexOf( )

> n | !=

> Select Operators->Comparison->!=

> n | +=

> Select Operators->Assignment->+=

> 6 | Choose Preview to run the report again to verify that the code you entered did not create any errors.

(c) Copyright Actuate Corporation 2008