cancel
Showing results for 
Search instead for 
Did you mean: 

Error : ReferenceError: 'companyhome' is not defined.

nitinkcv
Champ in-the-making
Champ in-the-making
Hi,

I'm actually trying to trigger a mail using the AlfrescoJavascript.

Part of my code in the process definition file is:


<transition name="moreInfoNeeded" to="moreInfoNeeded" >
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
                <variable name="bpm_package" access="read" />
                <variable name="initiator" access="read" />
                <variable name="bpm_workflowDescription" access="read" />
                <expression>
                     var mail = actions.create("mail");
                     var prsn = initiator;
                     try {
                        var emailAddress = prsn.properties.email;
                        mail.parameters.to = emailAddress;
                        mail.parameters.subject = "Content authoring task submitted with additional information";
                        mail.parameters.from = person.properties.email;
                        mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/notify_user_email_taskurl.ftl");                        
                        mail.parameters.text = "Default text goes here";
                        mail.execute(bpm_package);
                        logger.log("send mail to "+ prsn.properties.userName);
                     } catch (error) {
                        logger.log("Failed sending mail to "+ prsn.properties.userName + "  " + error);
                     }
               </expression>   
             </script>
         </action>
      </transition>

My ftl file is the below:


Hi,

A new workflow task has been created.
Please got to http://${url.context}/alfresco/command/ui/managetask?id=${pooled.id}&type=${pooled.qnameType}&contai... to view the workflow task.

Regards

Admin


However when i check my log file i get the following error:

DEBUG [org.alfresco.repo.jscript.ScriptLogger] Failed sending mail to admin ReferenceError: "companyhome" is not defined.

I have made use of companyhome in my process definition file to get the hold of an image and it worked.

Has anyone come across this before?

Thanks.
13 REPLIES 13

nitinkcv
Champ in-the-making
Champ in-the-making
I got the solution for the error.

I need to add a statement like:


<variable name="companyhome" access="read" />

which should be just before the <expression> tag. However now the problem comes to the ftl. It gives an error saying that

Caused by: freemarker.core.InvalidReferenceException: Expression url is undefined on line 4, column 24 in workspace://SpacesStore/2005cd22-e18f-4112-8442-e0a8558725c0.

BUt isnt the url attribute a keyword in ftl?

jayjayecl
Confirmed Champ
Confirmed Champ
it depends on the context of the FTL.
If your ftl is a presentation template in alfresco, yes.
If it's in the body of a mail, no.
you have to provide it yourself to the Freemarker model when building the email

nitinkcv
Champ in-the-making
Champ in-the-making
Hi,

Are there any examples or links that you have come across that i could follow to do this?

Thanks.

jayjayecl
Confirmed Champ
Confirmed Champ
Hi, someone faced the same troubles a few weeks ago.
It seems that he could manage to achieve what you are up to :

http://forums.alfresco.com/en/viewtopic.php?f=34&t=22202

nitinkcv
Champ in-the-making
Champ in-the-making
Thanks. I'll message him and ask if he was able to find out a way.

nitinkcv
Champ in-the-making
Champ in-the-making
JayJayEcl,

With respect to the post that you wriiten @ http://forums.alfresco.com/en/viewtopic.php?f=34&t=22202, youve said to modify the MailActionExecuter code to include the models.

The url for the tasks would be of the form http://localhost:8080/alfresco/command/ui/managetask?id=jbpm$2&amp;type=${pooled.qnameType}&amp;cont.... I tried to debug the MailActionExecuter class. However i was not find out the values for the 'id=jbpm$2' and 'type=${pooled.qnameType}' from any of the variables that are declared there in the class. I'm assuming that these values need to be put in the model which can be made use in the ftl.

Do you have any pointers regarding getting these?

Also in the post @ http://forums.alfresco.com/en/viewtopic.php?f=34&t=14595&p=75219#p55772 a piece of method is written however not sure whether its a overriden method or which class its from?

Thanks.

jayjayecl
Confirmed Champ
Confirmed Champ
the piece of method is from custom code.

However i was not find out the values for the 'id=jbpm$2' and 'type=${pooled.qnameType}' from any of the variables that are declared there in the class. I'm assuming that these values need to be put in the model which can be made use in the ftl

If you fork (rewrite) MailActionExecuter, you'll be able to add new parameters to the action, including passing (via scripting in the processDefinition.xml) new vars -such as taskID and taskType- to the action… and then handle them so that they are available in the model for the ftl template

nitinkcv
Champ in-the-making
Champ in-the-making
JayJayECL,

In the MailActionExecutor the only two param that it takes are the Action and NodeRef. Do you have any examples as to how to pass var like the taskID and TaskType to the Action from my script in the process definition file?

Currently i'm building up my task URL in the processDefinition file. Something like this:


