cancel
Showing results for 
Search instead for 
Did you mean: 

Java Script in Activiti

larsson7
Champ in-the-making
Champ in-the-making
Good morning everyone!


I have a problem, i desinged a simple workflow where the status of webcontent ist directly converted into the approves status. But it doesn´t work. Here is my javascript that i wrote: I want to catch the ID of a User and when this user makes a post, the web content should be directly approved.

<scriptTask id="scripttask1" name="Approve" scriptFormat="javascript">
<script><![CDATA[import com.liferay.portal.kernel.workflow.WorkflowStatusManagerUtil;
import com.liferay.portal.service.UserLocalServiceUtil;

user = Packages.com.liferay.portal.service.UserLocalServiceUtil.fetchUser(10382);

roles = user.getRoleIds();
status = "Deny";
for (i = 0; i < roles.length; i++) {
if (roles == 15) {
status = "Approve";
break;
}
}

WorkflowStatusManagerUtil.updateStatus(Packages.com.liferay.portal.kernel.workflow.WorkflowConstants.toStatus(status), workflowContext); ]]></script>
</scriptTask>

But i get the following error:

14:15:51,816 INFO [http-bio-8080-exec-5][WorkflowDefinitionManagerImpl:58] Try to deploy process DirectApprove
14:15:51,872 INFO [http-bio-8080-exec-5][WorkflowDefinitionManagerImpl:107] Process DirectApprove deployed with deployment ID 1516
14:15:51,873 INFO [http-bio-8080-exec-5][WorkflowDefinitionManagerImpl:109] 1 process definitions deployed
14:15:51,873 INFO [http-bio-8080-exec-5][WorkflowDefinitionManagerImpl:122] Process Definition Id for process process1 : process1:6:1519
14:16:02,323 ERROR [http-bio-8080-exec-7][WorkflowComparatorFactoryImpl:18] Method is not implemented
14:16:07,900 ERROR [http-bio-8080-exec-7][WorkflowComparatorFactoryImpl:18] Method is not implemented
14:16:24,158 INFO [http-bio-8080-exec-3][WorkflowInstanceManagerImpl:194] Start workflow instance process1 : 6
14:16:24,426 ERROR [http-bio-8080-exec-3][render_portlet_jsp:154] org.activiti.engine.ActivitiException: problem evaluating script: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "workflowContext" is not defined. (<Unknown source>#13) in <Unknown source> at line number 13
at org.activiti.engine.impl.scripting.ScriptingEngines.evaluate(ScriptingEngines.java:71)
at org.activiti.engine.impl.bpmn.behavior.ScriptTaskActivityBehavior.execute(ScriptTaskActivityBehavior.java:51)


Why is workflow context undefiened??

Thanks for your help!!!

Best Regards, Philipp
10 REPLIES 10

jbarrez
Star Contributor
Star Contributor
It seems to me you didn't set the 'workflowContext' variable in your process instance?

larsson7
Champ in-the-making
Champ in-the-making
Ok this could be the Problem, your right! I try to solve it and create a process variable at the start event.

But now i got another Problem.

<process id="process1" name="process1">
    <startEvent id="startevent1" name="Start">
      <extensionElements>
        <activiti:formProperty id="workflowContext" name="workflowContext" type="enum" required="true">
          <activiti:value id="Approve" name="Approve"></activiti:value>
          <activiti:value id="Deny" name="Deny"></activiti:value>
        </activiti:formProperty>
      </extensionElements>
    </startEvent>
    <scriptTask id="scripttask1" name="Approve" scriptFormat="javascript">
      <script><![CDATA[
  import Packages.com.liferay.portal.kernel.workflow.WorkflowStatusManagerUtil;
  import Packages.com.liferay.portal.service.UserLocalServiceUtil;
  
  user = Packages.com.liferay.portal.service.UserLocalServiceUtil.fetchUser(10382);

  roles = user.getRoleIds();
  status = "Deny";
  for (i = 0; i < roles.length; i++) {
   if (roles[i] == 15) {
    status = "Approve";
    break;
   }
  } 
  WorkflowStatusManagerUtil.updateStatus(Packages.com.liferay.portal.kernel.workflow.WorkflowConstants.toStatus(status), workflowContext);]]></script>
    </scriptTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" name="to Script Task" sourceRef="startevent1" targetRef="scripttask1"></sequenceFlow>
    <sequenceFlow id="flow2" name="to End" sourceRef="scripttask1" targetRef="endevent1"></sequenceFlow>
  </process>

Now i got the following error 😞 He said unknown source.

08:26:54,191 INFO  [http-bio-8080-exec-7][WorkflowDefinitionManagerImpl:58] Try to deploy process DirectApprove
08:26:54,241 INFO  [http-bio-8080-exec-7][WorkflowDefinitionManagerImpl:107] Process DirectApprove deployed with deployment ID 1728
08:26:54,251 INFO  [http-bio-8080-exec-7][WorkflowDefinitionManagerImpl:109] 1 process definitions deployed
08:26:54,251 INFO  [http-bio-8080-exec-7][WorkflowDefinitionManagerImpl:122] Process Definition Id for process process1 : process1:18:1731
08:26:57,231 ERROR [http-bio-8080-exec-7][WorkflowComparatorFactoryImpl:18] Method is not implemented
08:27:00,911 ERROR [http-bio-8080-exec-4][WorkflowComparatorFactoryImpl:18] Method is not implemented
08:27:12,921 INFO  [http-bio-8080-exec-4][WorkflowInstanceManagerImpl:194] Start workflow instance process1 : 18
08:27:13,021 ERROR [http-bio-8080-exec-4][render_portlet_jsp:154] org.activiti.engine.ActivitiException: problem evaluating script: sun.org.mozilla.javascript.internal.EvaluatorException: syntax error (<Unknown source>#2) in <Unknown source> at line number 2
at org.activiti.engine.impl.scripting.ScriptingEngines.evaluate(ScriptingEngines.java:71)

frederikherema1
Star Contributor
Star Contributor
Looks like you have a javascript syntax error. Try pasting the content of your javascript-task in a *.js-file in eclipse, perhaps that will help.

I'm not a javascript expert, but don't you need to add "var" before variable declarations (e.g.. user = …)?

larsson7
Champ in-the-making
Champ in-the-making
No you can do it without var…

I try another step like this:


  user = Packages.com.liferay.portal.service.UserLocalServiceUtil.fetchUser(10382);
 
  serviceContext = execution.getVariable("serviceContext");
  workflowContext = execution.getVariable("workflowContext");
   
  roles = user.getRoleIds();
  status = "Deny";
  for (i = 0; i < roles.length; i++) {
   if (roles[i] == 15) {
    status = "Approve";
    break;
   }
  } 
Packages.com.liferay.portal.kernel.workflow.WorkflowStatusManagerUtil.updateStatus(Packages.com.liferay.portal.kernel.workflow.WorkflowConstants.toStatus(status), workflowContext);]]

