locked
Best practices for modifying SPD WFs with Start on Change RRS feed

  • Question

  • Server: SPF 2013
    Editor: SPD 2013

    I have several workflows that are running fine but now need to be updated/modified. What I have notice is that when editing a Start on Change workflow is that two instances of the WF will be running as soon as the list item is modified (the original WF and the newly updated WF).

    Many of my Start on Change WFs that are having this issue either start then wait for a specific field update or have assigned a Task at which point the newly modified workflow is fired and now there are 2 instances running. I am now brainstorming on better ways to write these WFs to consider how to handle future updates to avoid multiple instances but if anyone has some suggestions or can point me to a best practices that would be greatly appreciated.

    One of my first thoughts was to use a parallel action and stop the WF if some criteria was met but if this occurred when a task has already been assigned it could be an issue and possibly orphan the task or would the task be removed?


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Thursday, October 11, 2018 9:10 PM

Answers

  • Hi Stunpals,

    Since the workflow and column value of items are both changed in the list, per my knowledge, it’s the best way that cancel running previous workflow to use the newly workflow.

    Best regards,

    Grace Wang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, October 17, 2018 2:09 AM

All replies

  • Hi Stunpals,

    It’s the default behavior that existing old workflow will still run for the items until it completes.

    If we want items to use newly workflow, we need to cancel the existing old workflow, and the task will make as completed 100% and Canceled.

    We can also cancel all the running workflow using PowerShell command after the workflow is updated.

    http://www.macaalay.com/2013/11/07/easiest-way-of-cancelling-all-sharepoint-workflows-in-progress/

    Best regards,

    Grace Wang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, October 12, 2018 10:43 AM
  • Thanks Grace for the suggestion I will have to test this, it may help with removing the existing running WF to avoid duplication. Do you know how the PS Cancel script will handle "To-Do" or "Collect Data" tasks that haven't been completed? Will it be the same as manually terminating a workflow from the the web UI as this does cleanly remove all tasks that were created by the workflow.

    I am also aware that the duplication behavior is expected just thought I'd ask if there is a better way to approach a SPD workflow to better handle future changes to the workflow.
    As I mentioned above, when I create a Start on Change WF I typically set a Wait action so that the WF doesn't run repeatedly and unnecessarily until it is actually needed. I try to use Start on Create as much as possible but sometimes that doesn't meet our requirements.


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Friday, October 12, 2018 2:43 PM
  • Grace it appears this PS script would cancel all running workflows, I am assuming I could add another filter to specify which workflow to cancel as I have about 7 other workflows on the same list that I DO NOT want to cancel?

    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Friday, October 12, 2018 2:56 PM
  • Hi Stunpals,

    We can use the PowerShell script to cancel a specific workflow in a list:

    $web = Get-SPWeb "http://spsitename"
    
    $list = $web.Lists["ListName"]
    
    #Get the specific workflow, Associated with the list
    
    $WorkFlowToCancel = "WorkflowName"
    
    # Iterate through all Items and all Workflows on Items
    
    foreach ($item in $list.Items)
    
     {
    
       foreach ($wf in $item.Workflows)
    
         {
    
         #Check for the particular workflow
    
            if( ($wf.ParentAssociation.Name -eq $WorkFlowToCancel) -and ($wf.IsCompleted -ne $true))
    
            {
    
                write-host "Previous workflow status:"  $wf.InternalState
    
                #Cancel Workflow
    
                [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)     
    
                write-host "Workflow Cancelled at $($list.title)! "
    
            }
    
         }
    
      }
    
    $web.Dispose();
    

    The PowerShell script only cancel all the running workflow associated with the specific workflow instance. It works as end the workflow in UI.

    Best regards,

    Grace Wang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, October 15, 2018 6:03 AM
  • Grace I am just testing this and it appears to be working perfectly.

    Is there a way in the output to also include the Item ID of the items (or other columns like Title) where their workflow have been cancelled? Just thinking this would be good for logging in case anyone wanted review the affected items especially on large lists.


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Monday, October 15, 2018 4:13 PM
  • Looks like I could change the last line to 

    write-output "Workflow cancelled for : " $listItem.Title;  

    This gives the List Item's Title.

    I'll have to look into how to also include the Items ID.


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Monday, October 15, 2018 4:18 PM
  • It appears that I am making the changes in the wrong area because the output is repeating the ID and Title of the last item in the list (see modified code below).

    When I compare the first version you provided where all workflows are cancelled it did provided the Items Title correctly and the "write-host" seems to be a the same level of the "foreach" statement but obviously I have it wrong.

    # The PowerShell script only cancel all the running workflow associated with the specific workflow instance. It works as end the workflow in UI.
    $web = Get-SPWeb "http://fsspf-02/SysDev/TestLab/TestJobTracking"
    
    #Your List Name
    $list = $web.Lists["EngList"]
    
    #Get the specific workflow, Associated with the list
    $WorkFlowToCancel = "StartOnChangeTest"
    
    # Iterate through all Items and all Workflows on Items
    foreach ($item in $list.Items)
     {
       foreach ($wf in $item.Workflows)
         {
         #Check for the particular workflow
            if( ($wf.ParentAssociation.Name -eq $WorkFlowToCancel) -and ($wf.IsCompleted -ne $true))
            {
                write-host "Previous workflow status:"  $wf.InternalState
                #Cancel Workflow
                [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)     
                # write-host "Workflow Cancelled at $($list.title)! "
                write-host "Workflow Cancelled at $($list.Title), $($listItem.ID), $($listItem.Title)"
            }
         }
      }
    $web.Dispose();


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Monday, October 15, 2018 4:40 PM
  • Sorry Grace, I don't do much with Powershell so I struggle with making modifications to someone else's code.

    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Monday, October 15, 2018 9:13 PM
  • Hi Stunpals,

    Change "$($listItem.ID), $($listItem.Title)" to "$($item.ID), $($item.Title)".

    Best regards,

    Grace Wang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, October 16, 2018 2:03 AM
  • Grace that change gets the logging I was looking for, thanks.

    This script will be very useful when I need to cleanup a big list with running workflows.

    Do you have any suggestions on the best practices when using a Start on Change WF? eg should I avoid using the "Wait Until" and pausing a WF? This works great to limit the WF from running over an over but creates the duplication issue when the WF is updated.

    Its a catch 22, with benefits for each option. Or maybe this is common and the best way to address changes on a STart on Change workflow is to kill all running previous versions?


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Tuesday, October 16, 2018 5:55 PM
  • Hi Stunpals,

    Since the workflow and column value of items are both changed in the list, per my knowledge, it’s the best way that cancel running previous workflow to use the newly workflow.

    Best regards,

    Grace Wang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, October 17, 2018 2:09 AM
  • Grace, I am assuming there a way to use PS to then the start the newly updated workflow?

    This would make it easier and less confusing for the users as the workflow would not start until the item is changed/saved which would require every list item to be opened and then saved. Only I would know that the running WF was terminated so no staff would know to make the update.

    I did use a PS script to bulk update a specific field but it would be better to just start the newly updated workflow rather than editing a field.

    A search brought me this example, can you confirm this script start the specified WF on all list items before I test it on my server? (https://sharepoint.stackexchange.com/questions/111493/how-do-you-start-a-sharepoint-2013-list-workflow-in-powershell)

    I contacted someone who knows our setup and I'll see if he can assist with the Start workflow.

    • Edited by Stunpals Thursday, October 18, 2018 9:01 PM
    Thursday, October 18, 2018 5:27 PM