###### Technical Articles # Introduction:

Hello all! This is the continuation of my first blog. Please have a look at it before this.

# Requirement Study:

As said in the conclusion of my first blog, if the data contains 5000 records, the loop has to be run 5000 times plus the number of subtotal rows. In the Above case it was 6 + 3 = 9 times

• 6 = No of Data
• 3 = No of Subtotal Rows

So Time Complexity will be O( No of Data + No of Subtotal Rows )

————————————————————————————————————————————–

We can optimize it to O(number of subtotal rows) by using a simple technique🤔 Let’s start.

# Steps:

1. Introduce One more internal table in Form Interface. 2. Fill them with subtotal Rows index on ABAP ( since Javascript array index starts from 0, we have to subtract 1 and store the index ). Note: I am hard-coding the values for blog purposes, but you have to place the appropriate logic for filling them.
3. Remove the subtotal flag mentioned in the previous blog, as there is no need for that now. Place the following script in the same place mentioned in the previous blog.
``````// Get rows_table Reference that is coming from ABAP
var rows_table = xfa.resolveNodes("\$record.ROWS_NO.DATA[*]");

// Get length of the table for running the loop.In our above case it will be 3. Loop Will be Run for three times
var length = rows_table.length;

// Loop Subtotal Row, get index one by one and apply formatting
for( var i =0 ; i<length; i++)
{
// Get Subtotal Row_index and use it for further formatting.
var row_index = xfa.resolveNode("\$record.ROWS_NO.DATA[" + i + "].ROW_NO").value;
// Explanation for this part given on Previous Blog. Refer that
xfa.resolveNode("data.MainSubform.ItemDetails.Item.FORM_DATA.DATA[" + row_index + "].INVOICE_NO").colSpan = "2";
xfa.resolveNode("data.MainSubform.ItemDetails.Item.FORM_DATA.DATA[" + row_index + "].INVOICE_DATE").presence = "hidden";
xfa.resolveNode("data.MainSubform.ItemDetails.Item.FORM_DATA.DATA[" + row_index + "].INVOICE_NO").para.hAlign = "center";
xfa.resolveNode("data.MainSubform.ItemDetails.Item.FORM_DATA.DATA[" + row_index + "]").fillColor = "192,192,192";
xfa.resolveNode("data.MainSubform.ItemDetails.Item.FORM_DATA.DATA[" + row_index + "].INVOICE_NO").font.weight = "bold";
xfa.resolveNode("data.MainSubform.ItemDetails.Item.FORM_DATA.DATA[" + row_index + "].INVOICE_CURRENCY").font.weight = "bold";
xfa.resolveNode("data.MainSubform.ItemDetails.Item.FORM_DATA.DATA[" + row_index + "].INVOICE_AMOUNT").font.weight = "bold";

}
``````
4. Please choose option Server for Run At Field in script.
5. That’s solve. We got the same output with fewer iterations (reduced to 3). So even if it were 1000 records and two subtotal rows only, the loop would run only two times. # Summary:

In this blog, I explained the optimization of my first blog method for subtotals.

Sincerely appreciate any feedback, comments, or questions.

Thanks

Nagaraj R