Convert code from "Column Expressions" to "Java Snippet"

Hello, @takbb how are you?
Sorry to bother you.
I would like some help to learn how the code I did in “Column Expressions” would look if it were created in “java snippet” code would be done in java snippet.

For me to understand how the logic works in Java snippet, it’s easier if it’s with some data that I’m familiar with.

But take your time, you can look at it whenever you’re free. It’s just for my learning.

I’m providing a workflow already with the rules executed.
The columns in “blue” are the values ​​from the database, the columns in yellow are the ones created by “column expressions.”

About the file: it’s just an inventory control, product cost, where the initial balance is 57 units, with a total value of 6726.52 and a unit value of each piece of 118.01 (first row).

Then, inventory control is done where code 1409 means a goods entry, so 57 +1 is added, and the value of 146.01 is added to the total.

(In the “@Qtd_Consumo” column, what is negative is stock output and what is positive is stock input)

In the example file, the result is already there, and by generating the flow again, the columns are created again in “orange”.

(I tried to keep it as simple as possible. In fact, it was you who helped me build the “column expressions” formula, but I know you have a lot of knowledge in Java and I had this pending task of using this example and trying to ask for your help to build it in Java, so I can understand and learn how to better understand the node).

You can use any java snippets you have to…

Change_to_java_snippet.knwf (26.5 KB)

1 Like

Hi @Felipereis50 , I will take a longer look when I have some time. And of course if anybody else wants to do the conversion, feel free to give it a go!

I haven’t fully inspected each of the Column Expression nodes, but I think they could probably all be merged into a single Java Snippet, or would you prefer them to remain as three Snippets (one for each Column Expression) ?

One notable difference in the two is that the rowindex of the first row in Column Expressions is 1 whereas for Java Snippet it is 0. Beyond that, for the most part the port from one to the other can be done reasonably methodically.

Here’s the first Column Expression, to be going on with:

var vInitial

switch (rowIndex()) 
 case 1:
    vInitial=vInitial + column("@Qtd_Consumo");

This could be written as the following Java Snippet

// Your custom variables:
Integer vInitial;

// Enter your code here:

switch (ROWINDEX)
	case 0:  // first row in JavaSnippet is 0 whereas first row in CE is 1
		vInitial += c_Qtd_Consumo;

out_qt_saldo_aggreg = vInitial;

I named the output column in this screenshot “qt_saldo_aggreg_js” as there was already the “qt_saldo_aggreg” column.

I’ll work through the others when I have some more spare moments, and upload the flow.

1 Like

Thanks Brian (for your time) :sunglasses:

(for the Java snippet, if you only need 1 node to the task, there’s no problem to use one)

You can see the others columns with no hurry.

(when I get back home, I will analyze it this first part)

:+1: (thanks again)

1 Like