Q&A: The One Where You’re Retrieving Process Variables from an External Task Worker

How do you go about retrieving a variable in your process instance that you’re not sure what or where it is? Let us show you.
By
Q&A: JSON requests
  • Blog
  • >
  • Q&A: The One Where You’re Retrieving Process Variables from an External Task Worker
TOPICS

30 Day Free Trial

Bring together legacy systems, RPA bots, microservices and more with Camunda

Sign Up for Camunda Content

Get the latest on Camunda features, events, top trends, and more.

TRENDING CONTENT

You know there’s a variable in your process instance, but you’re not sure what, or where it is. The model’s documentation says it’s there, but you don’t know what the value is. So, how do you go about retrieving it? For instance, your service sends out an email as part of the process, and you know the email address should be a variable within the process instance somewhere, but you can’t find it. 

Turns out, we hear about this scenario a lot. There are a few ways to retrieve the variable – if it even exists. 

Does the process variable even exist?

That’s really the first question to ask, isn’t it? If you attempt to retrieve and then use a process variable that doesn’t exist in the first place, you’ll have a whole host of other problems. These problems can be worse than what you started with, by introducing runtime errors, null pointer exceptions, and the like.

We previously covered how to handle a non-existent variable, so we won’t cover that again, but I encourage you to go back and read that blog post.

So the variable exists, but how do we get at it?

As mentioned, there are several ways to retrieve the variable. First, you can get all the variables in the process and then go through them all looking for the one you want. Take the following code snippet:

const { Variables } = require("camunda-external-task-client-js");

client.subscribe("topicName", async function({ task, taskService }) {
// Given:
// {
//    score: { value: 5, type: "integer", valueInfo: {} }
//    isWinning: { value: false, type: "boolean", valueInfo: {} }
// }
const values = task.variables.getAll();
console.log(values);

This returns the following:

{  score: 5, isWinning: false }

The call to `getAll() will, as the name implies, get all of the process variables in the process. Then, you can go through them one at a time to get the one you want. However, this isn’t very efficient, especially if you plan on doing it a lot.

However,  you can use much the same approach to get a single process variable. See the code snippet below:

// Given:
// score: { value: 5, type: "integer", valueInfo: {} }
const score = variables.get("score");
console.log(score);

 This prints out the following:

5

Again, you should be sure the variable exists before you try this, as doing this for a variable that doesn’t exist will result in a runtime error.

*Bonus content* What to do with the variable once you have it?

You’ve retrieved the variable, so what do you do with it? Additionally, how do you put it back with a new value (assuming that your process will alter the value as part of its processing)?

// Given:
// score: { value: 5, type: "integer", valueInfo: {} }
var score = variables.get("score");
console.log(score);
score += 10;
variables.set("score", { value: score, type: "integer", valueInfo: {} });
console.log(variables.get("score"));

In the code snippet above, we’re getting the existing score variable, increasing it by 10, and then returning that to the process. Subsequent parts of the process will see the value of 15, not the original value of 5. The above code will yield the following:

5
15

As you can see, you retrieved the variable, changed it, and put it back. When you retrieved the variable for the second time (using that second call to `variables.get(“score”), you got the changed value.

Try All Features of Camunda

Related Content

Learn from the experience of one organization that recently migrated from Camunda 7 to Camunda 8, including why they did it and how they prepared.
Level up your workflows with AI suggestions for increased efficiency from Camunda Copilot.
Make your business forms easier for customers to use and easier for staff to process with Camunda Forms.