Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

Help me with dynamic subtotals in a table.

Avatar

Former Community Member
I am having a problem with some formcalc I'm working on. Please help.



The form I've created is initially empty, and data is passed to it by an SAP NetWeaver system. I am passing delivery line items to the form. My form is structured like this:



LineItems

| HeaderRow

| Items (this is a grouping, and each occurance of the grouping maps to a record being passed to the form)

| | Row1 - VBELN_VA, BRGEW (sales order number and weight)

| | Row2 - POSNR_VA (sales order item)

| | Row3 - No Data worth mentioning here

| | FooterRow - SumGWeight (this field is the one that contains the script below)



Two things that I should perhaps mention:

1) the footer row is not defined as a footer row in the Object/Row/Type field - it's defined as a body row and is part of the "Items" grouping, because I might need it to show up per line item.

2) there is a script in the FooterRow initialize event that hides the entire row if the current sales order number <> next sales order number in the table.



What I am trying to get to is a subtotal by sales order number. If I just write



Code:

Sum LineItems.Row1[*].BRGEW



as I understand it, I will get a total of all records. So, I came up with this code:



//each occurance of sales order item number should give me total rows in //the table

var total = Count(data.BodyPage.PageFlow.LineItems.Item.Row2[*].POSNR_VA)



// this should give me the current sales order number

var firstSO = data.BodyPage.PageFlow.LineItems.Item.Row1.VBELN_VA.value.#text



//do once for each line in the table

for i = 0

upto total

step 1 do

// get the value of the sales order number for the table row that matches //the current loop pass

var secondSO = data.BodyPage.PageFlow.LineItems.Item.Row1[i].VBELN_VA.value.#text

var secondSOTotal = data.BodyPage.PageFlow.LineItems.Item.Row1[i].BRGEW.value.#text

// if the two are equal, increment our subtotal

if (firstSO == secondSO) then

$.value.#float = $.value.#float + secondSOTotal

endif

endfor



What I would expect to see after all this is (leaving Row2 & Row3 off, since they're not really relevant):



S01 | 100

S01 | 102

*Subtotal | 202*

S02 | 103

S02 | 104

*Subtotal | 207*

S03 | 104

S03 | 106

*Subtotal | 210*

S04 | 107

*Subtotal | 107*



What I'm actually seeing is this:



S01 | 100

S01 | 102

*Subtotal | 204*

S02 | 103

S02 | 104

*Subtotal | 311*

S03 | 104

S03 | 106

*Subtotal | 316*

S04 | 107

*Subtotal | 214*



There are two problems here:



1) It looks like it's skipping the first item in the table when doing the subtotal calculation

2) For each row where the subtotal line shows up, it's double counting



I think the reason for this is as follows - when the loop is doing the first loop pass, the value of the variable i is 0. When it tries to look at index 0 of the table, it is actually looking at the current index of the table, instead of the first table row. So, in the first subtotal, we see the first table row in skipped and the second is doubled. In the second subtotal, the first table row would be ignored anyway, so we only see the 4th row doubled.



Does anyone have any idea how I might fix this? Please let me know if there is anything I can clarify or explain further.



Thank you in advance.
0 Replies