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
An update. In my localhost log i'm getting the following stack trace:

Nov 10, 2009 1:04:59 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class org.alfresco.web.app.ContextListener
java.lang.NoClassDefFoundError: org.alfresco.service.cmr.repository.StoreRef
   at java.lang.J9VMInternals.verifyImpl(Native Method)
   at java.lang.J9VMInternals.verify(J9VMInternals.java:63)
   at java.lang.J9VMInternals.initialize(J9VMInternals.java:124)
   at java.lang.Class.newInstanceImpl(Native Method)
   at java.lang.Class.newInstance(Class.java:1243)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3787)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
   at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:830)
   at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:719)
   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
   at org.apache.catalina.core.StandardService.start(StandardService.java:516)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:615)
   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Nov 10, 2009 1:04:59 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Nov 10, 2009 1:05:00 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Nov 10, 2009 1:05:02 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Nov 10, 2009 1:05:02 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()

Just for FYI, im also putting my TaskInstanceObject class here. Please note it's also in the same package as MailActionExecuter (i.e. org.alfresco.repo.action.executer):


package org.alfresco.repo.action.executer;

import java.io.Serializable;

public class TaskInstanceObject implements Serializable{
   
   /**
    *
    */
   private static final long serialVersionUID = -5369157359360104619L;
   
   String taskID;
   String taskName;
   String taskInstanceURL;
   public String getTaskID() {
      return taskID;
   }
   public void setTaskID(String taskID) {
      this.taskID = taskID;
   }
   public String getTaskName() {
      return taskName;
   }
   public void setTaskName(String taskName) {
      this.taskName = taskName;
   }
   public String getTaskInstanceURL() {
      return taskInstanceURL;
   }
   public void setTaskInstanceURL(String taskInstanceURL) {
      this.taskInstanceURL = taskInstanceURL;
   }
   
   
   

}


Any pointers would be appreciated.

Thanks.

calle
Champ in-the-making
Champ in-the-making
Hi
When I send emails with attachments from Jbpm first of all I had to implement an extension to the EmailActionExecuter to handle attachments (this is covered in http://forums.alfresco.com/en/viewtopic.php?p=51285). Then you have to pass on any attachment as a parameter to the extended EmailActionExecuter like:

final Action mailAction = actionService.createAction(AttachmentEmailerActionExecuter.NAME);
        mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_SUBJECT, properties.get(MailProperty.SUBJECT));       
        mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_TO, properties.get(MailProperty.TO_ADDRESS));
        mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_FROM, properties.get(MailProperty.FROM_ADDRESS));
        mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_TEXT, properties.get(MailProperty.BODY));
        mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_TEMPLATE, properties.get(MailProperty.TEMPLATE));
        mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_ATTACHMENTS, attachments);
        mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_MODEL, model);
        actionService.executeAction(mailAction, null);

The attachements are a List<NodeRef> in this case, but it depends on how you choose to extend EmailActionExecuter.

Finally I have implemented my own JBPMAction to send the emails, like:


public class SendExternalConfirmation extends JBPMSpringActionHandler {

   private static final Logger logger = Logger.getLogger(SendExternalConfirmation.class);
   
   private static final long serialVersionUID = 343656888342953693L;

   private static final String INITIATOR_EMAIL_PROCESS_VARIABLE = "initiatorEmail";
   private static final String ATTACHMENT_NODEREF_PROCESS_VARIABLE = "a_nodeRef";
         
   
   //—————————————————
   //Public methods
   //—————————————————
   public void execute(final ExecutionContext context) throws Exception {
      final String initiatorEmail = (String) context.getVariable(INITIATOR_EMAIL_PROCESS_VARIABLE);
      final JBPMNode attachmentNode = (JBPMNode)context.getVariable(ATTACHMENT_NODEREF_PROCESS_VARIABLE);

      final ArrayList<NodeRef> attachedDocuments = new ArrayList<NodeRef>();
      attachedDocuments.add(attachmentNode.getNodeRef());
               
      final Action mailAction = actionService.createAction(AttachmentEmailerActionExecuter.NAME);
      mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_SUBJECT, "Test subject"));       
      mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_TO, initiatorEmail);
      mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_FROM, "noreply@alfresco.com");
      mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_TEXT, "bla bla");
      mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_TEMPLATE, "if use freemarker put a template here…");
      mailAction.setParameterValue(AttachmentEmailerActionExecuter.PARAM_ATTACHMENTS, attachedDocuments);
      mailAction.setParameterValue(TransmittalAttachmentEmailerActionExecuter.PARAM_MODEL, model);
       
      actionService.executeAction(mailAction, null);
   
   }
}

