title: How to update Azure API Management named values in a batch?
body: <p>We have existing production services which are based on this <a href="https://github.com/Azure/api-management-policy-snippets/blob/master/examples/Get%20OAuth2%20access%20token%20from%20AAD%20and%20forward%20it%20to%20the%20backend.policy.xml" rel="nofollow noreferrer">policy sample</a> which acquires a bearer token via client grant flow. However we want to switch the client application settings to a new app, which requires updating 4 named values. How can this be done with high throughput traffic volumes?</p>
  
  <p>We have updated named value pairs as <a href="https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-properties" rel="nofollow noreferrer">documented</a> and we've looked at the <a href="https://docs.microsoft.com/en-us/rest/api/apimanagement/2019-01-01/property?patch" rel="nofollow noreferrer">REST API</a>. There is only 1-by-1 update of values. We applied this on lower volume environments with no problems. However when we applied this on production environments we have outages with the expression unable to parse the value, which results in a null and caused an outage.</p>
  
  <p>This was the policy, copied from the portal.</p>
  
  <pre><code>        &lt;choose&gt;
              &lt;when condition="@(!context.Variables.ContainsKey(&amp;quot;access_token&amp;quot;))"&gt;
                  &lt;send-request ignore-error="true" timeout="20" response-variable-name="response" mode="new"&gt;
                      &lt;set-url&gt;{{authorizationServer}}&lt;/set-url&gt;
                      &lt;set-method id="apim-generated-policy"&gt;POST&lt;/set-method&gt;
                      &lt;set-header name="Content-Type" exists-action="override"&gt;
                          &lt;value&gt;application/x-www-form-urlencoded&lt;/value&gt;
                      &lt;/set-header&gt;
                      &lt;set-body&gt;@{return "client_id={{clientid}}&amp;amp;resource={{scope}}&amp;amp;client_secret={{clientsecret}}&amp;amp;grant_type=client_credentials";}&lt;/set-body&gt;
                  &lt;/send-request&gt;
                  &lt;set-variable name="responseJson" value="@(((IResponse)context.Variables[&amp;quot;response&amp;quot;]).Body.As&amp;lt;JObject&amp;gt;())" /&gt;
                  &lt;set-variable name="access_token" value="@(&amp;quot;Bearer &amp;quot; + (String)((JObject)context.Variables[&amp;quot;responseJson&amp;quot;])[&amp;quot;access_token&amp;quot;])" /&gt;
                  &lt;set-variable name="expires_in" value="@((int)((JObject)context.Variables[&amp;quot;responseJson&amp;quot;])[&amp;quot;expires_in&amp;quot;] - 20)" /&gt;
                  &lt;cache-store-value key="access_token" value="@((string)context.Variables[&amp;quot;access_token&amp;quot;])" duration="@(((int)context.Variables[&amp;quot;expires_in&amp;quot;]))" /&gt;
              &lt;/when&gt;
          &lt;/choose&gt;
          &lt;set-header name="Authorization" exists-action="override"&gt;
              &lt;value&gt;@((string)context.Variables["access_token"])&lt;/value&gt;
          &lt;/set-header&gt;
  </code></pre>
  
  <pre><code>  "Elapsed": 109,
    "Source": "set-variable[3]",
    "Reason": null,
    "Message": "Expression evaluation failed. Value cannot be null.\r\nParameter name: value\r\n   at Newtonsoft.Json.Linq.JToken.EnsureValue(JToken value)\r\n   at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)",
    "Scope": "product",
    "Section": "inbound",
    "Path": "choose\\when[1]",
    "PolicyId": "",
    "TransportErrorCode": 0,
    "HttpErrorCode": 0
  }
  </code></pre>
  
  <p>I suspected that the issue was related to one of these variables being null but our logs were unclear as to which variable.</p>