azure-devopstfsazure-boards

Field Automation Based On Date Field


I've created a board with the fields "Start Date," "End Date," and "Deadline." The "Deadline" represents the number of days required to complete the task.

While I currently enter the "Start Date" and "End Date" manually, I would like to automate the calculation of the "Deadline" based on these dates. Is there a way to achieve this?

I would like to do it without using Power Automate if possible.

I've checked the available configurations and searched both here and on Google, but haven't found a solution yet.


Solution

  • I am afraid that there is no out-of-box method can automatically calculate of the Deadline field based on Start Date and End Date.

    Since you don't want to use Power Automate, I suggest that you can use Script to run Rest API to calculate date.

    Refer to the following steps:

    Step1: Create a boards query(Boards -> Queries) to list all work items that need to be calculated.

    For example:

    enter image description here

    You can record the QueryID in the url.

    Step2: We can use PowerShell script to run the following Rest APIs:

    Wiql - Query By Id: Use to list work items in Query.

    Work Items - Get Work Item : Use to get the value of the Start Date and End Date field.

    Work Items - Update: Use to update the DeadLine field in work item.

    Here is the PowerShell sample:

    $token = "PAT"
    
    $Boardsqueryid ="QueryId"
    
    $ProjectId = "Projectname"
    
    $OrganizationId = "OrganizationName"
    
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    $getworkitems ="https://dev.azure.com/$($OrganizationId)/$($ProjectId)/_apis/wit/wiql/addf177b-a5f8-460a-a976-40a5b52f2923?api-version=7.1-preview.2"
    
    $response2 = Invoke-RestMethod -Uri $getworkitems -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
    
    foreach($workitem in $response2.workitems)
    {
        $workitemid =  $workitem.id
    
        echo "workitemid: $workitemid"
    
        $url="https://dev.azure.com/$($OrganizationId)/$($ProjectId)/_apis/wit/workitems/$($workitemid)?api-version=7.1-preview.3"
    
        $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
    
        $startdate = $response.fields.'Microsoft.VSTS.Scheduling.StartDate'
        
         echo "startdate: $startdate"
    
        $enddate = $response.fields.'Custom.EndDate'
         
        echo "enddate: $enddate"
    
        $ts = New-TimeSpan -Start $startdate -End $enddate
    
        $days = $ts.Days
        echo "deadlinedays: $days"
    
        $urlupdateworkitem = "https://dev.azure.com/$($OrganizationId)/$($ProjectId)/_apis/wit/workitems/$($workitemid)?api-version=7.1-preview.3"
    
         $body="[
                  {
                    `"op`": `"add`",
                    `"path`": `"/fields/Custom.deadline`",
                    `"value`": `"$days`"
                  }
    
               ]"
    
    
      $response1 = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PATCH  -Body $body -ContentType application/json-patch+json
    }
    

    Result:

    Powershell side:

    enter image description here

    Work item Page:

    enter image description here