One Star

IF/else statement usage in tMap

Hi - I have a problem in applying the if/else condition in tMap. I am trying to do below validations in tMap.
if (in.stop_seq == 1 && in.message_status == 10 && in.message_type == 40) 'AA'
else if (in.stop_seq == 1 && in.message_status == 10 && in.message_type == 45) 'X3'
else if (in.stop_seq == 1 && in.message_status == 10 && in.message_type == 50) 'AF'
else if (in.stop_seq == 2 && in.message_status == 10 && in.message_type == 40) 'AB'
else if (in.stop_seq == 2 && in.message_status == 10 && in.message_type == 45) 'X1'
else if (in.stop_seq == 2 && in.message_status == 10 && in.message_type == 50) 'CD'
My source is a db2 and trying to populate a output filed based on this condition. As am new to this, tried all possible options but didn't work. Could you please help?
Note:In my screenshot attached, i tried with one input value to check and populate the target.
Thanks in advance!!!

  • Data Integration
29 REPLIES
One Star

Re: IF/else statement usage in tMap

In the tmap, the formula must return a value that will be assigned to the output column of your schema. Then, you can't use if/then/else statements in it.
Instead, use conditional operator "?" like this :
(condition) ? then_value : else_value
For your example :
(in.stop_seq == 1 && in.message_status == 10 && in.message_type == 40) ? "AA" :
((in.stop_seq == 1 && in.message_status == 10 && in.message_type == 45) ? "X3" :
((in.stop_seq == 1 && in.message_status == 10 && in.message_type == 50) ? "AF" :
...
((in.stop_seq == 2 && in.message_status == 10 && in.message_type == 50) ? "CD" : null)))
One Star

Re: IF/else statement usage in tMap

Boulayj - Thanks a lot!! It really worked. Could you please let me when to use if/elseif & ?: in tMap expression?
I am really new to Java..learn the things. Once again your help is greatly appreciated.:-)
One Star

Re: IF/else statement usage in tMap

You must always use conditional operator "?" in tMap expression.
If you want to use if/then/else (for a more complex expression) then you have to create your own routine and to use it in the tmap expression. A routine is a java method that must return a value.
One Star

Re: IF/else statement usage in tMap

talend generate java script and when you put a value in the interface field it write myField = your_value;
you can use routine or the simplify syntax of java but you cant use function or other condition.
when u change something u can see the java code in the code tab of the job, if there is red line u have bad entry in a component. it s very important for the data type Smiley Happy
Seventeen Stars

Re: IF/else statement usage in tMap

NO, Talend does not create Java Script!!. JavaScript != Java. Its only the name, nothing else.
It would be correct to write "Talend generate java code..."
By The Way, this post is in the wrong category!
Moderator

Re: IF/else statement usage in tMap

Hi nboulic,
Thanks for sharing your experience of using tMap component with us(exactly, Talend generates Java code). In addition, for the user routine mentioned by @boulayj, please refer to How+to+create+user+routines and Calling+a+routine+from+a+Job
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: IF/else statement usage in tMap

Hi All....
I am facing problem in 'if then elsif condition'
as while if  statement is true at that time else also execute and it directly execute else statement instead of elsif statement.
Moderator

Re: IF/else statement usage in tMap

Hi salvekaran_1,
I am facing problem in 'if then elsif condition'
as while if  statement is true at that time else also execute and it directly execute else statement instead of elsif statement.

Could you please set an example for your requirment?
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: IF/else statement usage in tMap

Hello Sabrina, 
I wanna define multiple conditions in tMap Component just like that following example,
if  Employees.Department_id != Department.Department_id  Then Print "Department ID is not valid" 
if  Employees.Department_id == Department.Department_id  Then Print  "null"
if  Employees.Job_Id != Jobs.Job_Id  Then Print " Job ID is not Valid"
if  Employees.Job_Id == Jobs.Job_Id  Then Print "null"
I' wrote this type of condition in tMap Expression , is it right ya wrong please notify me. 
row1.DEPARTMENT_ID!=row2.DEPARTMENT_ID ? "Error_1" :
row1.DEPARTMENT_ID ==row2.DEPARTMENT_ID ? null :
row1.JOB_ID !=row3.JOB_ID ? "Error_2" :
row1.JOB_ID ==row3.JOB_ID ? null :
row1.HIRE_DATE != row1.HIRE_DATE)? "Error_3" :null 
Thanks
Kiran Salve
ValuD Software's
One Star

