cancel
Showing results for 
Search instead for 
Did you mean: 

Running JavaScript before submitting workflow form

sihnu
Champ in-the-making
Champ in-the-making
Hi,

like the subject says I would like to run JavaScript and call web script before submitting a form. So when the user is pressing the submit button the JavaScript should be run but I just don't know which is the best way to implement this. I've been looking at forms-runtime.js but it is general file for every form in Alfresco and that doesn't seem to be proper place to do my implementation because I only want to use my script when submitting a workflow form. Could somebody point me to the right direction?

Thanks in advance.
6 REPLIES 6

hardik1512
Star Contributor
Star Contributor
You can do this by overriding activiti-transitions.js file which is @ path share\components\form\workflow. This file contains a function ActivitiTransitions_onClick which is called when any button in workflow form in clicked. You can add your condition and custom code there.

sihnu
Champ in-the-making
Champ in-the-making
Thanks, I will try this right away! Smiley Happy I'll tell here how I'll manage.

sihnu
Champ in-the-making
Champ in-the-making
It seems that init form of the workflow is not using activiti-transitions.js file. Now I'm back to using this form-runtime.js but I don't know if this is very smart choice because I should do some hax to detect what form is in use. So that would be done checking if some element is found or not. I wonder how init form handles the transition buttons. If anyone could bring some light to that I would be really happy. I didn't mention earlier I needed this feature for the init form because I didn't know there were differences between them.

darkredd
Star Contributor
Star Contributor
Hi,

I am not quite sure if your feature is global or should apply to specific workflow. If the case is for specific workflow, I have always put my init scripts on the startEvent task of the workflow; which executes upon a transition click. You would just have to handle you code to look out for failures, then you can use the <strong>throws</strong> keyword of javascript to prevent the workflow from starting. But if your case is to happen on workflow select, then you will have to use the process's execution level listener to achieve that.

Hope this is helpful.

lmaorence
Confirmed Champ
Confirmed Champ

Hello, I'm having the same problem. Did you made a workaround about this?

anon26949
Star Contributor
Star Contributor

You can try something like this:

    <serviceTask id="scripttask1" name="Submit" activiti:class="org.alfresco.repo.workflow.activiti.script.AlfrescoScriptDelegate">
      <extensionElements>
        <activiti:field name="script">
          <activiti:string><![CDATA

               for(var i = 0; i < bpm_package.children.length; i++) {               
                    var node = bpm_package.children[i];
                    var isRegistered = node.properties["myfirm:isRegistered"];
                    
                    if(isRegistered==true) {
                         throw new Error ("The document has already been registered");
                    }
               }
               
     ]]></activiti:string>
        </activiti:field>
        <activiti:field name="runAs">
          <activiti:string><![CDATA[admin]]></activiti:string>
        </activiti:field>
      </extensionElements>
    </serviceTask>

Regards.