cancel
Showing results for 
Search instead for 
Did you mean: 

Workflow JavaScript API

bcoulson
Champ in-the-making
Champ in-the-making
All

I am trying to start a workflow without attaching it to a document but rather to have the document be created as one of the workflow steps. I learned how to do this through other posts here. So I have added the following to an event in my processdefinition.xml.

   <event type="before-signal">
            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
               <runas>admin</runas>
               <script>
                  <expression>
                        var today = new Date();
                          var fileName = "testing" + today.getTime() + ".txt";
             logger.log("Creating file called " + fileName);
         var doc = bpm_package.createFile(fileName);
         doc.content = today + "Classification = " + scwf_classification +
            "\nDocument Type = " + scwf_documentType;
         logger.log("Document Name = \"" + doc.properties.name + "\"");
         logger.log("Workflow Manager instance is " + workflow);
         var wfDef = workflow.getDefinitionByName("jbpm$scwf:publishDocument");
         logger.log("Workflow Definition = " + wfDef + ", ID = " + wfDef.getId());
         var activeWf = wfDef.getActiveInstances();
         logger.log("Active Workflow instances = " +
            (activeWf == null) ? "NONE" : activeWf.length);
         logger.log("Workflows associated with document = " +
         (doc.activeWorkflows == null) ? "NONE" : doc.activeWorkflows.length);
         logger.log("Document NodeRef = \"" + doc.nodeRef + "\"");
         logger.log("bpm_package = \"" + bpm_package.properties.name + "\"");
      </expression>
   </script>
</action>
</event>

All seems fine - it creates the file as I would expect. However when I run my workflow (I start it by selecting the "Start Workflow" option in the MyTasks dashlet). It creates the file, but the log shows no active instances ofthis workflow definition - it does find the definition. Then when I transition to the next step in my workflow, I see the file as an item as expected. When I select it, I am presented with an error saying that "Requested item can not be displayed as it is no longer available". Yet it still shows me the document content despite this error. I am stumped. I also cannot find the document anywhere in my repository. The log file has the following in it.

14:32:56,085 User:mike ERROR [freemarker.runtime] get(webdavUrl) failed on instance of org.alfresco.repo.template.TemplateNode

get(webdavUrl) failed on instance of org.alfresco.repo.template.TemplateNode
The problematic instruction:
———-
==> ${node.webdavUrl} escaped ${jsonUtils.encodeJSONString(node.webdavUrl)} [on line 55, column 18 in org/alfresco/slingshot/documentlibrary/item.lib.ftl]
in user-directive itemLib.itemJSON [on line 28, column 28 in org/alfresco/slingshot/documentlibrary/node.get.json.ftl]
———-

Java backtrace for programmers:
———-
freemarker.template.TemplateModelException: get(webdavUrl) failed on instance of org.alfresco.repo.template.TemplateNode
   at freemarker.ext.beans.BeanModel.get(BeanModel.java:223)
   at freemarker.core.Dot._getAsTemplateModel(Dot.java:76)
   at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
   at freemarker.core.ListLiteral.getModelList(ListLiteral.java:119)
   at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:89)
   at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
   at freemarker.core.Expression.getStringValue(Expression.java:93)
   at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.Macro$Context.runMacro(Macro.java:172)
   at freemarker.core.Environment.visit(Environment.java:603)
   at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.Environment.process(Environment.java:190)
   at freemarker.template.Template.process(Template.java:237)
   at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:197)
   at org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:589)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:267)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)
   at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:377)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:381)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:436)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:466)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:304)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
   at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
   at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
   at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.reflect.InvocationTargetException
   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:597)
   at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:840)
   at freemarker.ext.beans.BeanModel.invokeThroughDescriptor(BeanModel.java:276)
   at freemarker.ext.beans.BeanModel.get(BeanModel.java:183)
   … 54 more
Caused by: org.alfresco.repo.model.filefolder.FileFolderServiceImpl$InvalidTypeException: Type is not handled by this service: {http://www.alfresco.org/model/system/1.0}container
   at org.alfresco.repo.model.filefolder.FileFolderServiceImpl.isFolder(FileFolderServiceImpl.java:273)
   at org.alfresco.repo.model.filefolder.FileFolderServiceImpl.toFileInfo(FileFolderServiceImpl.java:225)
   at org.alfresco.repo.model.filefolder.FileFolderServiceImpl.getNamePath(FileFolderServiceImpl.java:1040)
   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:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.alfresco.repo.model.filefolder.TempFileMarkerInterceptor.invoke(TempFileMarkerInterceptor.java:77)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.model.ml.MLContentInterceptor.invoke(MLContentInterceptor.java:125)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.model.filefolder.MLTranslationInterceptor.invoke(MLTranslationInterceptor.java:205)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:80)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:44)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.audit.AuditMethodInterceptor.proceedWithAudit(AuditMethodInterceptor.java:217)
   at org.alfresco.repo.audit.AuditMethodInterceptor.proceed(AuditMethodInterceptor.java:184)
   at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:137)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy72.getNamePath(Unknown Source)
   at org.alfresco.repo.template.BaseContentNode.getWebdavUrl(BaseContentNode.java:412)
   … 61 more
