One Star

Passing a context variable from a job to a subjob and back

Try to
* pass a context variable from a job to a subjob
* change it in the subjob
* and pass it back to the main job.
You will find a working solution at the end. I'm working with TOS-version 4.2.0.
A first (obvious) try does NOT work:
a) In the subjob the variable arrives as null.
b) Having set the context variable in the subjob to another value and returned back to the main job the variable is again null!
--------------------------------------------------
My main job has a context variable named errorMsg (Type String) and is built as follows with 4 components:
tJava
|
tMsgBox
|
tRunJob
|
tMsgBox
--------------------------------------------------
My subjob has a context variable named errorMsg (Type String) and is built as follows with 3 components:
tMsgBox
|
tJava
|
tMsgBox
--------------------------------------------------
When running the main job the 4 MsgBoxes show the following values:
1) OK << main job
2) null << subjob start
3) Subjob << subjob end
4) null << returned to main job
---------------------------------------------------
the next try improves the situation:
When defining a context variable errorMsg with value ((String) context.getProperty("errorMsg"))
on tRunJob instead of checking "Transmit whole context" then while running the main job the 4 MsgBoxes show the following values:
1) OK << main job
2) OK << subjob start
3) Subjob << subjob end
4) OK << returned to main job
Conclusion: "Transmit whole context" does not work (at least not the way I expected)
Remaining problem: How to pass back the context variable?
---------------------------------------------------
Another step leads to the final solution:
In the main job add a component tJavaRow and drag a main link from the tRunJob component to it:
tRunJob ----> tJavaRow
In tJavaRow add a schema column named errorMsg and synchronize columns.
As code enter the following:
output_row.errorMsg = ((String) context.getProperty("errorMsg"));
In the subjob add 2 components tJavaRow and tBufferOutput and link them with the last MsgBox component by main links - as follows:
tMsgBox -----> tJavaRow ----> tBufferOutput
Remarks:
1) tJavaRow needs an input flow. We use tMsgBox as dummy input.
2) It seems that tBufferOutput must be the very last component in the subjob! Otherwise its values are not returned to the main job!
In the new tJavaRow component add a schema column named errorMsg and synchronize columns.
As code enter the following:
output_row.errorMsg = ((String) context.getProperty("errorMsg"));
Running the main job the 4 MsgBoxes now show the following values:
1) OK << main job
2) OK << subjob start
3) Subjob << subjob end
4) Subjob << returned to main job
9 REPLIES
One Star

Re: Passing a context variable from a job to a subjob and back

Hi,
I've updated the code, this then passes the context variables as expected. (using Transmit whole context), i'm not sure about the context.get/setProperty methods you were using...
To get the values back to the parent job use tbufferoutput component.
---
My main job has a context variable named errorMsg (Type String) and is built as follows:
tJava
|
tMsgBox
|
tRunJob
|
tMsgBox
--------------------------------------------------
My subjob has a context variable named errorMsg (Type String) and is built as follows:
tMsgBox
|
tJava
|
tMsgBox
hope this helps.
One Star

Re: Passing a context variable from a job to a subjob and back

sorry with tbuffer component. this should give you what you're after.
My main job has a context variable named errorMsg (Type String) and is built as follows:
tJava
|
tMsgBox
|
tRunJob flow out of this to tjavarow , click copy child job schema (once child job is setup)
|
tMsgBox
--------------------------------------------------
My subjob has a context variable named errorMsg (Type String) and is built as follows:
tMsgBox
|
tJava
|
tMsgBox
|
tjava flow to tbufferOutput - schema with errorMsgSmiley Frustratedtring
One Star

Re: Passing a context variable from a job to a subjob and back

Thanks Chris.
Your
context.errorMsg = "OK";
might be shorter than my
context.setProperty("errorMsg", "OK");
At the beginning of my subjob trials I got an error message while using "context.errorMsg" but maybe due to another error or a misunderstanding of the error message from my side.
One Star

Re: Passing a context variable from a job to a subjob and back

I tried your suggestion with "context.errorMsg" but it does not work.
The 2nd MsgBox shows null!
One Star

Re: Passing a context variable from a job to a subjob and back

have you rechecked Transmit whole context?
One Star

Re: Passing a context variable from a job to a subjob and back

Hi,
It doesn't work for me neither. I checked "transmit hole context", but it seems that in the sub-job second tjava component, the context variable still have its original value, as if the first tjava statement was canceled.
One Star

Re: Passing a context variable from a job to a subjob and back

This blog post uses an alternative technique that doesn't involve schemas. Define a context variable as "Object", set the variable to a Map in the parent job, link the context variable Map to the child job, unpack and set a property on the Map in the child job. The context of the Map -- a context variable -- are now available to the parent job.
http://bekwam.blogspot.com/2011/05/passing-parameters-and-variables-to.html
-Carl
Employee

Re: Passing a context variable from a job to a subjob and back

Thanks for the link to the post Carl!
Best,
Pcoffre.
Four Stars

Re: Passing a context variable from a job to a subjob and back

This blog post uses an alternative technique that doesn't involve schemas. Define a context variable as "Object", set the variable to a Map in the parent job, link the context variable Map to the child job, unpack and set a property on the Map in the child job. The context of the Map -- a context variable -- are now available to the parent job.
http://bekwam.blogspot.com/2011/05/passing-parameters-and-variables-to.html
-Carl

This is wonderful! Very very helpful. Thank you for posting!