Problem with setting the variable

Hello everybody,

I am currently working on a small Camunda program to familiarize myself with it.
My current problem is this:
In a service task, I save an entity in the Database, created by typing. So far so good, that works. The next task is a UserTask in which the Entity should be reissued. To realize this, I have the ID of the created entity, by means of DelegateExecution, stored in a variable (in the ServiceTask). In the following UserTask I want to read this variable again but it is no longer available.

What can this be?
I have already realized other small programs in Camunda before and it worked there.

Thank you in advance,
David

Hi @David_Krenz,

how did you save the variable? Is the delegate in a seperate process called by a call activity?

execution.setVariable("name", somevalue) is enough to save the value in the process instance.

If you use a call activty, you should fill the out mapping.

Hope this helps, Ingo

Hello @Ingo_Richtsmeier,

thank you for your prompt reply.

Thats my BPMN:
image

For the Service Task ist this my Java Class:

@Named
@Stateless
public class SpeichernController implements Serializable {

    @Inject
    private AusschreibungService ausschreibungService;

    public void speichern(DelegateExecution execution) throws IOException {
        Map<String, Object> processVariables = execution.getVariables();

        Ausschreibung ausschreibung = new Ausschreibung();
        ausschreibung.setName((String) processVariables.get("name"));
        ausschreibung.setVon((LocalDate) processVariables.get("von"));
        ausschreibung.setBis((LocalDate) processVariables.get("bis"));
        ausschreibung.setAnforderungen((List<Anforderungen>) execution.getVariable("anforderungen"));

        execution.removeVariables(processVariables.keySet());
        ausschreibung = ausschreibungService.speichern(ausschreibung);
        execution.setVariable("id", ausschreibung.getId()); // There I set the variable
        ServerLogger.log("ID -> " + execution.getVariable("id"));
    }

}

I save the variable with execution.setVariable(…);

For the usertask I have created a JSF with a BackingBean:
This ist the Methode witch ist called from the JSF Page:

 public Ausschreibung getAusschreibung() {
        Integer id = businessProcess.getVariable("id"); // There i want get the variable
        ServerLogger.log("ID -> " + id);
        
        if (ausschreibung == null) {
            ausschreibung = ausschreibungService.find((int) businessProcess.getVariable("id"));
        }

        ServerLogger.log(ausschreibung.toString());

        return ausschreibung;
    }

In the Top of the Backing BEan i Inject he BusinessProcess. Then i want to get the variable with i have set in the Usertask, but the variable returns null.

I hope the explanation is understandable,
David

What is the output of the logging statements?

The output of the ServerLogger.log ,at the Service Task, is 1. It is the correct id.
In the User Task is the output is null.

The output of the exception is a Null Pointer Exception.
The Exception flies in this Line in the User Task:
ausschreibung = ausschreibungService.find((int) businessProcess.getVariable("id"));

You could double check the variable value in cockpit.

I assume it’s a CDI problem…

This is the Annotation from the Backing Bean:

@Named
@ConversationScoped
public class AusschreibungErstellenBean implements Serializable {

I haven’t implemented JSF with CDI-Beans by myself.

Beyond checking the value in Cockpit, I haven’t any further suggestions…

that is the Cockpit view from a running Instanz:

Yeah, the service task works as expected. You can concentrate on the CDI setup.

Thank you for your help in identifying a problem.
Do you have any other suggestion to resolve my problem?

When does this line of code get called? It looks like you’re deleting all the variables in the process.

With this line I delete only the variables from the map.
Map <String, Object> processVariables = execution.getVariables ();

I delete all the variables I do not need anymore.
I tried to comment on the line, but that does not help.

And in the line below the line that sets the variable, the varable is present.

 execution.setVariable("id", ausschreibung.getId()); // There I set the variable
 ServerLogger.log("ID -> " + execution.getVariable("id")); // There I get the variable and it displays 1

Hi @David_Krenz

May you attach your xhtml page & your backing bean java code?

This is the backing bean from the usertask:

package org.camunda.bpmn.bewerbung.bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.camunda.bpm.engine.cdi.BusinessProcess;
import org.camunda.bpm.engine.cdi.jsf.TaskForm;
import org.camunda.bpmn.bewerbung.logger.ServerLogger;
import org.camunda.bpmn.bewerbung.model.Ausschreibung;
import org.camunda.bpmn.bewerbung.service.AusschreibungService;

@Named
@ConversationScoped
public class AusschreibungErstellenBean implements Serializable {