You the call your own action from the JBPM processdefinition. Like

<event type="node-enter">
   <action class="com.mycompany.SendExternalConfirmation"/>
</event>
Of course the attachments and email address must have been set in the workflow previously for this to work.

//Carl

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

Thanks for all your help.
I'm able to send the task url in the body of the mail. I'm making use of an email template(.ftl file).

I guess the exceptions that i got were because the jar file that i created got corrupted somehow. I made use of the jar cvf to pack the jar up which worked perfectly.

Like JayJayECL mentioned, i had to pass my taskid and tasktype on to the Action. The MailActionExecuter class had to be tweeked to handle the newly passed params and to add it as part of the ftl model.

Thanks.

sans
Champ in-the-making
Champ in-the-making
Hi nitinkcv,
I am trying to send an email with the task url.
Here is part of my process_definition.xml.
<task name="wf:submitAdhocTask" swimlane="initiator"/>
           <event type="task-create">
               <script>
                  <variable name="myTaskId" access="read,write" />
                      <expression>myTaskId = "jbpm$"+ (executionContext.getTaskInstance().getId()+1);</expression>
                  </script>
            </event>
               <transition name="" to="adhoc">
            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
               <script>
                  
                  if(logger.log != null)
                     logger.log("Testing mail!!!");
                  var mailAction = actions.create("mail");
                  mailAction.parameters.from = initiator.properties["cm:email"];
                  mailAction.parameters.to = bpm_assignee.properties["cm:email"];
                  mailAction.parameters.subject = person.properties.firstName +" "+person.properties.lastName+" has assigned you this task: " + bpm_workflowDescription;
                  mailAction.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/Notify Email Templates/custom_notify_user_email.ftl");
                  length =  bpm_package.children.length;
                  mailAction.parameters.taskid = myTaskId;
                  if(logger.log != null)
                  {
                     logger.log(mailAction.parameters.from);
                     logger.log(mailAction.parameters.to);
                     logger.log(mailAction.parameters.subject);
                     logger.log("first name "+person.properties.firstName);
                     logger.log("last name "+person.properties.lastName);
                     logger.log(length);
                     if(length==0)
                        logger.log("No Document is selected hence email is not sent.");
                  }
                  
                  for(var i = 0; i &lt; length; i++)
                  {
                     if(logger.log != null)
                        logger.log("Before Execute");
                     mailAction.execute(bpm_package.children[i]);
                     if(logger.log != null)
                        logger.log("After Execute");
                  }
               </script>
            </action>
         </transition>
And I have extended the MailActionExecuter.java
Here is the code snippet from extended java.
public static class TaskHelper
    {
        String taskId;
       
        public String getTaskId()
        {
           return this.taskId;
        }
        public void setTaskId(String taskId)
        {
           this.taskId = taskId;
        }
    }
private Map<String, Object> createEmailTemplateModel(NodeRef ref)
   {
      Map<String, Object> model = new HashMap<String, Object>(8, 1.0f);
     
      NodeRef person = personService.getPerson(authService.getCurrentUserName());
      model.put("person", new TemplateNode(person, serviceRegistry, null));
      model.put("document", new TemplateNode(ref, serviceRegistry, null));
      NodeRef parent = serviceRegistry.getNodeService().getPrimaryParent(ref).getParentRef();
      model.put("space", new TemplateNode(parent, serviceRegistry, null));
     
      // current date/time is useful to have and isn't supplied by FreeMarker by default
      model.put("date", new Date());
     
      // add custom method objects
      model.put("hasAspect", new HasAspectMethod());
      model.put("message", new I18NMessageMethod());
      model.put("dateCompare", new DateCompareMethod());
      model.put("url", new URLHelper(repoRemoteUrl));
      model.put("task", new TaskHelper());
      return model;
   }

But still I can't access the task variable in my ftl.
I am getting an error saying that task variable is undefined.

please help!!

Thanks
Sans!!