Search code examples
javascriptwso2wso2-esbwso2-enterprise-integratorwso2-micro-integrator

how i can do if between two dynamic variable in mediator script esb wso2


I have this if condition but does not work if the value is static,when I annee2 and annee1 static for example <2023 and >=2019 it works : here is my code:

<script language="js"><![CDATA[var table = [
{"TABLE_NAME":"20220917"},
{"TABLE_NAME":"20221018"},
{"TABLE_NAME":"20221211"},
{"TABLE_NAME":"20230417"}
]; 
var date2 ="20221211";
var date1 ="20191018";
var annee1=date1.substr(0, 4);
var annee2=date2.substr(0, 4);
for (var i=0; i<table.length;i++){
if(parseInt(table[i].TABLE_NAME.substr(0, 4))<parseInt(annee2) && (parseInt(table[i].TABLE_NAME.substr(0, 4))>=parseInt(annee1))){
str = str +" select * from "+"`"+table[i].TABLE_NAME+"`"+" " +union + "";
}}]]></script>

I get as error: Query was empty


Solution

  • In the given example, the value getting set to annee2 is 2022 since the date2 is set to 20221211. But your Table list doesn't have dates before 2022. Hence in the given example the if condition is never true.