Power Platform: Analyse email attachments with AI

In today’s fast-paced business environment, leveraging automation and artificial intelligence (AI) is crucial for maintaining efficiency and gaining insights. This blog post explores how to create a Power Automate flow that reads emails with attachments, extracts content from images or PDF documents using AI, and can perform various analyses on the extracted text. For instance, you can determine the sentiment, generate summaries, or classify emails as purchase requests or complaints and routing then accordingly. By integrating these advanced technologies, businesses can streamline their processes, enhance decision-making, and stay ahead in the competitive landscape.

AI Generated image

Create an AI model

First, we need to create a custom AI document model or AI Prompt that receives the PDF content and analyses it. But off course we can also use a default model. In this example we will be using the default AI Sentiment.

Create a Power Automate Flow

In this flow we will get all the attachments from the email and get the content ready to be sent for a sentiment analysis.

  • Create a Power Automate flow with the trigger, When a new email arrives in a shared mailbox (V2).
  • Connect this with the required email box and select the option Include Attachments to Yes.
  • Add the action Initialize variable and call it Initialize variable – Attachment Content.
  • Add a Scope action called Scope – Get PDF Content.
  • We are going to combine all the content of all the found PDF attachments into one variable. You could also send each attachment file separately.
  • First, we need to filter the attachment files to only get the PDF file.
  • Add a Filter Array action and call it Filter Array – Attachment for PDF.
  • Set the from to Attachments.
  • Set the first value to Attachment Content type.
  • Set the filter to is equal to.
  • Set the second value to application/pdf.
  • Add an Append to string variable action and name it Append to string variable – Attachment Content.
  • Set the Name to Attachment Content.
  • Set the Value to Content (from the filter array) – .
  • An apply to each will be automaticallycreated, name it Apply to each – Found PDF.
  • Add a Recognize text in an image or a PDF document below the apply to each.
  • Set the image to Attachment Content.
  • Add a Scope and name it Scope – AI Sentiment.
  • Add a Create Text with GPT using a prompt action and name it Create Text with GPT using a prompt – Get sentiment.
  • Set AI Sentiment as the Prompt.
  • Set Input Text to Attachment Content variable.
  • Your flow now looks like this.

Power Platform: Use AI to evaluate every incoming email

Leveraging AI for email management not only saves time but also reduces the risk of human error. By automating the evaluation process, businesses can ensure that important emails, such as orders, are promptly and accurately identified, allowing for quicker response times and improved customer satisfaction. Embracing AI in email workflows is a smart move towards greater productivity and operational excellence.

In this article, I explain how to create an AI prompt model using Microsoft GPT to streamline email processing. By integrating Power Automate, the flow sends the content of each new email to the AI model, which then determines if the email is an order. If identified as an order, the AI responds with a JSON output indicating a positive result. This approach enhances the efficiency and accuracy of order processing through intelligent automation.

Create an AI model

For this blog I create a simple AI prompt to evaluate if a incoming email is a request for an order. The actual prompt is more complicated but confidential.

  • Open the AI hub aka AI Builder and select the Create text with GPT using a prompt.
  • Give the prompt a name.
  • Create three input parameters.
    • Email
    • EmailSubject
    • Attachments
  • Select by Output JSON (preview) and click on Edit.
  • Add the following JSON code.
{
      "Order": "true"
}
  • Set the Model to either GPT 3.5 (cheaper but less accurate) or GPT 4 (more expensive and more accurate).
  • Set the Temperature to 0.
  • Set the Prompt as follows or make your own prompt.
  • Save the prompt.

Create a Power Automate Flow

The Power Automate flow will start for every incoming email and will send to the AI model, the email body, subject and names of the attachments. The model will return its determination in JSON form.

  • Create a new Power Automate flow with an outlook/email trigger, When a new email arrives in a shared mailbox.
  • Add an Initialize variable action to create a variable called All attachment names.
  • Add an Append to string variable and select by the name the All attachment names variable.
  • Set for the value the attachments of the email, with the following code. This will automatically add a Apply to each action.
items('Apply_to_each_-_Attachment')?['name'] - 
  • Add the Create text with GPT using a prompt action.
  • Select your created AI Prompt.
  • Set by attachments the variable All attachment names.
  • Set by Email the Body of the email.
  • Set by EmailSubject the Subject of the email.
  • This will start the AI model and it will return its results to the flow.
  • Form here on you can add in the requered steps for your specific process and test the process.

Power Automate: Set email category

In a recent AI project, I developed a solution to evaluate every incoming email in a shared mailbox using an AI model (GPT with prompt). To inform users of the shared mailbox that an email has been processed, the email is flagged with a custom Outlook category. This process leverages a special HTTP action for Outlook, simplifying the implementation. Notably, there is no need to set up any special permissions if the flow owner already has access to the shared mailbox. This example does not cover the specifics of communicating with the AI through Power Automate.

