Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
chou
Contributor III
Contributor III

Edit output_row dynamically

Hello, 

 

I am using tjavaRow, 

I have many output_row starting with VALEURUNITE, inspite of writing many times output_row.VALEURUNITE**** = input_row.VALEURUNITE

I looped, but I didin't knew how to edit the output_row value : 

for ( java . lang . reflect . Field field: output_row.getClass().getDeclaredFields()) {

if (field.getName().matches("VALEURUNITE.*")) {
output_row.[field.getName()] = input_row.valeur_unite_tmp;
}

}

Can you help me by telling me how to implement the output_row values dynamically?

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable

An even better solution. All of my fields are called "newColumn", "newColumn1", etc, etc. In this example I am changing just one column called "newColumn".

 

output_row.newColumn = input_row.newColumn;
output_row.newColumn1 = input_row.newColumn1;
output_row.newColumn2 = input_row.newColumn2;
output_row.newColumn3 = input_row.newColumn3;

int count = 0;

for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) {
	if(count>1){
		System.out.println(field.getName());
		if(field.getName().compareToIgnoreCase("newColumn")==0){
			field.set(output_row,"TEST123");
		}else{
		 	field.set(output_row,field.get(output_row));
		}
	}
	count++;
	
}

View solution in original post

6 Replies
Anonymous
Not applicable

It is not entirely clear what you are looking to do here. Can you elaborate please?

chou
Contributor III
Contributor III
Author

I am trying to loop all output columns, then find ones that are finishing
with the word "valenturate" and then load all these output_row with a
value. My problem how to load these output_row?

Inspite of doing
output_row.1valenturate = 1
output_row.2valenturate = 1
output_row.3valenturate = 1
I want to do it on one step.
Anonymous
Not applicable

Ah I see. There are a couple of hacks to do this. See below an example of how to set every column to the same value without errors. I am assuming you can extrapolate to include your logic....

 

int count = 0;

for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) {
	if(count>1){
		field.set(output_row,"TEST123");
	}
	count++;
}

What the above does is very similar to your base code. I am skipping the first two fields because they are hidden fields that are not used,. Trying to set those will cause an error. The subsequent fields I know (in this example) are all Strings. 

Anonymous
Not applicable

An even better solution. All of my fields are called "newColumn", "newColumn1", etc, etc. In this example I am changing just one column called "newColumn".

 

output_row.newColumn = input_row.newColumn;
output_row.newColumn1 = input_row.newColumn1;
output_row.newColumn2 = input_row.newColumn2;
output_row.newColumn3 = input_row.newColumn3;

int count = 0;

for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) {
	if(count>1){
		System.out.println(field.getName());
		if(field.getName().compareToIgnoreCase("newColumn")==0){
			field.set(output_row,"TEST123");
		}else{
		 	field.set(output_row,field.get(output_row));
		}
	}
	count++;
	
}
akumar2301
Specialist II
Specialist II

looks intresting approach  @chou 

 

can you please let us know why you are trying to do this ? what is your complete use case?

 

 

chou
Contributor III
Contributor III
Author

I am trying to do this, inspite of writing output_row many times for each case, here is the solution and it is woring great thanks to @rhall  :

int i = 0;
for (java.lang.reflect.Field field: output_row.getClass().getDeclaredFields()) {
if (i>1){

if (field.getName().equalsIgnoreCase("VALEUR_" + input_row.code)){
field.set(output_row,input_row.valeur_tmp);
}
else if (field.getName().equalsIgnoreCase("LIBELLE_" + input_row.code)){
field.set(output_row,input_row.Libelle_tmp);
}

else {

field.set(output_row,"");

}

}
i++;

}