My goal here is to access an execution variable of type pdf that i uploaded earlier, then to send this pdf as an attachment in an email using a Java class referenced in a service task.

Cockpit Variables

^the pdf I'm targeting is the "proofOfRequestFile". It's binary data is in "pdf"

Attempt 1:

        FileValue pdfVariable = (FileValue) delegateExecution.getVariable("proofOfRequestFile");

Returns error:

class java.io.ByteArrayInputStream cannot be cast to class org.camunda.bpm.engine.variable.value.FileValue (java.io.ByteArrayInputStream is in module java.base of loader 'bootstrap'; org.camunda.bpm.engine.variable.value.FileValue is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @4f7d0008)

PS: Using (File) instead of (FileValue) returns the same error

Attempt 2:

byte[] pdfBytes = (byte[]) delegateExecution.getVariable("proofOfRequestFile");

Returns error:

class java.io.ByteArrayInputStream cannot be cast to class [B (java.io.ByteArrayInputStream and [B are in module java.base of loader 'bootstrap')

Attempt 3:

I tried using the binary variable containing the pdf (shown in picture above)

        byte[] pdfBytes = (byte[]) execution.getVariable("pdf");

Returns error: class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader 'bootstrap')

1

There are 1 answers

0
Jan Rohwer On

Use the method getVariableTyped of the DelegateExecution instead. You then get a typed version of the variable, where you can explore the type and access the correct value. It also supports a second parameter to deserialize the value (I'm not sure if that's the default for the method with just one parameter). In your third attempt the type of the variable is String, so you have to access it as a string and decode that.