14:32:56,272  ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 02170001 Wrapped Exception (with status template): 02170157 Error during processing of the template 'get(webdavUrl) failed on instance of org.alfresco.repo.template.TemplateNode'. Please contact your system administrator.
org.springframework.extensions.webscripts.WebScriptException: 02170001 Wrapped Exception (with status template): 02170157 Error during processing of the template 'get(webdavUrl) failed on instance of org.alfresco.repo.template.TemplateNode'. Please contact your system administrator.
   at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:758)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:171)
   at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:377)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:381)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:436)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:466)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:304)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
   at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
   at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
   at java.lang.Thread.run(Thread.java:619)
Caused by: org.alfresco.service.cmr.repository.TemplateException: 02170157 Error during processing of the template 'get(webdavUrl) failed on instance of org.alfresco.repo.template.TemplateNode'. Please contact your system administrator.
   at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:201)
   at org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:589)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:267)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)
   … 24 more
Caused by: freemarker.template.TemplateModelException: get(webdavUrl) failed on instance of org.alfresco.repo.template.TemplateNode
   at freemarker.ext.beans.BeanModel.get(BeanModel.java:223)
   at freemarker.core.Dot._getAsTemplateModel(Dot.java:76)
   at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
   at freemarker.core.ListLiteral.getModelList(ListLiteral.java:119)
   at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:89)
   at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
   at freemarker.core.Expression.getStringValue(Expression.java:93)
   at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.Macro$Context.runMacro(Macro.java:172)
   at freemarker.core.Environment.visit(Environment.java:603)
   at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:210)
   at freemarker.core.Environment.process(Environment.java:190)
   at freemarker.template.Template.process(Template.java:237)
   at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:197)
   … 27 more
Caused by: java.lang.reflect.InvocationTargetException
   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:597)
   at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:840)
   at freemarker.ext.beans.BeanModel.invokeThroughDescriptor(BeanModel.java:276)
   at freemarker.ext.beans.BeanModel.get(BeanModel.java:183)
   … 54 more
Caused by: org.alfresco.repo.model.filefolder.FileFolderServiceImpl$InvalidTypeException: Type is not handled by this service: {http://www.alfresco.org/model/system/1.0}container

I have tried the following.
1. adding a variable for bpm_package, and workflow but that actually results in an error the first time I try to access that variable. The lines I added were:
<variable name="bpm_package" access="read,write"/> and
<variable name="workflow" access="read, write"/>
2. Adding document to userhome directory, which btw, I can find when I search the repository, but still results in the same error when I try to open file.
3. I did output the webdavUrl of the document at one point and it was null which makes sense given the log message.

I am wondering if I need to attach the document to the workflow somehow? But I thought by creating the file in the bpm_package, I would be doing that.
1 REPLY 1

bcoulson
Champ in-the-making
Champ in-the-making
I thought i would update my post based on recent findings.

I changed the API I was using from createFile -> createNode.
I then realized that I need to add my newly created node to the bpm_package.
I am using the Share interface and I noticed that my workflow worked fine in Explorer. I then realized that I was not inserting the document into the Share documentLibrary folder but rather into the Share root folder. So I modified the location where the file was being created and the error went away. Here is my new source code:

         var today = new Date();
      var fileName = "testing" + today.getTime() + ".txt";
   logger.log("childByNamePath = " + companyhome.childByNamePath("/Sites/IPDSShare/documentLibrary"));
      var doc = companyhome.childByNamePath("/Sites/IPDSShare/documentLibrary").createNode(fileName, "cm:content");
      logger.log("Created file called " + fileName);
   doc.addAspect("cm:versionable");
   doc.content = today + "Classification = " + scwf_classification +
      "\nDocument Type = " + scwf_documentType;
   bpm_package.addNode(doc);

That works. Now I am attempting to transform my plain text document into a word document but transformDocument() keeps returning null. Any ideas on how to do this differently? The code I was using is as follows.


var wordDoc = doc.transformDocument("application/msword");
logger.log("wordDoc = " + wordDoc); /** always prints null **/

Thanks!