    @Inject
    private AusschreibungService ausschreibungService;

    @Inject
    private TaskForm taskForm;

    @Inject
    private BusinessProcess businessProcess;
    
    private Ausschreibung ausschreibung;

    public Ausschreibung getAusschreibung() {
        int id =  businessProcess.getVariable("id");
        ServerLogger.log("ID -> " + id);
        if (ausschreibung == null) {
            ausschreibung = ausschreibungService.find(id);
        }
        
        return ausschreibung;
    }

    public void submit()  {
        ausschreibung.setGenehmigt(false);
        ausschreibung = ausschreibungService.update(ausschreibung);
        try {
            taskForm.completeTask();
        } catch (IOException ex) {
          ServerLogger.log(Level.SEVERE, ex.getMessage());
        }
    }
}

and this is the xhtml page:

<!DOCTYPE HTML>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" 	
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <f:view>
        <f:metadata>
            <f:event type="preRenderView"
                     listener="#{camundaTaskForm.startProcessInstanceByKeyForm()}" />
        </f:metadata>
        <h:head>
            <title>Ausschreibung erstellen</title>
        </h:head>
        <h:body>
            <h2>Ausschreibungsanforderungen</h2>
            <h:form id="submitForm">
                <h:panelGrid columns="2">

                    <p:outputLabel for="name" value="Name"/>
                    <p:inputText id="name" value="#{ausschreibungErstellenBean.ausschreibung.name}" readonly="true" />

                    <p:outputLabel for="von" value="Von "/>
                    <p:calendar id="von" value="#{ausschreibungErstellenBean.ausschreibung.von}" converter="localDateTimeConverter" pattern="yyyy-mm-dd" mask="true" readonly="true" />

                    <p:outputLabel for="bis" value="Bis"/>
                    <p:calendar id="bis" value="#{ausschreibungErstellenBean.ausschreibung.bis}" converter="localDateTimeConverter" pattern="yyyy-mm-dd" mask="true" readonly="true"/>

                    <p:dataList value="#{ausschreibungErstellenBean.ausschreibung.anforderungen}" var="a" type="ordered">
                        <f:facet name="header">
                            Anforderungen
                        </f:facet>
                        #{a.anforderungen.text}
                    </p:dataList> 

                    <p:outputLabel for="anforderungen" value="Anforderungstext"/>
                    <label/>
                    <p:textEditor id="anforderungen" required="true" widgetVar="editor1" value="#{ausschreibungErstellenBean.ausschreibung.ausschreibungstext}" height="300" style="margin-bottom:10px"/>

                    <p:commandButton value="Clear" type="button" onclick="PF('editor1').clear();" icon="ui-icon-close" />

                </h:panelGrid>
                <h:commandButton type="button" id="submit_button" value="Ausschreibung abgeben" action="#{ausschreibungErstellenBean.submit()}" />
                <h:messages style="color:red;margin:8px;" /> 
            </h:form>
        </h:body>
    </f:view>
</html>

Hi @David_Krenz

In your xhtml, replace

<f:event type="preRenderView"
                     listener="#{camundaTaskForm.startProcessInstanceByKeyForm()}" />

with

<f:event type="preRenderView"
                     listener="#{camundaTaskForm.startTaskForm()}" />

Hello @hassang,
that solves my problem.
Thanks so much

What is the difference between the two events?

Hi @David_Krenz,

camundaTaskForm.startProcessInstanceByKeyForm() should be used in the start form

camundaTaskForm.startProcessInstanceByKeyForm() method which extracts the process definition key from the URL and starts a conversation for the start form.

camundaTaskForm.startTaskForm() should be used in subsequent task forms

camundaTaskForm.startTaskForm() method which extracts the task id from the URL and starts a conversation for the task form

Hi @hassang

Thanks for your help and the explanation.