Re: IF/else statement usage in tMap

Hi Kiran,
Seems everything ok but  add row1.HIRE_DATE.compareTo(row2.HIRE_DATE)!=0  in your expression.
Then your Expression became  like below after include  above logic.
row1.DEPARTMENT_ID!=row2.DEPARTMENT_ID ? "Error_1" :
row1.DEPARTMENT_ID ==row2.DEPARTMENT_ID ? null :
row1.JOB_ID !=row3.JOB_ID ? "Error_2" :
row1.JOB_ID ==row3.JOB_ID ? null :
row1.HIRE_DATE.compareTo(row2.HIRE_DATE)!=0? "Error_3" :null 
Regards,
Kumar

Re: IF/else statement usage in tMap

Hi salvekaran_1,

                         I think u meant to say that, filtering based on ur 5 condition using expression filter ?? if so ur doing it in wrong way
One Star

Re: IF/else statement usage in tMap

try below statement

Employees.Department_id != Department.Department_id? "Department ID is not valid" : Employees.Department_id == Department.Department_id? "null": Employees.Job_Id != Jobs.Job_Id? " Job ID is not Valid":Employees.Job_Id == Jobs.Job_Id?"null":"null"
One Star

Re: IF/else statement usage in tMap

Hi Umesh,
I tried above statement in tMap Expression but it's not working properly please check below screenshot
One Star

Re: IF/else statement usage in tMap

Hi,
I have situation in tMap.
I have 2 columns STARTDATE & ENDDATE . i just want to validate that STARTDATE should not be greater than ENDDATE &
ENDDATE should not be less than STARTDATE. 
WILL YOU PLEASE HELP ME TO IMPLEMENT THIS LOGIC.
Both columns are type DATE .
Thanx in advance.
Smiley Happy
One Star

Re: IF/else statement usage in tMap

you can use below function to check whether your date is greater than End date or not. 
Operation: StartDate>EndDate && EndDate>StartDate
row4.STARTDATE != null && (row2.STARTDATE.before(row4.ENDDATE)) 

This will check whether Start Date is before ENDDATE. that means it is lesser than EndDate. 
You can check other Date functions here. talend-date-routine-function-with-examples
Moderator

Re: IF/else statement usage in tMap

Hi Shri_Kul1,
You can create a tMap variable to carry out the logic on the dates and then use that variable to direct the flow to the output table you want.
Please see my screenshot for detail. Feel free to let us know if it is OK with you.
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: IF/else statement usage in tMap

Hi Sabrina & Umesh ,
Actual scenario is .... the fields i have mentioned  (STARTDATE    &    ENDDATE) are coming from the same table , there is no look up table , but there is 2 target table that are for good data and for bad data, 
in Bad data table there should be only null or the data which is failed at the validation .
so in this case i want "Start Date should not be greater than End date " & "End date should be greater than Start Date" @ the Bad table.
Fields datatype is "Date"  in source table .
waiting for reply...!!!!Smiley Happy
Thanks
One Star

Re: IF/else statement usage in tMap

In that case you can use below statement respectively for each good and bad flow. 
This one is for Good Data. 
row4.STARTDATE != null && (row2.STARTDATE.before(row4.ENDDATE)) 

And this one is for bad data. 
(row4.STARTDATE != null && (row2.STARTDATE.after(row4.ENDDATE))) || row4.STARTDATE == null

Hope this will solve your problem. 
One Star

Re: IF/else statement usage in tMap

Its Working ....
Thanx Umesh...Smiley Happy
One Star

Re: IF/else statement usage in tMap

Great... it worked for you! 
One Star

Re: IF/else statement usage in tMap

Hi Sabrina & Umesh,
I have situation ,
I have column which should have only TRUE & FALSE as values but for some records this column having other (1, yes, no...) & null also,
so i want to filter out those records which has other value for this column & i want to put FALSE instead of null only for this column
& if this column is not TRUE or FALSE or Null then it should print as "Value is Different"... plz see below Table for your reference .This is my source data...
ID    BL_Values
1    TRUE
2    FALSE
3    12
4    Yes
5    TRUE
6    TRUE
7    FALSE
8    FALSE
9   
10   
One Star

Re: IF/else statement usage in tMap

