Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
lucTalend
Contributor

tRest with dynamic body

Hey ,  

 

I want to create a flow to get data from API ,to made that , I need to create export definition first.
In export definition I need to put from what date range , data need to be prepared.

So I thought that the simplest way to archive that will be use tJavaRow component where inside I will do that code :

String todayDate = TalendDate.formatDate("yyyy-MM-dd", new Date());
String yesterday = TalendDate.addDate(todayDate,"yyyy-MM-dd",-1,"dd");
String Definition = "{\"name\": \"Bulk Activity Export - Email Clickthrough\",\"fields\": {\"ActivityDate\": \"{{Activity.CreatedAt}}\",\"ActivityType\": \"{{Activity.Type}}\",\"EmailAddress\": \"{{Activity.Field(EmailAddress)}}\",\"EmailWebLink\": \"{{Activity.Field(EmailWebLink)}}\",\"SubjectLine\": \"{{Activity.Field(SubjectLine)}}\"},\"filter\": \"'{{Activity.Type}}' = 'EmailClickthrough' AND '{{Activity.CreatedAt}}' >= '" + yesterday + "' AND '{{Activity.CreatedAt}}' < '" + todayDate + "'\"}";
globalMap.put("Definition", Definition);

 

But then I cannot pass "Definition" value to the tRest component.

Current , my flow looks like :

0683p000009MA4e.png

 

For now , I've get all values from tSetGlobalVar component , and for static export definition works without no problem.

 

How to resolve that problem ?

 

Thank you , 

Lucjan 

Labels (3)
1 Solution

Accepted Solutions
lennelei
Creator III

Hi,

 

first of all, some tips:

 

String todayDate = TalendDate.formatDate("yyyy-MM-dd", new Date());
String yesterday = TalendDate.addDate(todayDate,"yyyy-MM-dd",-1,"dd");

//you can use following Talend methods
String todayDate = TalendDate.getDate("yyyy-MM-dd");
String yesterday = TalendDate.addDate(TalendDate.getCurrentDate(),-1,"dd");
//which is equivalent to
String yesterday = TalendDate.addDate(new Date(),-1,"dd");

Now, regarding your question :

 

 

If I understand correctly, you want to produce the json changing only the dates according to the current day?

 

In that case, you could directly put some java code in the tRest body :

"{\"name\": \"Bulk Activity Export - Email Clickthrough\",\"fields\": {\"ActivityDate\": \"{{Activity.CreatedAt}}\",\"ActivityType\": \"{{Activity.Type}}\",\"EmailAddress\": \"{{Activity.Field(EmailAddress)}}\",\"EmailWebLink\": \"{{Activity.Field(EmailWebLink)}}\",\"SubjectLine\": \"{{Activity.Field(SubjectLine)}}\"},\"filter\": \"'{{Activity.Type}}' = 'EmailClickthrough' AND '{{Activity.CreatedAt}}' >= '" + TalendDate.addDate(TalendDate.getCurrentDate(),-1,"dd")+ "' AND '{{Activity.CreatedAt}}' < '" + TalendDate.getDate("yyyy-MM-dd")+ "'\"}";

 

However, it will be hard to maintain as the body grows.

 

I would prefer to use something like this :

 

tFixedFlowInput -row-> tWriteJSONField -row-> tFlowToIterate -iterate-> tRest ...

 

In the tFixedFlowInput, you create all the required data for the json (name, fields, and so one) with correct values.

You then create your JSON with the tWriteJSONField.

The tFlowToIterate is used to put the JSON in globalMap and you'll use this in your tRest.

 

And if your "Definition" comes from a database, you can replace the tFixedFlowInput with whatever data source you nedd.

View solution in original post

2 Replies
lennelei
Creator III

Hi,

 

first of all, some tips:

 

String todayDate = TalendDate.formatDate("yyyy-MM-dd", new Date());
String yesterday = TalendDate.addDate(todayDate,"yyyy-MM-dd",-1,"dd");

//you can use following Talend methods
String todayDate = TalendDate.getDate("yyyy-MM-dd");
String yesterday = TalendDate.addDate(TalendDate.getCurrentDate(),-1,"dd");
//which is equivalent to
String yesterday = TalendDate.addDate(new Date(),-1,"dd");

Now, regarding your question :

 

 

If I understand correctly, you want to produce the json changing only the dates according to the current day?

 

In that case, you could directly put some java code in the tRest body :

"{\"name\": \"Bulk Activity Export - Email Clickthrough\",\"fields\": {\"ActivityDate\": \"{{Activity.CreatedAt}}\",\"ActivityType\": \"{{Activity.Type}}\",\"EmailAddress\": \"{{Activity.Field(EmailAddress)}}\",\"EmailWebLink\": \"{{Activity.Field(EmailWebLink)}}\",\"SubjectLine\": \"{{Activity.Field(SubjectLine)}}\"},\"filter\": \"'{{Activity.Type}}' = 'EmailClickthrough' AND '{{Activity.CreatedAt}}' >= '" + TalendDate.addDate(TalendDate.getCurrentDate(),-1,"dd")+ "' AND '{{Activity.CreatedAt}}' < '" + TalendDate.getDate("yyyy-MM-dd")+ "'\"}";

 

However, it will be hard to maintain as the body grows.

 

I would prefer to use something like this :

 

tFixedFlowInput -row-> tWriteJSONField -row-> tFlowToIterate -iterate-> tRest ...

 

In the tFixedFlowInput, you create all the required data for the json (name, fields, and so one) with correct values.

You then create your JSON with the tWriteJSONField.

The tFlowToIterate is used to put the JSON in globalMap and you'll use this in your tRest.

 

And if your "Definition" comes from a database, you can replace the tFixedFlowInput with whatever data source you nedd.

lucTalend
Contributor
Author

Thanks @lennelei - this export definition will be the same , only dates need to be changed - so your solutions is great !