cancel
Showing results for 
Search instead for 
Did you mean: 

Custom variables' values are not shown in the task for a workflow started from webscript untill the application is restarted.

tmacftw
Champ in-the-making
Champ in-the-making

Hello Alfresco Community,

EDIT2:

TLDR: When I start a workflow using java web script, all of my custom variables don't have their values visible in the tasks (which I set with the webscript when starting the workflow) until I restart the web server. After the restart - they are visible and If I execute the web script again to start another workflow, then for both of the workflows all my custom variables in the tasks are not visible. If I restart the web server they appear again. This is a really strange behavior. Please help.

I am still new to Alfresco and I am doing my best learning it but I have some troubles with updating workflow variables from webscript using the Public Java API. After some research I was able to start custom workflow from my web script using a  java class which I will paste at the bottom but I have problems with setting my custom variables.
Summary of the problem: It appears that my custom variables are being set on execution and also on task level, but for some reason Alfresco is not showing their values in the tasks?!?

I did the following test: started a workflow using the webscript, then I checked the task and nothing was populated. Then, I just restarted the app server and without doing anything else when I checked the same task - the properties were visible. Also, if I start another workflow with the webscript, then the variables in the tasks for both workflows are disappearing again until I restart the app server again. Does anyone know what may be causing this or how can Iissue an update or something, without restarting the app server?

When I restart the app server - they are visible again. Here is the class that I am using with my web script:

public class StartWorkflow extends AbstractWebScript {
    /**
     * The Alfresco Service Registry that gives access to all public content services in Alfresco.
     */
    private ServiceRegistry serviceRegistry;
    
    public void setServiceRegistry(ServiceRegistry serviceRegistry) {
        this.serviceRegistry = serviceRegistry;
    }

    @Override
    public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {        
              
        // Create JSON object for the response
        JSONObject obj = new JSONObject();
        
        try {                        
            // Check if parameter defName is present in the request            
            String wfDefFromReq = req.getParameter("defName");
            if (wfDefFromReq == null) {
                obj.put("resultCode", "1 (Error)");
                obj.put("errorMessage", "Parameter defName not found.");
                return;      
            }            
            // Get the WFL Service
            WorkflowService workflowService = serviceRegistry.getWorkflowService();
            // Build WFL Definition name
            String wfDefName = "activiti$" + wfDefFromReq;
            // Get WorkflowDefinition object
            WorkflowDefinition wfDef = workflowService.getDefinitionByName(wfDefName);          
            // Check if such WorkflowDefinition exists
            if (wfDef == null) {
                obj.put("resultCode", "1 (Error)");
                obj.put("errorMessage", "No workflow definition found for defName = " + wfDefName);
                return;
            }            
            
            // Get parameters from the request
            Content reqContent = req.getContent();
            if (reqContent == null) {
                obj.put("resultCode", "1 (Error)");
                obj.put("errorMessage", "No content.");
                return;
            }
            String content;
            content = reqContent.getContent();
                                           
            if (content.isEmpty()) {
                obj.put("resultCode", "1 (Error)");
                obj.put("errorMessage", "Content is empty.");
                return;
            }
            
            JSONTokener jsonTokener = new JSONTokener(content);
            JSONObject json = new JSONObject(jsonTokener);
            
            Map<QName, Serializable> params = new HashMap();
            params.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "Some string "
                    + json.getString("cmpropSmiley TongueropOne"));                       
            
            NodeRef packageNodeRef = workflowService.createPackage(null);
            params.put(WorkflowModel.ASSOC_PACKAGE, packageNodeRef);
            
            Iterator<String> iterator = json.keys();   
            while (iterator.hasNext()) {                                                                
                String paramName = iterator.next();
                String value = json.getString(paramName);
                //obj.put(paramName, value);
                QName qName = QName.createQName(paramName);
                params.put(qName, value);               
            }                            
            // Create process
            WorkflowPath wfPath = null;
            wfPath = workflowService.startWorkflow(wfDef.getId(), params);
            
            obj.put("resultCode", "0 (Success)");
            obj.put("workflowId", wfPath.getId());            

        } catch (JSONException e) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST,
                    e.getLocalizedMessage());
        } catch (IOException ioe) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST,
                    "Error when parsing the request.",
                    ioe);
        } finally {
            // build a JSON string and send it back
            String jsonString = obj.toString();
            res.getWriter().write(jsonString);
        }
    }
}

Thank you in advance!

Regards,

K.

EDIT: I forgot to mention that these custom variables are in a custom aspect, which is added to my custom tasks.

1 REPLY 1

tmacftw
Champ in-the-making
Champ in-the-making

OK, I found my mistake. I wasn't generating the QName properly. It is fixed now and I am marking this as closed.