Creating custom category in Outlook

  • Open Microsoft Outlook.
  • Click on Category in the Home Ribbon.
  • Click on All Categories.
  • Click on New.
  • Set a name and select a color.
  • Click on OK followed by Clicking on OK.
  • You have now created the custom Outlook category.

Setting the category with Power Automate

  • Create a new Power Automate flow with an outlook/email trigger. For example: When a new email arrives in a shared mailbox.
  • Add the Send an HTTP request action.
  • Set the URI to be following code.
https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages/triggerOutputs()?['body/id']
  • Set the method to PATCH
  • Use the following JSON code for the body
{
"categories": ["AI Finished"]
}
  • Set the Content-Type to application/json.
  • Save the flow.

Power Apps: Hidding and showing a component

I was tasked with developing a canvas app for a client that required each screen to include a popup element. The popup needs be openend by clicking an image, upon being clicked, it triggers a popup screen where users can compose a message. I utilized a Library Component and explored methods to control its visibility effectively. The challenge was ensuring that the message popup would appear when an image is clicked and disappear upon user interaction with a designated button. My solution involved manipulating the component’s dimensions — adjusting the height and width to expose only the image or to reveal the entire component. This technique successfully creates the perception of the popup screen being opened and closed by the user.

  • The first step is to create a solution with a canvas app and a component library.
  • In my example I named the component comp VIM Message.
  • Then open the component.
  • Set the size of the component to 640 by 640.
  • Add two custom output properties to the component named CompVIMHeigt and CompVIMWidth.
  • Set the component width to ‘comp VIM Message’.CompVIMWidth
  • Set the component height to ‘comp VIM Message’.CompVIMHeight
  • Set the component reset to Set(varCompHeight, 80);
  • Add an image element and set the following code on the Onselect. This will make the container visible and make the component bigger.
Set(varComVIMVisible, true);
Set(varCompHeight, 640);
Set(varCompWidth, 640);
  • You might need to adjust the numbers based on your needs.
  • Also set the X and Y properties on your image based on your needs. I use the following.
X = 'comp VIM Message'.Width - 55
Y = 15
  • Add a container with the element you need in the popup.
  • Set the following properties of the container.
Visible = varComVIMVisible
Height = 440
Width = 520
X = 75
Y = 190
Visible = varComVisible
  • Add two buttons in the container one called Cancel (in Dutch that would be Annuleren) and Send (in Dutch that would be Verzenden).
  • Set for both button the onselect to the following code, this will make the component smaller and hide the container.
Set(varCompHeight, 80);
Set(varCompWidth, 60);
Set(varComVIMVisible, false);
  • Open the canvas app and add the component comp CIM Message.
  • Insert the component into the canvas app.
  • Set the Heigth property to the following code. This will make sure the Height has a value when the canvas app page is loaded.
If(IsBlank('comp VIM Message_HS'.CompVIMHeight), 80, 'comp VIM Message_HS'.CompVIMHeight)
  • Set the Width property to the following code. This will make sure the Width has a value when the canvas app page is loaded.
If(IsBlank('comp VIM Message_HS'.CompVIMWidth), 60, 'comp VIM Message_HS'.CompVIMWidth)
  • Set the X property to the following code. This will make sure that the Image part of the component is on the left top side of the screen.
If('comp VIM Message_HS'.CompVIMWidth = 640, 'Home Screen'.Width - 640, 'Home Screen'.Width - 60)
  • Now run the canvas app and see if it works (it might not work in the play function in the edit mode).

From Bing Maps to Azure Maps

Bing maps has been an easy-to-use solution for getting location information in canvas apps, but this offering will be placed under the Azure Maps umbrella. While Bing Maps will continue to function for now, it’s essential to prepare for the transition. In this blog post I will explain how to use Azure Maps in a Canvas App. Please note that the locations shown in this post are not my actual location.

Azure Maps Services

  • First, we need to create a Azure Maps Account in a resource group (I will presume you know how to create a resource group).
  • Open your resource group.
  • Click on Create and search for and click on Azure Maps.
  • Click on Create.
  • Give the Azure Maps Account resource a name.
  • Select the correct Region and Pricing tier.
  • Agree with the terms and click on Review + Create.
  • Click on Create and wait for the deployment to be ready.
  • Click on Go to resource.
  • Click on Authentication and copy the Primary Key. You will need this in the Power Automate Flow.

