From Thursday, July, 9, 3:00 PM Pacific,
our Community site will be in
read-only mode
through Sunday, July 12th.
Thank you for your patience.

Using local variable in tmap expression builder

Highlighted
Nine Stars

Using local variable in tmap expression builder

Hi,

I was wondering if we can write if-else statement in the expression builder of Tmap, also can we write case statements?? if yes how can we write these both??

Thanks in advance

Highlighted
Community Manager

Re: Using local variable in tmap expression builder

You can write in-line IF statements in the tMap, like below....

 

row1.myIntColumn == 1 ? "MyValueIsOne" : "MyValueIsNotOne"

You can also combine IF statements like so....

row1.myIntColumn == 1 ? "MyValueIsOne" : row1.myIntColumn == 2 ? "MyValueIsTwo" : "MyValueIsNeitherOneOrTwo"

If you want to use a case statement, you should write it into a Routine. Anything that requires more than one statement needs to be done using a Routine.

 

However you *could* potentially use tMap variables (the box in the middle of the tMap window) to carry out a case statement. Set up each of the cases in the variables using an IF statement in each one. If the IF is true, output a value, if not output null. Then in your last tMap variable, check the values of each of tmap variables (each of the cases) and return the one which is not null. This is probably a bit more complicated than just using a Routine however.

Highlighted
Nine Stars

Re: Using local variable in tmap expression builder

so in tmap we have to use ternary operators and write if-else statement.

but for case statement - how can we call a routine onto a tmap expression builder 

can u give small example on the same

thanks

 

Highlighted
Community Manager

Re: Using local variable in tmap expression builder

Here is a basic example of a Case statement in a routine....

package routines;

public class SwitchExample {

    
	public static String intToString(int i){
		String returnVal = "";
		
		switch (i) {
        case 1:  returnVal = "One";
                 break;
        case 2:  returnVal = "Two";
                 break;
        case 3:  returnVal = "Three";
                 break;
        case 4:  returnVal = "Fourl";
                 break;
        case 5:  returnVal = "Five";
                 break;
        case 6:  returnVal = "Six";
                 break;
        case 7:  returnVal = "Seven";
                 break;
        case 8:  returnVal = "Eight";
                 break;
        case 9:  returnVal = "Nine";
                 break;
        case 10: returnVal = "Ten";
                 break;
        default: returnVal = "Greater Than Ten";
                 break;
		}
		
		return returnVal;
		
	}
	
}

To use it in a tMap, you would just call it like this.....

 

routines.SwitchExample.intToString(row.myIntValue)

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog