cancel
Showing results for 
Search instead for 
Did you mean: 

JavaScript: check for undefined variable

mruflin
Champ in-the-making
Champ in-the-making
I'm unsuccessfully trying to find out whether a variable is defined or not. Is there a simple way to do this?

Context:
I'm inside a workflow.jbpm.AlfrescoJavaScript action (defined in a workflow) and am trying to check whether bpm_workflowDueDate is set or not.

The following two

bpm_workflowDueDate === undefined
typeof bpm_workflowDueDate == 'undefined'

throws the following Exception

16:09:22,203 ERROR [graph.def.GraphElement] action threw exception: Failed to execute supplied script: ReferenceError: "bpm_workflowDueDate" is not defined. (AlfrescoScript#1)
org.alfresco.service.cmr.repository.ScriptException: Failed to execute supplied script: ReferenceError: "bpm_workflowDueDate" is not defined. (AlfrescoScript#1)
        at org.alfresco.repo.jscript.RhinoScriptService.executeScriptString(RhinoScriptService.java:235)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:335)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
        at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:116)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
        at org.alfresco.repo.security.permissions.impl.AlwaysProceedMethodInterceptor.invoke(AlwaysProceedMethodInterceptor.java:40)

Thanks in advance for a hint on this one
5 REPLIES 5

gavinc
Champ in-the-making
Champ in-the-making
Try using the following syntax:

if (bpm_workflowDueDate != void) taskInstance.dueDate = bpm_workflowDueDate;

This is used in the adhoc_processdefinition.xml file describing the ad hoc workflow.

mruflin
Champ in-the-making
Champ in-the-making
This unfortunately does not work. The code inside <script> and inside the script action is executed differently. The first one passes, the second one throws a ReferenceError. I'm even not sure whether the first one is actually JavaScript.


  <task-node name="review">
    <task name="cwwf:reviewTask" swimlane="assignee">
      <event type="task-create">
        <script>       
          if (bpm_workflowDueDate != void) taskInstance.dueDate = bpm_workflowDueDate;
          if (bpm_workflowPriority != void) taskInstance.priority = bpm_workflowPriority;
      </script>
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
         <script>
           if (bpm_workflowDueDate != void) {
              taskInstance.dueDate = bpm_workflowDueDate;
           }
          </script>
        </action>
      </event>
    </task>

My guess is that RhinoScriptService handles undefined variables in a wrong way, because (x == undefined) is valid JavaScript yet does it throw an exception here.

gavinc
Champ in-the-making
Champ in-the-making
Apologies, I obviously didn't read your post well enough.

The script within the first <script> block is actually beanShell syntax which is what I mistakenly thought you were referring to.

The second <script> block as you say is indeed RhinoScript and so x == undefined should work, we can't think of any reason why not.

We'll take a look and see if there is any reason for this.

vladimir_kovaly
Champ in-the-making
Champ in-the-making
That works fine:

            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
               <script>
                  <![CDATA[
                  if (typeof bpm_workflowDueDate != 'undefined')
                     taskInstance.dueDate = bpm_workflowDueDate;
                  if (typeof bpm_workflowPriority != 'undefined')
                     taskInstance.priority = bpm_workflowPriority;
                  ]]>
               </script>
            </action>

mathias_lin
Star Contributor
Star Contributor
x == undefined
does not work in all cases.

In a web script controller for example:

While this


if (args.to != undefined) …


works, the following


if (requestbody != undefined) …
if (formdata != undefined) …


will both throw an error:


ReferenceError: "requestbody"/"formdata" is not defined.