> For the complete documentation index, see [llms.txt](https://atlas.anywhere.now/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://atlas.anywhere.now/product-reference/dialogue-studio/dialogue-studio-example-flows/connect-dynamics-365-application-user.md).

# Connect to Microsoft Dynamics 365 as an application user

Import a flow in Dialogue Studio that authenticates as an application user and searches for a Microsoft Dynamics 365 contact by phone number or email.

This example connects Dialogue Studio to Microsoft Dynamics 365 using an application user and an OAuth client credentials flow, then searches for a contact by phone number or email. Use it as a starting point for looking up caller information from Dynamics 365.

## What it does

Accessing the Dynamics 365 OData API requires an access token. This flow requests and caches that token, then uses it to search for a contact whose mobile phone, work phone, or email matches the caller's identifier.

## Prerequisites

* **Microsoft Entra ID app registration**: the app registration must include the **Dynamics CRM > user\_impersonation** API permission. Obtain the client ID and client secret through your approved secret handling process.
* **Application user setup in Dynamics 365**: in your Dynamics 365 environment, go to **Settings > Security > Users**, switch to **Application Users**, select **New**, and choose the app registration.
* **Role assignment**: assign a role to the application user, for example the `SalesPerson` role for managing contacts.

## Preview

![Dynamics 365 contact search flow](https://2627137428-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FscZpnEdBjDgQmlVn1iLr%2Fuploads%2Fgit-blob-5130c02392f52df5b10b295433c35bee2d6fe46b%2Fdynamics365-contact-search.png?alt=media)

## Import the flow

1. Log in to your Dialogue Studio environment.
2. Open or create a tab for the flow.
3. From the menu in the top right, select **Import** and add the JSON from [`flows/dynamics365-contact-search.json`](https://github.com/AnywhereNow/atlas-docs/tree/main/product-reference/dialogue-studio/dialogue-studio-example-flows/flows/dynamics365-contact-search.json).
4. Open the **Configure Dynamics 365 configurations** change node and fill in:
   * `client_id` - from the app registration's **Overview** page.
   * `client_secret` - created and copied from **Manage > Certificates & secrets** on the app registration.
   * `resource` - the URL of your Dynamics 365 environment, for example `https://tenant.crm.dynamics.com`.
   * `tenant_id` - from the app registration's **Overview** page.
5. Open the **Search contact** inject node and enter the phone number or email address you want to search for.
6. Select the blue box before the inject node to test the flow.
7. Use the debug sidebar to see the output and which debug node the result reached.

{% hint style="warning" %}
Enter the client secret using your organization's approved secret handling process. Never leave a real client secret in a flow you export, share, or debug.
{% endhint %}

## How the flow works

* **Trigger flow**: the **Search contact** inject node starts the flow with a query (`q`), for example a phone number in `tel:` format.
* **Configure Dynamics 365 variables**: the **Configure Dynamics 365 configurations** change node sets `client_id`, `client_secret`, `resource`, and `tenant_id` on the message, which are required for authentication.
* **Dynamics 365 access token** (group of nodes):
  * **Check Token Validity** checks whether a cached token in flow context is still valid. If so, it uses the existing token; otherwise it requests a new one.
  * **Use Existing Token** sets the Authorization header from the cached token.
  * **Prepare Token Request** builds the client credentials payload and headers for a token request.
  * **Get Token** sends the HTTP POST request to obtain a new access token.
  * **Store Token and use New Token** stores the new token and its expiry in flow context and sets the Authorization header.
* **Dynamics 365 contact search** (group of nodes):
  * **Prepare Contact Request** builds the OData query URL, formatting the input as a phone number match (`contains(mobilephone, ...)` or `contains(telephone1, ...)`) or an email match (`emailaddress1 eq ...`), depending on the format of the query.
  * **Get Contact** sends the HTTP GET request to Dynamics 365.
* **Validate output** (group of nodes):
  * **Check statusCode** verifies the response was `200`. If not, a debug node logs the failure.
  * **Check if any results** and **Check if multiple results** branch to separate debug nodes for no results, a single result, or multiple results.

## Safe fallback behavior

A failed token request or a non-200 response from Dynamics 365 routes to a dedicated debug node ("Failed to retrieve information") instead of continuing with an invalid response. A search with no matching contact is logged separately ("no results") rather than treated as an error, so your flow can distinguish "the lookup failed" from "the contact was not found."

## Verification

1. Trigger the inject node with a known contact's phone number and confirm the "Returned single contact" debug entry shows the expected contact.
2. Trigger it with an email address and confirm the same result.
3. Trigger it with a value that does not match any contact and confirm the "no results" debug entry fires instead of an error.
4. Temporarily use an invalid client secret and confirm the token request fails safely and is logged, rather than causing the flow to hang.

## Related topics

Go to [Dialogue Studio example flows](/product-reference/dialogue-studio/dialogue-studio-example-flows.md) for the full gallery of example flows.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://atlas.anywhere.now/product-reference/dialogue-studio/dialogue-studio-example-flows/connect-dynamics-365-application-user.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