The Power Automate Flow

  • Create a new Power Automate Flow and name it Azure maps get Postal Code.
  • Add as the trigger Power Apps (V2), with two text inputs called Longtitude and Latitude.
  • Next, we will add a HTTP action, this will call Azure Maps to get the location details based on the provided GPS location.
  • Set the method to Get.
  • Set the URI to the following code, make sure to put in your subscription-key (that is the primary key from the Azure Maps Account resource).
  • Set the Latitude and Longitude with the input fields form the trigger.
https://atlas.microsoft.com/search/address/reverse/json?api-version=1.0&subscription-key={YOUR KEY}&query={Latitude},{Longitude}
  • Add the action Parse JSON and give it the name Parse JSON – Location information.
  • Set the Content to the body of the HTTP call and set the schema as detailed in the linked file.
  • Add a compose action (if you want to only return the first found postal code).
  • The code for the input is as follows if you have named the Parse JSON action the same as I did.
first(body('Parse_JSON_-_Location_information')?['addresses'])?['address']?['postalCode']
  • Add the Respond to a Power App or flow action.
  • Add an output text called postalcode and add the compose as its value.
  • Your flow will now look like this.

The canvas app

We will now create the canvas app with a map that shows the user’s current location on a map, and a button to get the postal code of that location and a reset button. Please note this is not my actual location.

  • Create a new canvas app, I blank Phone canvas.
  • Click on Power Automate followed by Add flow to add the earlier created flow.
  • Add the map element to the canvas app.
  • Set the DefaultLatitude property with the following code.
Location.Latitude
  • Set the Location.Latitude property with the following code.
 Location.Longitude
  • Set DefaultLocation property with the following code.
true
  • If you want to see your Latitude and Logitude you can add labels to display the values.
  • Add a Label and set the Text property with the following code.
"Latitude: " & Location.Latitude
  • Add a Label and set the Text property with the following code.
"Longitude: " & Location.Longitude
  • Add a button and set the Text property with the following code.
"Get postal code"
  • Set following code on the OnSelect property. If needed update the code with your flow name, I used AzuremapsgetPostalCode.
Set(varPostalCode,AzuremapsgetPostalCode.Run(Location.Longitude, Location.Latitude));
  • Add a button and set the Text property with the following code.
"Reset"
  • Set following code on the OnSelect property.
Set(varPostalCode, Blank())
  • Add a label and set the Text property with the following code.
"Postal Code: " & varPostalCode.postalcode

Power Pages: Prefill fields with data form the user profile

Microsoft Power Platform provides powerful tools for creating web portals, and Power Pages is one of them. In this blog post, we’ll explore how to enhance the user experience by pre-filling order forms on your portal using custom fields. Specifically, we’ll focus on pre-populating the order form with data from the user’s profile page.

Imagine you have a Power Pages website with an order form. Users need to fill out details such as their city and country and other relevant information. To streamline this process, we’ll leverage custom fields you’ve added to the user’s profile page. When a user accesses the order form, we’ll automatically populate these fields based on their profile information.

Adding fields to the Profile

  • Add the required fields to the Contact and Order table in Dataverse
  • For example, a text field called City and a lookup field called Country.
  • Add the fields to the form Profile Web Form, which is the default form used in the Power Pages profile page.
  • Open the Power Pages website to see if the changes are visible and fill them.

JavaScript to prefile the columns on a order

  • Open the Portal Management of your Power Pages website.
  • Open the element where you need to prefill the fields. In my case it’s the Order Information Web Form Step that is part of the Create Order Web form.
  • On the tab Form Options (this can be different per element) and navigate down to the Custom JavaScript input box.
  • Add the following code and change the following if needed.
$(document).ready(function() {

    //Get the profile county ID and Name data
    var CountryGUID = "{{ user.abc_country.id }}";
    var CountryName = "{{ user.abc_country.name }}";    
  
    //Get the City from the users profile (contact table)
    var City = "{{ user.abc_city }}";
  
    //set the text input element city with the profile field City
    document.getElementById("city").value = City;

    //set the lookup input element with the profile field Country
    //Set the name of the lookup field
    $("#abc_country_name").attr("value",CountryName);
    //Set the GUID of the lookup field
    $("#abc_country").attr("value", CountryGUID);
    //Set the logical name of the lookup table
    $("#abc_country_entityname").attr("value","abc_country");
});
  • Open the page in a web browser and use the web developers tools (f12) to find the id of the city field.
  • Update the document.get ElementById if required.
  • Do the same thing for the #abc_country_name, #abc_country and #abc_country_entityname.
  • Update the $(“”).attr where required.
  • If you are using different field names make sure to update the field names or add them to JavaScript.
  • {{ user.[fieldname] }} will return the value of that field.
  • {{ user.[fieldname].id }} will return the id of the selected value of a lookup field.
  • {{ user.[fieldname].name }} will return the name of the selected value of a lookup fields.
  • Save the changes and your fields will now be filled automatically.