Sum and Subtraction on Table Records

Hello!

Here's an image of a given Table Records:

I want to be shown on the dKms column, the subctration between 191407-19101, and I've already tried the following expression:


If( FuelOrderTable.List.CurrentRowNumber + 1 = 0 , 0 , 

FuelOrderTable.List[ FuelOrderTable.List.CurrentRowNumber +1 ].FuelOrder.Kms -
FuelOrderTable.List.Current.FuelOrder.Kms )

but occurs a Internal Error and the Table isn't shown.


I'm new to OutSystems and some of these math operations using vectors are in someway, challeging to me.

Thanks in advance,

ruipmoura

Hi Rui,


Have you looked in service center to see exactly what the error message is? It will give more details behind the "internal error"  .  Also.  Can you upload a sample?  . If you remove that formula, does the table show?

Where are you placing this expression?

Looks like this is two separate tables, correct?

Also, what version of the OS platform and Service Studio are you using?

Hey Rui,

I assume you are showing 2 separate table records?  They are not rendered row 1 of table 1, row 1 of table 2, row 2 of table 1, etc., they are rendered per table.  This means when you reference FuelOrderTable, you are likely always referencing the last row.

Also, you are probably getting an internal error due to your array going out of bounds.  When you have FuelOrderTable.List[FuelOrderTable.List.CurrentRowNumber + 1], if you are on the last row then by adding 1 to it you are going out of bounds.

I also find this line curious, 

If( FuelOrderTable.List.CurrentRowNumber + 1 = 0 , 0 

When are you expecting the current row number + 1 to equal 0?  The first row has a CurrentRowNumber of 0, not -1.

If you can provide a sample OML file of how you've structured your screen and your data, I would be happy to show you how to proceed.


Stacey Levine,


I've done it, and it was what I suspected.

The error given in the Service Center is "index 11 is out of range", and it's because the last record hasn't an instance after, so it does not have data to do the math operation. 

And yes, if I remove the Expression the table is shown!

I've attached the .oml file.


Thanks





So you just need to add an extra If around the expression to not do that math at your last row.

tell me one thing each line on the table on the right correspont a line on the table on the left?


If you want the subtraction of the first line and the second line to appear in that expression just use

FuelOrderTable.List[1].FuelOrder.Kms - FuelOrderTable.List[0].FuelOrder.Kms 

If you want something to be used in all lines then you need to write something a bit more complex

FuelOrderTable.List[2nd table name.list.currentRownNumber + 1].FuelOrder.Kms - FuelOrderTable.List[2nd table name.list.currentRownNumber].FuelOrder.Kms 

BUT You must place a condition that will not try to subtract the last record from the last record+1 so :

if(2nd table name.list.currentRownNumber = FuelOrderTable.List.length-1,

//no ideia what you want to subtract on the last one
0
,
FuelOrderTable.List[2nd table name.list.currentRownNumber + 1].FuelOrder.Kms - FuelOrderTable.List[2nd table name.list.currentRownNumber].FuelOrder.Kms )


Something like this i think.

Craig St.Jean wrote:

Hey Rui,

I assume you are showing 2 separate table records?  They are not rendered row 1 of table 1, row 1 of table 2, row 2 of table 1, etc., they are rendered per table.  This means when you reference FuelOrderTable, you are likely always referencing the last row.

Also, you are probably getting an internal error due to your array going out of bounds.  When you have FuelOrderTable.List[FuelOrderTable.List.CurrentRowNumber + 1], if you are on the last row then by adding 1 to it you are going out of bounds.

I also find this line curious, 

If( FuelOrderTable.List.CurrentRowNumber + 1 = 0 , 0 

When are you expecting the current row number + 1 to equal 0?  The first row has a CurrentRowNumber of 0, not -1.

If you can provide a sample OML file of how you've structured your screen and your data, I would be happy to show you how to proceed.


Yeah, 2 Table Records. FuelOrderTable is the main one, and contains data that the user inputs, the second one is a created to return in this case, fuel consumption and other wanted indicators.


Since the last row + 1 has no records, wrongly I assumed that was filled with 0, that's why that preposition. 


The file is already attached in a previous comment.

Thanks


Here i change the preparation of the oml a bit

Hey Rui,

What about an expression like this:

If (TableRecords1.List.CurrentRowNumber = 0, 0,
    FuelOrderTable.List[TableRecords1.List.CurrentRowNumber].FuelOrder.Kms - FuelOrderTable.List[TableRecords1.List.CurrentRowNumber - 1].FuelOrder.Kms)

On the first row, we will just have 0.  Otherwise, we will take the Kms of the current row minus the previous row's Kms.  Is that what you are trying to accomplish?

Carlos Diogo Lima Gonçalves wrote:

Here i change the preparation of the oml a bit

now, the problem returned to the 1st row since the Internal Error returned "index 1 is out of range".

Unfortunately, I'm quite new in programming language and the cycle done on the preparation is not easily obvious, still I've almost sure you understood what i want to implement.


Solution

Looking at your preparation, you are looping and comparing against the GetFuelOrders result, however your assign statement is working on FuelOrderTable instead of GetFuelOrders.  Can you adjust your assignment to use GetFuelOrders like this:

GetFuelOrders.List[GetFuelOrders.List.CurrentRowNumber + 1].FuelOrder.Kms - GetFuelOrders.List.Current.FuelOrder.Kms

Solution

Sorry i just found out that you are using the wrong table. i just rollback all the changes i did on the oml and did some changes on the screen expressions.


Note: this thing you are trying to do will stop work when you sort the table since the values will change position.

Carlos Diogo Lima Gonçalves wrote:

Sorry i just found out that you are using the wrong table. i just rollback all the changes i did on the oml and did some changes on the screen expressions.


Note: this thing you are trying to do will stop work when you sort the table since the values will change position.

This I've already achieved by the following expression (the first one i've used):


If( FuelOrderTable.List.CurrentRowNumber = 0 , 0 ,

FuelOrderTable.List.Current.FuelOrder.Kms -
FuelOrderTable.List[ FuelOrderTable.List.CurrentRowNumber -1 ].FuelOrder.Kms )


Craig, I haven't tried already your last suggestion, i'll do it next, but this is what i want:


where, 191407-191010 = 397, 192135 - 191 407= 728, and so on.


Let me express my sincere thanks for all of this help.

Craig St.Jean wrote:

Looking at your preparation, you are looping and comparing against the GetFuelOrders result, however your assign statement is working on FuelOrderTable instead of GetFuelOrders.  Can you adjust your assignment to use GetFuelOrders like this:

GetFuelOrders.List[GetFuelOrders.List.CurrentRowNumber + 1].FuelOrder.Kms - GetFuelOrders.List.Current.FuelOrder.Kms

The change on the Preparation proposed by Carlos and your adjustment to the assign solved the problem :D

Al last, can i apply the same procedure for the dDate and Lt/100 Km columns?


Many thanks for the precious Help.




Glad things are working!  Yes, you can apply the same pattern to those columns.  For dDate, there is a built-in DiffDays function as well.

There's only a bit of "searching" left to understand all of this procedure, and It's done.


You made my day, both of You,

Many Thanks

Rui Pedro Moura wrote:

There's only a bit of "searching" left to understand all of this procedure, and It's done.


You made my day, both of You,

Many Thanks

Glad that i helped :)