<task name="ppdwf:contentAuthorTask"  swimlane="content_author" >
         <event type="task-create">
                <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
                  <script>
                     executionContext.setVariable("taskInstanceID", taskInstance.id);
                     executionContext.setVariable("taskInstanceName", taskInstance.name);
                  </script>
                </action>            
         </event>
      </task>
      <transition name="moreInfoNeeded" to="moreInfoNeeded" >
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
                <variable name="bpm_package" access="read" />
                <variable name="initiator" access="read" />
                <variable name="bpm_workflowDescription" access="read" />
                <variable name="companyhome" access="read"></variable>
                <variable name="taskInstance" access="read" />
                <expression>
                     var mail = actions.create("mail");
                     var prsn = initiator;
                     try {
                        var emailAddress = prsn.properties.email;
       var taskType = executionContext.getVariable("taskInstanceName").substring(executionContext.getVariable("taskInstanceName").indexOf(":")+1);
                        var taskURL = "http://localhost:8080/alfresco/command/ui/managetask?id=jbpm$" + executionContext.getVariable("taskInstanceID") + "&amp;type={com.pixs.workflowuri}" + taskType + "&amp;container=plain";
                        mail.parameters.to = emailAddress;
                        mail.parameters.subject = "Content authoring task submitted with additional information";
                        mail.parameters.from = person.properties.email;
                        mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/notify_user_email_taskurl.ftl");                        
                        mail.parameters.text = "Default text goes here. Please click on" + taskURL  + " to go to the task";
                        mail.execute(bpm_package);
                        logger.log("send mail to "+ prsn.properties.userName + "taskURL: " + taskURL);
                     } catch (error) {
                        logger.log("Failed sending mail to "+ prsn.properties.userName + "taskURL: " + taskURL + "  " + error);
                     }
               </expression>   
             </script>
         </action>
      </transition>

But i would like to make use of a ftl template. Also rather hardcoding the workflowURI it would be nice to get it from some variable.

Thanks.

nitinkcv
Champ in-the-making
Champ in-the-making
Found out a way to pass parameters i think.
Page @ http://wiki.alfresco.com/wiki/3.1_JavaScript_API#ScriptAction_API mentions that
parameters
    an associative array (map) of the parameters for the action. For example mail.parameters.subject. Each parameter is keyed by a String. Parameters can be modified and new parameters added.

So what i'm hoping is that i can add new parameters to the mail. So inside my process definition file i'm adding something like"


mail.parameters.taskid = executionContext.getVariable("taskInstanceID");
mail.parameters.tasktype = executionContext.getVariable("taskInstanceName");                        

And i'm modifying the MailActionExecter class:


      // add objects for the taskid, taskName and the url for the taskInstance related information
      model.put("taskInstanceObj", obj);

where my obj is a java class which has the taskURL as one of its member variables.

My ftl is of the form:


New task has been created. Please click on ${taskInstanceObj.taskURL} to view the task.

I have re-jar the alfresco-repository.jar with these two java classes(MailActionExecuter and TaskInstanceObject).

However on server start i get the following exception:
ERROR [org.springframework.beans.factory.support.DefaultListableBeanFactory] Destroy method on bean with name 'mail' threw an exception
java.lang.NoClassDefFoundError: org.alfresco.repo.action.executer.TaskInstanceObject
   at java.lang.Class.getDeclaredMethodsImpl(Native Method)
   at java.lang.Class.getDeclaredMethods(Class.java:612)
   at org.apache.cxf.common.injection.ResourceInjector.getAnnotatedMethods(ResourceInjector.java:347)
   at org.apache.cxf.common.injection.ResourceInjector.getPreDestroyMethods(ResourceInjector.java:340)
   at org.apache.cxf.common.injection.ResourceInjector.invokePreDestroy(ResourceInjector.java:317)
   at org.apache.cxf.common.injection.ResourceInjector.destroy(ResourceInjector.java:96)
   at org.apache.cxf.bus.spring.Jsr250BeanPostProcessor.postProcessBeforeDestruction(Jsr250BeanPostProcessor.java:80)
   at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:135)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:344)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:317)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:290)
   at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:730)
   at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:708)
   at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:675)
   at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:338)
   at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:74)
   at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:3882)
   at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4523)
   at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:924)
   at org.apache.catalina.startup.HostConfig.undeployApps(HostConfig.java:1191)
   at org.apache.catalina.startup.HostConfig.stop(HostConfig.java:1162)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:313)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
   at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1086)
   at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1098)
   at org.apache.catalina.core.StandardEngine.stop(StandardEngine.java:448)
   at org.apache.catalina.core.StandardService.stop(StandardService.java:584)
   at org.apache.catalina.core.StandardServer.stop(StandardServer.java:744)
   at org.apache.catalina.startup.Catalina.stop(Catalina.java:628)
   at org.apache.catalina.startup.Catalina$CatalinaShutdownHook.run(Catalina.java:671)

Any ideas what could be wrong?

Thanks