Not able to receive process variables from the previous pool

Dear Camunda Community,

Currently I’m using Zeebe NodeJS client for implementing job workers. I am having an issue of not receiving any process variables from the previous pool.

Variables that I have in the previous pool:

Not received any variables from the previous pool:

I believe I need to publish a message which I’m doing already from the previous pool and then receive it in the next pool, there I should be able to receive the variables using “Message start event” with the same “message name” that I published from “Send production order”. Here is the code for “Send production order” worker:

const { ZBClient, Duration } = require('zeebe-node')
const uuid = require('uuid');
const sendCustomerOrderForProduction = new ZBClient();
sendCustomerOrderForProduction.publishMessage({
	correlationKey: 'quantity',
	messageId: uuid.v4(),
	name: 'juststart',
	variables: { valueToAddToProcessVariables: 'here', status: 'PROCESSED' },
	timeToLive: Duration.seconds.of(10), // seconds
})
sendCustomerOrderForProduction.createWorker({
	taskType: 'sendCustomerOrderForProduction',
	taskHandler: handler,
})
function handler(job) {
	console.log('Task variables', job.variables)
    console.log("Sending customer order now for production...");
    console.log("Number of bicycles to be produced: ", job.variables.quantityNeededForProduction);
	// Task worker business logic goes here
	const updateToBrokerVariables = {
		updatedProperty: 'newValue',
	}
	return job.complete(updateToBrokerVariables)
}
module.exports = sendCustomerOrderForProduction;

Here is my BPMN diagram:
bicycle-process-model.bpmn (28.0 KB)

What am I doing wrong please? Any help regarding receiving variables would be highly appreciated. Thank you! :smiley:

Kind regards,
Rahib B.

Since each pool is a distinct process, you are not expected to be able to retrieve process variables from a different process.

Your message contains the variables “valueToAddToProcessVariables” and “status”, which you can see are being set in the new process. If you need other variables, then you would need to pass them as part of the message.

Hi @GotnOGuts,

Thank you for your reply. Could you please share an example how can I do that? I have been trying to do it for hours but sadly no progress. :frowning:

So let’s say I want job.variables.quantityNeededForProduction variable to pass as part of the message, how can I achieve that please?

How can I access the variable in the sendCustomerOrderForProduction.publishMessage({}) object after the job has been completed by the worker sendCustomerOrderForProduction.createWorker({}) object?

Thanks!

From your own code…

If you set the variable value to the value from the Sending Process, it will be included in the message to the Receiving Process.

Well, answering to my own thread after spending days in figuring out a way of sending process instance from one pool to another pool using “Send Task” with all the previous process variables. Thought it might help someone trying to implement something similar :slight_smile:, here is the solution:

const { ZBClient, Duration } = require('zeebe-node');
const zbc = new ZBClient();
const uuid = require('uuid');

//Declare all your process variables globally here that you are using
let quantityNeededForProduction = ''; //example process variable from the pool you are sending the process from

const messagePayload = {
  messageId: uuid.v4(),
  name: 'receiveProductionOrder', //Global message reference of "Message Start Event" inside Camunda Modeler
  variables: {
    // Include any other variables that you want to pass to the workflow
  },
  timeToLive: Duration.seconds.of(10), // seconds
};

const sendCustomerOrderForProduction = zbc.createWorker({
  taskType: 'sendCustomerOrderForProduction',
  taskHandler: handler,
  onReady: () => sendCustomerOrderForProduction.log('Job worker started successfully!')
});

function handler(job) {
quantityNeededForProduction = job.variables.quantityNeededForProduction;
console.log("\nNumber of bicycles to be produced: ", job.variables.quantityNeededForProduction);

  // Task worker business logic goes here
  updateToBrokerVariables = {
    quantityNeededForProduction: quantityNeededForProduction,
  };

  // Include the updated variables in the message payload for the next step in the workflow
  const updatedMessagePayload = {
    ...messagePayload,
    variables: {
      ...messagePayload.variables,
      ...updateToBrokerVariables,
    },
  };

  // Publish the updated message to the workflow
  zbc.publishMessage(updatedMessagePayload);

  // Complete the current job
  job.complete(updateToBrokerVariables);
  console.log("\nSending customer order for production: ", updatedMessagePayload)
}
module.exports = sendCustomerOrderForProduction;

You appear to have a bug in this, in that you are providing a variables block in both the Job JSON template (messagePayload) and in the “Message to Send” block (updatedMessagePayload)

As you discovered (which is what was advised in the prior post) - the solution is to pass the variables from the initiating process to the destination process within the message.

Corrected code for sending task to message start event:

const ZB = require('zeebe-node')

const zbc = new ZB.ZBClient(); // localhost:26500 || ZEEBE_GATEWAY_ADDRESS

let orderID = ''; //Example variable

const sendCustomerOrderForProduction = zbc.createWorker({
  taskType: 'sendCustomerOrderForProduction',
  taskHandler: handler,
  onReady: () => sendCustomerOrderForProduction.log('Job worker started successfully!')
});

function handler(job) {
  const correlationValue = 124; //To correlate send and receive tasks. Same variable goes inside the process diagram
  orderID = job.variables.orderID; 

  zbc.publishStartMessage({
    name: 'receiveProductionOrder', //Global message reference of message start event
    variables: {
      correlationValue,
      orderID: orderID,
    },
  })

  return job.complete({ correlationValue: correlationValue }); //Sending correlationValue upon job completion
}

module.exports = sendCustomerOrderForProduction;

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.