4

I want to set a context variable depending from a TalendDate routine in the "value as Table" tab which would allow me to select the day of yesterday : TalendDate.formatDate("yyyy-MM-dd", TalendDate.addDate(TalendDate.getCurrentDate(),-1,"dd"))

It does not work, I do not have any returns when I include in the selection as date('"+context.date+"').

Could you please tell me what I am missing ? I would be very grateful if you could help me with that.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3463383
  • 43
  • 1
  • 2
  • 6

2 Answers2

4

You can use the tContextLoad component to create key value pairs of contexts to be used in your job or child jobs.

In your case you would want to use a tFixedFlowInput component to specify what you wanted (or this could be any input really but this is just a way of hard coding it directly to the job without needing an external resource) and then link that to the tContextLoad component with a schema of key and value like so:

Defining context variables at run time

You'll want to use your original code snippet as the value in your tFixedFlowInput component:

tFixedFlowInput configuration

In the first screenshot above I've also added a tContextDump component and connected it to a tLogRow so you can see the available contexts to the job:

Output of tContextDump

ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
1

You should create a global variable, then in a tJava assign a value to it.

Or according to your current situation, in tJava paste the following:

context.date = TalendDate.formatDate("yyyy-MM-dd", TalendDate.addDate(TalendDate.getCurrentDate(),-1,"dd"));

Then you can use your variable. It won't be a java Date type, but a string.

Balazs Gunics
  • 2,017
  • 2
  • 17
  • 24
  • 1
    As a general rule of thumb I would avoid doing things in a tJava component unless you absolutely have to. The whole point of using an ETL engine is the metadata that it contains and the fact that it's more obvious to understand why things are doing what they are. Even with simple code snippets you lose some of this benefit. – ydaetskcoR Apr 16 '14 at 12:50
  • I agree with you, however this simple solution can save you a lot of time. And the user wanted to have a context variable to store yesterdays date. Also extending this solution makes to have default dates which can be overridden, like: if(context.date == null) {context.date = TalendDate.formatDate("yyyy-MM-dd", TalendDate.addDate(TalendDate.getCurrentDate(),-1,"dd"));} This way you could call your job with any kind of load date. – Balazs Gunics Apr 16 '14 at 14:24