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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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!!!

Labels (2)
32 Replies
Anonymous
Not applicable
Author

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)))
Anonymous
Not applicable
Author

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.:-)
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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 0683p000009MACn.png
Anonymous
Not applicable
Author

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!
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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