Sandra Renaud [https://community.jboss.org/people/joploya] created the discussion
"Re: Getting process instance variables"
To view the discussion, visit: https://community.jboss.org/message/804237#804237
--------------------------------------------------------------
Hello,
I try this solution to retrieve processInstance varaibles but I got a NullPointerException.
Before I tried this solution : https://community.jboss.org/message/798123#798123 https://community.jboss.org/message/798123 without success.
Eric how can you do to have a not null processInstance please?
Here my code :
/**
         * This method allow to access object store in
         * process instance variables
         * @return an object link to the processInstance
         */
         public Object getProcessVariable(Long processInstanceId, Long taskId, String key){
                   final Long piId = processInstanceId;
                   Object retObj = null;
                   WorkflowProcessInstance wpi = (WorkflowProcessInstance)kSession.getProcessInstance(processInstanceId);
                   if((wpi == null) || (wpi.getVariable(key) == null)){
                             //method1 : retrieve content map variables
                             Task task = getTaskService().getTask(taskId);
                             TaskData tData = task.getTaskData();
                             Long docContentId = tData.getDocumentContentId();
                             System.out.println("process instance is null, doc contentId = "+docContentId);
                             Content content = getTaskService().getContent(docContentId);
                             Map<?, ?> variables = ((Map<?, ?>) ContentMarshallerHelper.unmarshall(content.getContent(), null));
                             //display keys
                             System.out.println("content map keys :");
                             for(Object o : variables.keySet()){
                                       System.out.println(o.toString());
                             }
                             System.out.println("and key search is "+key);
                             /*
                             content map keys :
                                       ActorId
                                       Skippable
                                       TaskName
                                       GroupId
                             key search is formId
                             */
                             retObj = variables.get(key);
                             //method 2 : retrieve the the variable Scope
                             Map<String, Object> variables2 = kSession.execute(new GenericCommand<Map<String, Object>>(){
                                       @Override
                                       public Map<String, Object> execute(Context context) {
                                                 StatefulKnowledgeSession ksession = ((KnowledgeCommandContext) context).getStatefulKnowledgesession();
                                                 ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.getProcessInstance(piId);
                                                 VariableScopeInstance variableScope = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE); // ==> NullPointerException
                                                 Map<String, Object> variables = variableScope.getVariables();
                                                 return variables;
                                       }
                             });
                             //display keys
                             System.out.println("variables keys :");
                             for(Object o : variables2.keySet()){
                                       System.out.println(o.toString());
                             }
                             System.out.println("key search is "+key);
                             retObj = variables2.get(key);
                   }else{
                             retObj = wpi.getVariable(key);
                   }
                   return retObj;
         }
Thanks and Best Regards.
--------------------------------------------------------------
Reply to this message by going to Community
[https://community.jboss.org/message/804237#804237]
Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]