Using if - else if - else in tMap

Four Stars

Using if - else if - else in tMap

DateTimeCreated  (String)                    EnquiryUID   (String)                                                            ChannelId (Integer)                   ClientId (Integer)

2018-07-16 00:56:46                             037CA2E9-3860-4D45-9234-4894BC8F07FA                     229

2018-07-15 23:48:00                             1807158dc9a4a9047142ea8f06                                          296

 

Requirements:

case 1: If digits 2 to 8 of DateTimeCreated  match digits 0 to 6 of EnquiryUID   and ChannelId is 229 or 296, then ClientId is 0

case 2: If digits 2 to 8 of DateTimeCreated  do not match digits 0 to 6 of EnquiryUID   and ChannelId is 229 or 296, then ClientId is 9

case 3: else ClientId

 

This is the method in the routine IsGTX_EnquiryUID

 

public static boolean isGTX (String dateTimeCreated, String EnquiryUID){

String noSpacesDateTimeCreated = dateTimeCreated.replaceAll("\\D", "");

String dateTimeCreatedChar2to8 = StringUtils.substring (noSpacesDateTimeCreated, 2, 8);


String EnquiryUIDFirst6Char = StringUtils.substring(EnquiryUID, 0, 6);

if (dateTimeCreatedChar2to8.equals(EnquiryUIDFirst6Char)) {

return true;

}

else {

return false;


}
}

What I did so far is using the trinary operator in tMap, but it only works on one condition.

row1.ChannelId == 229?( IsGTX_EnquiryUID.isGTX(row1.dateTimeCreated,  row1.enquiryUID)?0 :9):row1.clientId; 

I tries to use if - else if - else in tMap, but it did not worked.

How to do this for 3 different conditions in tMap?

 I am using Talend Data Management Platform v6.3.1

 

 

 

Community Manager

Re: Using if - else if - else in tMap

You can do this with an inline IF condition. As an example, if you have a condition like this....

if(myInValue==1){
      myOutValue = "a";
}else if(myInValue==2){
     myOutValue = "b";
}else if(myInValue==3){
     myOutValue = "c";
}else{
     myOutValue = "z";
}

....you would represent it like this as an in-line IF....

myOutValue = (myInValue==1 ? "a" : myInValue==2 ? "b" : myInValue==3 ? "c" : "z")  
Employee

Re: Using if - else if - else in tMap

Hi,

     My suggestion will be to use a tjavarow for complex row level if-else condition operations. Even though you can do the same logic in tmap, you may have to to think about the readability of the conditions also. 

Four Stars

Re: Using if - else if - else in tMap

Hi @rhall_2_0,

 

Your suggestion is good if I have one InValue, but the problem is that I have different requirements. You are suggesting:

myOutValue = (myInValue==1 ? "a" : myInValue==2 ? "b" : myInValue==3 ? "c" : "z")  

 I have two inValues

myOutValue = if (myInValueOne==1 || myInValueOne==2) and myInValueTwo ==3 ? "a":"b", else myOutValue;

 

Four Stars

Re: Using if - else if - else in tMap

Thank you, I will check if possible to add a new component in the job.
Community Manager

Re: Using if - else if - else in tMap

myOutValue = ((myInValueOne==1 || myInValueOne==2) && myInValueTwo==3 ? "a" :"b")

Your pseudocode IF had an extra ELSE which would never be reached....

 
if (myInValueOne==1 || myInValueOne==2) and myInValueTwo ==3 ? "a":"b", else myOutValue;

2019 GARNER 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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch 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