Wolfik
Wolfik

Reputation: 326

How to automaticly change Work Item State

I want to create rule like: When custom field value is set to X then change state od Task to New. Can it be done?

It's hard to belive this isn't possible but I can't find a way to do it.

Upvotes: 0

Views: 2664

Answers (1)

Ziyang Liu-MSFT
Ziyang Liu-MSFT

Reputation: 5012

Default custom rule does not support changing state. There are two workarounds you can refer to.

Workaround1:

You can customize a field to decide whether to change the state, trigger a webhook when the field changes, and then trigger a pipeline through this webhook. In the pipeline, the state of the current work item is changed by running the REST API Work Items – Update.

There are the detailed steps.

  1. Customize a field named "ChangeState" in my sample. Its definition is as follows:

enter image description here

  1. Follow this doc Define a webhooks resource to create a webhook and a "Incoming Webhook" service connection. The setting of the webhook: enter image description here

Set Trigger on this type of event to Work item updated, set Field to the customed field created at the first step.

  1. Set the webhook created at the second step as the pipeline resource and run the REST API Work Items – Updateto update the state of the work item. There is the YAML for your reference:

    resources:
       webhooks:
         - webhook: ChangeWIState           ### Webhook alias
           connection: ChangeNameSC    ### Incoming webhook service connection
    
    
     pool:
       vmImage: ubuntu-latest
    
     steps:
    
     - task: PowerShell@2
       inputs:
         targetType: 'inline'
         script: |
           Write-Host ${{ parameters.ChangeWIState.resource.workItemId}}
           $token = "<your PAT>"
           $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
           $url="https://dev.azure.com/<org name>/_apis/wit/workitems/${{ parameters.ChangeWIState.resource.workItemId}}?api-version=7.0"
           $body = @'
           [
             {
               "op": "add",
               "path": "/fields/System.State",
               "value": "New"
             }
           ]
           '@
           $head = @{ Authorization =" Basic $token" }
           $response = Invoke-RestMethod -Uri $url -Method PATCH -Headers $head -Body $body -ContentType application/json-patch+json
    

"ChangeWIState" is the name of my webhook and "ChangeNameSC" is the name of my servie connection.

Every time the field "ChangeState" changes, the pipeline will be triggered to run the REST API to change the state to new.

Workaround2:

You can use this extension "Work item form one click actions.

  1. Set Actions as following: enter image description here

  2. Set Triggers as following: enter image description here

When the value of the field "Test" changes to "X", the state will change to "New". enter image description here

enter image description here

Upvotes: 1

Related Questions