I have an Azure YAML Pipeline that is setting variables in a step to use in later steps. For example, setting a timestamp to a variable and passing later into the ServiceNow Change Management connector for the "planned start date" field. The problem is, the planned start date field does not seem to accept any syntax for the pipeline's variables. The pipeline fails no matter what I try. The ServiceNow Change Management connector exists in an environment check in the pipeline's step.
Here's my example YAML:
trigger:
branches:
include:
- development
paths:
include:
- /migrations
stages:
- stage: SetVariablesStage
jobs:
- job: SetVariableJob
steps:
- powershell: |
$dateTimeFormat = "yyyy-MM-dd HH:mm:ss"
$scheduledDateTimeFormat = "yyyy-MM-dd HH:mm"
$plannedStartDateTime = (Get-Date).AddMinutes(-60).ToString($dateTimeFormat)
$plannedEndDateTime = (Get-Date).ToString($dateTimeFormat)
$scheduledStartDateTime = (Get-Date).AddMinutes(-60).ToString($scheduledDateTimeFormat)
$scheduledEndDateTime = (Get-Date).ToString($scheduledDateTimeFormat)
Write-Host "##vso[task.setvariable variable=plannedStartDateTime;isOutput=true]$plannedStartDateTime"
Write-Host "##vso[task.setvariable variable=plannedEndDateTime;isOutput=true]$plannedEndDateTime"
Write-Host "##vso[task.setvariable variable=scheduledStartDateTime;isOutput=true]$scheduledStartDateTime"
Write-Host "##vso[task.setvariable variable=scheduledEndDateTime;isOutput=true]$scheduledEndDateTime"
echo "plannedStartDateTime: $plannedStartDateTime"
echo "plannedEndDateTime: $plannedEndDateTime"
echo "scheduledStartDateTime: $scheduledStartDateTime"
echo "scheduledEndDateTime: $scheduledEndDateTime"
displayName: 'Set Custom Variables'
name: SetVariableStep
pool:
vmImage: ubuntu-latest
- stage: 'dev_stage'
dependsOn: SetVariablesStage
variables:
- group: TestPipeline-Variables_Dev
- name: env
value: "dev"
jobs:
- deployment: DeployToDevelopment
variables:
plannedStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedStartDateTime'] ]
plannedEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedEndDateTime'] ]
scheduledStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledStartDateTime'] ]
scheduledEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledEndDateTime'] ]
displayName: 'Deploying to Development'
pool:
vmImage: 'ubuntu-latest'
environment:
name: 'Development'
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: UsePythonVersion@0
displayName: 'Use Python 3.8.x'
inputs:
versionSpec: '3.8.x'
- task: Bash@3
inputs:
targetType: 'inline'
script: |
<redacted: Deployment script>
- job: update_ServiceNow
dependsOn: DeployToDevelopment
variables:
plannedStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedStartDateTime'] ]
plannedEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.plannedEndDateTime'] ]
scheduledStartDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledStartDateTime'] ]
scheduledEndDateTime: $[ stageDependencies.SetVariablesStage.SetVariableJob.outputs['SetVariableStep.scheduledEndDateTime'] ]
steps:
- task: UpdateServiceNowChangeRequest@2
inputs:
ServiceNowConnection: 'ServiceNow_Sandbox'
NewStatus: '6'
otherParameters: '{ "u_work_notes": "$(Build.BuildNumber) $(plannedStartDateTime) $(plannedEndDateTime)", "u_scheduled_start": "$(scheduledStartDateTime)", "u_scheduled_end": "$(scheduledEndDateTime)" }'
pool: server
Example ServiceNow connection configuration:
For the Control Options: Linked Variable Group, I have it connected to the correct Variable group.
When I run the pipeline, the environment check fails. It is related to the Planned Start Date field because if I hard code a value the pipeline works. If I remove the value it works, but later fails due to a business rule in our ServiceNow on the field.
How do I use a variable in the field so I don't have to hard code it? The last step in the pipeline that updates servicenow works correctly. I am able to set the "otherParameters" correctly with the pipeline variables. Just not in the Environment Check ServiceNow connector.

The
ServiceNow Change Managementin environment approvalcannotdirectly invoke the variables which created in previous stage/job.You can try to output the variable value to validate. For example, output in
Approvals:To use the variable value, as an alternative, you can put the target value to
variable group, and link to this group inServiceNow Change Management, then you can use the variable inside.I don't have servicenow account, so i checked with
rest api approvalas an example.I updated the variable group variable value in powershell task. Make sure the
{project} build service(org)account has administrator role on the Variable group security so that it can update the variable value.I simply invoked the variable from Variable group in the url:
Confirm the variable is correctly get in rest api url when pipeline started.