In any CRM system specially in Microsoft Dynamics 365, clean, accurate address data is essential. Whether you’re sending invoices, scheduling deliveries, or running marketing campaigns, even a small typo can cause delays, missed opportunities, or lost revenue.

But let’s be honest, people make mistakes. Sometimes Addresses are entered incomplete, inconsistent, or just plain wrong. And fixing them manually is a time consuming job.

Now imagine if you could automatically clean up those messy addresses using the power of Azure AI Foundry(GPT 4) with no manual effort,. That’s exactly what I am going to show you how to do.

Let’s walk you through how we built an intelligent Address Cleaner API using:

  • Azure Functions in C#
  • GPT-4 via Azure AI Foundry
  • ✅ Integration-ready for Dynamics 365 CE or Power Platform

💡 Real-World Scenario

Imagine a sales rep enters this address into Dynamics 365:

221B Bker Str, Lndon

Our GPT-powered Azure Function will instantly clean and complete it as:

221B Baker Street, London, UK

🛠️ Step-by-Step Implementation

Step 1: Create an HTTP Azure Function (C#)

Use Visual Studio: Create a new project

Set the project name to AddressCleanerFunction and set the location where you want to keep your solution.

I am providing .NET 8.0 Isloated + HTTP trigger azure function.

After clicking create the azure function will be created. I will do some adjustment in the function so that it can do the magic.

Replace the function.cs to this code and rename the file to AddressCleanerFunction.cs

[Function("CleanAddress")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{       

    // Read the request body  
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

    // Deserialize the request body  
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    string rawAddress = data.address;

    if (string.IsNullOrWhiteSpace(rawAddress))
    {
        return new BadRequestObjectResult("Please provide an 'address' field in the request body.");
    }

    var prompt = $"You are an intelligent address assistant. Clean, complete, and standardize this address:\n\"{rawAddress}\".\nOutput the corrected address only.";

    string response = await CallGPT(prompt);

    return new OkObjectResult(new
    {
        original = rawAddress,
        corrected = response.Trim()
    });      
}

Step 2: Call GPT-4 via Azure AI Foundry

You have to first create the Azure AI Foundry project by following steps. If you already have a existing project, then you can ignore the creation step.

Once you click on create, deployment will take around 1-2 minute and it will create the resource for you

Click on Go to Azure AI Foundry Portal

It will take you to the portal where you can copy end point and key. Select Model catalog and then select GPT-4o for this work from the below list.

Please follow below numbers to copy the value for end point and key that we will use in our azure function to connect

Once you copied the value then go back to your visual studio and adjust the code.

For this demo, I am using local.settings.json and hence update the values here for End point and Api key. Make sure the name should match here in and as well in your azure function code.

Final thing would be to add this function in the same file.

Adding the code here so that you can take reference.

private static async Task<string> CallGPT(string prompt)
{

    var endpoint = Environment.GetEnvironmentVariable("EndPoint");  //stored in local.settings.json
    var apiKey = Environment.GetEnvironmentVariable("ApiKey");

    using var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

    var payload = new
    {
        messages = new[]
        {
                new { role = "system", content = "You are a helpful assistant that cleans up and completes mailing addresses." },
                new { role = "user", content = prompt }
            },
        temperature = 0.3,
        max_tokens = 200
    };

    var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");

    var response = await httpClient.PostAsync(endpoint, content);
    response.EnsureSuccessStatusCode();

    var responseBody = await response.Content.ReadAsStringAsync();
    dynamic result = JsonConvert.DeserializeObject(responseBody);
    return result?.choices[0]?.message?.content;
}

Step 3 : Testing the Function with Postman

For our demo, I will be testing the code locally by using postman. In your case, you can first deploy the code to server and then test it. This azure function can be called by Logic app or power automate, etc.

Lets set up postman

POST http://localhost:PortNumber/api/CleanAddress
Content-Type: application/json

{
  "address": "221B Bker Str, Lndon"
}

Once done, then start debugging in visual studio

Copy the local host url and paste that in postman

Response from Postman

{
    "original": "221B Bker Str, Lndon",
    "corrected": "221B Baker Street, London, UK"
}

🔗 Integrating with Dynamics 365 CE

You can connect this Azure Function to Dynamics 365 via:

➤ Power Automate

Trigger: When record is created or updated

Step: HTTP → POST → Azure Function

Step: Update row with corrected address

➤ C# Plugin (optional)

Use HttpClient in a plugin to call the Azure Function during PreUpdate or Post create stage.

Thanks for Reading 🙂

No responses yet

Leave a Reply