I got now error but the status of the web content i want to add is "pending" and not "approve". Maybe he don´t go into the loop? I ábsolutly don´t know what i do wrong 😞

Thanks for your help!

larsson7
Champ in-the-making
Champ in-the-making
Ok, now the context is approved.
With this code it works:


<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:smileysurprised:mgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:smileysurprised:mgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="process1" name="Einfacher Direct - Approve ">
    <startEvent id="pending" name="Start">
    </startEvent>
    <scriptTask id="scripttask1" name="Approve" scriptFormat="LiferayJavaScript">
      <script><![CDATA[  
user = Packages.com.liferay.portal.service.UserLocalServiceUtil.fetchUser(10382);
roles = user.getRoleIds();
outputTransition = "denied";
for (i = 0; i < roles.length; i++) {
  if (roles[i] == 15) {
   outputTransition = "approved";
   break;
  }
}
Packages.com.liferay.portal.kernel.workflow.WorkflowStatusManagerUtil.updateStatus              (Packages.com.liferay.portal.kernel.workflow.WorkflowConstants.toStatus(outputTransition), workflowContext);]]></script>
    </scriptTask>
    <sequenceFlow id="flow1" name="to Script Task" sourceRef="pending" targetRef="scripttask1"></sequenceFlow>
    <endEvent id="denied" name="End"></endEvent>
    <sequenceFlow id="flow2" name="to End" sourceRef="scripttask1" targetRef="denied"></sequenceFlow>
  </process>

Finally the status of the web content changes directly into "approved".
But when i look in my submission, i was supprised, that the status of "my submissions" is denied?!
What does that mean?

The following code is about the 3 error infos:


13:01:45,690 INFO  [http-bio-8080-exec-5][WorkflowInstanceManagerImpl:379] Stored new process instance ext 2384 -> 63
13:01:51,420 ERROR [http-bio-8080-exec-9][WorkflowComparatorFactoryImpl:30] Method is not implemented
13:01:52,660 ERROR [http-bio-8080-exec-9][WorkflowComparatorFactoryImpl:30] Method is not implemented
13:01:54,600 ERROR [http-bio-8080-exec-10][WorkflowComparatorFactoryImpl:30] Method is not implemented

So he stored a new process instance, but then something goes wrong.

Someone an idea?! 🙂

Thanks!

larsson7
Champ in-the-making
Champ in-the-making
Okay, now it´s clear! 🙂

iam
Champ in-the-making
Champ in-the-making
Ok, now the context is approved.

Someone an idea?! 🙂

Workflow context for Liferay is variables of the execution:
workflowContext = execution.getVariables();

larsson7
Champ in-the-making
Champ in-the-making
Thank you for your help 🙂

I have another question, what is the difference between LiferayJavaScript and JavaScript?
Why do i have to use LiferayJavaScript to make my workflow work?

I read, that Liferay and Activiti use the script language javascript, so can someone tell me why a workflow witch is written in javascript doesn´t work in Liferay?

Thanks!!!

frederikherema1
Star Contributor
Star Contributor
Probabily the LiferayJavascript is aware of the life-ray context, while plain old JS isn't. Since you're using liferay-services, this makes sense I guess…