Monday, July 15, 2013

Approval workflow - This task is currently locked by a running workflow and cannot be edited.

Hello everybody,
I think that if you had developed some approval workflow you would receive an error in the title at least once. You can find a lot of information about the error: "This task is currently locked by a running workflow and cannot be edited.". Here are some links which were quite useful to me:
The purpose of this post won't be to describe how to solve this error or find the cause of it since there are many posts about this topic already.
I would like to share the approach I have used to achieve my goals. So what my goals were...
1) Typical approval workflow with  at least one approver.
2) All tasks should be approved if at least one approver approves the task.

So here is the snapshot of the workflow designer:

CreateParallelTaskWithContentType is an activity which creates all tasks and there is logic to do so, but I won't describe it here because it is not related to this topic.

OnTaskCreated activity just sends emails when a new task is assigned.

WhileActivity is a loop which holds task till it is approved.

OnParallelTaskChanged is an activity which controls the previous loop and break it after a task is updated.

CompleteTask is an activity which should complete the current task and in my case has no method.

CodeCompleteAllTask activity is an activity which should complete all tasks if one of the tasks is approved.

Code of the last activity I have written according to articles above and here it is:
//Email body
string emailBody = string.Empty;
//Email receiver
SPFieldUserValue recieverValue;
foreach (SPWorkflowTask task in this.workflowProperties.Workflow.Tasks)
{
   if (task[SPBuiltInFieldId.TaskStatus].ToString() != SPResource.GetString(new CultureInfo((int)task.Web.Language, false), "WorkflowTaskStatusComplete", new object[0]))
   {
       SPSecurity.RunWithElevatedPrivileges(delegate()
       {
           //Send email notification about rejected task
           recieverValue = (SPFieldUserValue)task.Fields[SPBuiltInFieldId.AssignedTo].GetFieldValue((String)task[SPBuiltInFieldId.AssignedTo]);
           receiverCompleteAllAssociatedTasks = recieverValue.User;
           emailBody += "some text <br />";
           sendMail(receiverCompleteAllAssociatedTasks.Email, "Your reply is no longer needed.", emailBody, true, true);
           emailBody = string.Empty;

           //Set task as completed
           bool allowUnsafeUpdates = task.Web.AllowUnsafeUpdates;
           task.Web.AllowUnsafeUpdates = true;
           task["ApproveStatus"] = "Rejected";
           Hashtable data = new Hashtable();
           data[SPBuiltInFieldId.Completed] = "TRUE";
           data[SPBuiltInFieldId.PercentComplete] = 1;
           data["FormData"] = "Completed";
           data["TaskStatus"] = "Approved";
           data["Reviewed"] = "Yes";
           data["SendSignOffEmail"] = "Yes";
           data[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)task.Web.Language, false), "WorkflowTaskStatusComplete", new object[0]);

           bool succed = SPWorkflowTask.AlterTask((task as SPListItem), data, false);
           if (!succed)
             sendMail("", "Error / completeAllAssociatedTasks()-AlterTask", "Error occured at EmployeeID: " + this.workflowProperties.Item["EmployeeID"], false, false);
           //No need to use task.Web.Update() or task.SystemUpdate(), it is already involved in AlterTask method;

           //Set AllowUnsafeUpdates to the original value
           task.Web.AllowUnsafeUpdates = allowUnsafeUpdates;
        });
    }
  }

receiverCompleteAllAssociatedTasks = null;



I wouldn't recommend doing any type of actions except the approval logic in the replicator activity. Perhaps there is a bug in my code but in my case some activities inside replicator activity were fired more than once even they shouldn't be.

This should be all and I hope this helps somebody with this annoying error :)

Happy coding...

No comments:

Post a Comment