Get inner detail value based on all records

Hello @Sander @Erik @hamelj @Marten and other OL society.

For example I have nested inner tables called “details”

Each details have field “NoChargeFlag” which value is sometimes “Y” or sometimes empty.

I created this code and place insid text script (each result set matching)

var detailS = record.tables.detailS;
var myChargesContainer = "";

for( var i = 0; i < detailS.length; i++) 
{
	var details = detailS[i].tables.detailDetails;
	
	for( var j = 0; j < details.length; j++) 
	{
		myChargesContainer += details[j].fields.NoChargePerContract + "|";
		
	}
}

if(myChargesContainer.includes('Y')) {
	this.show();
}
else {
	this.hide();
}

I am thinking is better way doing this?

Adam

Just for fun, 2022.2 allows you to do this:

const flat = Array.map(record.detailS, r => r.detailDetails).reduce(Array.concat);
const noCharge = flat.some(r => r.NoChargePerContract == "Y");
noCharge ? this.show() : this.hide();

Aside from arrow functions, 2022.2 also supports for…of loops, which would improve readability.

None of that works in the current version of Connect though.

Your code looks okay to me. Maybe you could refactor it a bit to break out of those loops as soon as you encounter a “Y” since there is no point in continuing after that.

Good idea.

Ale when You use map() function then You choose only partiuclar fields? So it should improve performance maybe @Sander

Not sure about performance. I was trying to get it done with the least amount of code possible, but I think readability is more important. Just go with whatever makes the most sense to you.

1 Like

I have one more question, maybe trivial maybe not or stupid.

Why in that case we use const? Instead of var or let.

It’s a good question, it’s just a habit. As a rule I use let instead of var, and const instead of let (where possible). It’s a stylistic choice, just like you can either add a semicolon at the end of each line or choose to always omit semicolons.

Let is block-scoped, which makes more sense to me than function-scoped (especially in for loops). Const is a signal that the variable is never reassigned, which can make the code a little bit easier to read and maintain. But in the end it really doesn’t matter much.

1 Like