Hi,
I have an issue with the below logic. It is always loading "Processing" regardless for any values of row1.status.
Could anyone help me with the below logic where I am missing
row1.status == "Closed" ? "Complete" : "Processing"
Regards
Saket Krishna
One Star

Re: IF/else statement usage in tMap

Hi Sabrina & Umesh,
I have situation ,
I have column which should have only TRUE & FALSE as values but for some records this column having other (1, yes, no...) & null also,
so i want to filter out those records which has other value for this column & i want to put FALSE instead of null only for this column
& if this column is not TRUE or FALSE or Null then it should print as "Value is Different"... plz see below Table for your reference .This is my source data...
ID    BL_Values
1    TRUE
2    FALSE
3    12
4    Yes
5    TRUE
6    TRUE
7    FALSE
8    FALSE
9   
10   

if VL_Values is a string datatype not Boolean-
BL.Values == null ? "FALSE " : ("TRUE".equalsIgnorecase(BL.Values) || "FALSE".equalsIgnorecase(BL.Values) ? BL.Values : "Value is different")
One Star

Re: IF/else statement usage in tMap

I have a problem with "if-then-else" statement in tMap.
I have created a simpel text file. One of the lines are
0;NL6655/0;NL1091/0;Beschermde Gegevens;Beschermde Gegevens;60,00;17551,50;15;241,33;2008-08-06;0;
I reformated the second column. I have splitted the value NL6655 to NL as code and 6655 as fmid.
I am trying to expand the 6655 with 2 leading 0 so the fmid will be 006655.
But if the code is not NL then it should keep it value.
What I have done is in the tMap Variable the following.

StringHandling.TRIM(row3.code) == "NL" ? String.format("%06d", Integer.parseInt(row3.fmidnum))
:row3.fmidnum .
But then the result is :
.----+----+-------+---------+-------------------+-----+--+--+-----+---+--------.
|                                  tLogRow_2                                   |
|=---+----+-------+---------+-------------------+-----+--+--+-----+---+-------=|
|line|code|fmidnum|sponsor  |contractor         |adres|pp|gp|level|sal|inactive|
|=---+----+-------+---------+-------------------+-----+--+--+-----+---+-------=|
|0   |NL  |6655   |NL1091/0 |Beschermde Gegevens|     |  |  |null |   |null    |.
So no leading zero's before 6655 what I expect.
When I change the code tMap code to :
StringHandling.TRIM(row3.code) == StringHandling.TRIM(row3.code) ? String.format("%06d", Integer.parseInt(row3.fmidnum))
:row3.fmidnum
I get the following result.
|                                  tLogRow_2                                   |
|=---+----+-------+---------+-------------------+-----+--+--+-----+---+-------=|
|line|code|fmidnum|sponsor  |contractor         |adres|pp|gp|level|sal|inactive|
|=---+----+-------+---------+-------------------+-----+--+--+-----+---+-------=|
|0   |NL  |006655 |NL1091/0 |Beschermde Gegevens|     |  |  |null |   |null    |
Their are multiple values for code like XN, BE, NL.
What do i wrong.
Thanks for you reply
Four Stars

Re: IF/else statement usage in tMap

Hi msnoeck,
sorry but probably you miss a couple of part from your code (extract the code, extract number portion of the string,...).
This is a working version of your formula:
StringHandling.LEFT(StringHandling.TRIM(strTest),2).equals("NL") ? String.format("%06d", Integer.parseInt(strTest.substring(2,strTest.indexOf("/"))))
bye
One Star

Re: IF/else statement usage in tMap

hi Sabrina,
 Good day..!
I had a problem can we use assignment operator for non primitive data types...
consider below screen shot, the condition is ((row2.Address_2) == null )?"A":"B" into the column Checking which has been declared as string...But the condition is not working. can any one help me. I got Strucked...


Thanks 
Blessey
Ten Stars

Re: IF/else statement usage in tMap

The condition looks fine. Are you sure that the Address2 value is not an empty String instead of NULL?
Rilhia Solutions
One Star

Re: IF/else statement usage in tMap

hi rhall,
Nice to see you reply...i just tried with (row3.Address_2.contains(" "))?"A":"B" ..... it works fine
thanks
Blessey
Four Stars

Re: IF/else statement usage in tMap

How to compare and load data between two tables using talend and id with higher value should be loaded from the source ,What condition should i use to do so