Skip to main content

How to pass the IDs of selected rows to Power Automate

This article is for you if:

  • You would like to select one or more records from data grid or subgrid
  • Get the IDs of the selected rows
  • Pass these IDs to Power Automate Flow

To do this, you will require the following components:

  • Power Automate Flow with Http trigger
  • XrmToolBox: Ribbon Workbench (RWB) tool
  • a JavaScript

Step 1: Create a Power Automate Flow

We will be triggering a flow using HTTP request when the button is clicked in our Model Driven App.

For your trigger, select ‘When a HTTP request is received’.

We will need a Request Body JSON Schema. This is how the data (row IDs) will be passed from the JavaScript to power automate flow.

Paste the following Schema into the box:

json

{
  "type": "object",
  "properties": {
    "TimesheetIds": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}

In case you are interested, we get this Schema from the JSON below:

json

{
  "TimesheetIds": [
    "11111111-f98d-ef11-8a69-111111111111",
    "33333333-b69b-ef11-8a69-333333333333"
  ]
}

If you copy the above, click ‘Use sample payload to generate schema’ and paste it in, you will get the Schema.

Step 2: Get the flow URL

After the trigger, to be able to save the flow, let’s add a Compose action for now and get our result back from the trigger. This allows us to save the flow and generate the URL.

 
Copy the HTTP POST URL and save it somewhere. We will need this later.

Step 3: Create a solution for the button

To create a custom button through Ribbon Workbench, you will need a new solution which contains the entity of the records you want to multi-select. The solution cannot contain attributes, forms, apps, dashboards, workflows, or else you will not be able to create the button through RWB.

e.g. Time Sheet table

 

Step 4: Connect to your environment and launch the solution in RWB

When you open RWB in XrmToolBox, it will prompt you to Load Solution. Simply select the solution you just created above and click OK.

Step 5: Let’s create the button

On the left side of RWB under Toolbox there is a BUTTON command. Click and hold BUTTON and drag it to anywhere you want your button to appear.

It will be under the sub grid in this case.

The button will appear in the list and under the SOLUTION ELEMENTS.


Step 6: JavaScript

JavaScript

function passSelectedTimesheetsToFlow(data) {
    // Fetch the selected IDs from the "data" parameter
    var selectedTimesheetIds = data || []; // Ensure data is not null or undefined

    // Ensure at least one timesheet is selected
    if (selectedTimesheetIds.length === 0) {
        alert("Please select at least one timesheet.");
        return;
    }

    // Convert the IDs to JSON payload
    var payload = JSON.stringify({ TimesheetIds: selectedTimesheetIds });

    // Define the Power Automate flow URL
    var flowUrl = "<your_flow_url>"; // Your Flow URL

    // Call the Power Automate flow
    var req = new XMLHttpRequest();
    req.open("POST", flowUrl, true);
    req.setRequestHeader("Content-Type", "application/json");

    req.onreadystatechange = function () {
        if (req.readyState === 4) {
            if (req.status === 200 || req.status === 202) {
                alert("Flow triggered successfully!");
            } else {
                alert("Failed to trigger the flow. Error: " + req.responseText);
            }
        }
    };

    req.send(payload);
}

Feel free to replace the code with your entity names, and especially <your_flow_url>. We noted this down in Step 2.

Full disclosure: this code was written with the help of Microsoft Copilot. You can paste the code into any chat bot you want to ask for an explanation if required.

One of the most important parts of the code is your function name. In the above example, we have:

JavaScript

function passSelectedTimeSheetToFlow(data) {}

Our function name here is passSelectedTimeSheetToFlow. Make sure to note down the function name.

Now, save your notepad file with an extension “.js”.

 

Step 7: Upload as web resource

Save the JavaScript file ‘<name.js> as a web resource into any of your solutions, except the RWB solution we created in Step 1. RWB will not load the solution if there is a web resource.

 

 

Step 8: Creating a command in RWB

Back to RWB, now let’s create a command using the JavaScript. Under SOLUTION ELEMENTS > COMMANDS, click the + icon.

On the right, under Properties: Command, let’s Add Action > JavaScript Action.

Find the JavaScript web resource in Library. In this example, we can type ‘pass’ and find.

 
We’ll have to write down our function name on the right too - ‘passSelectedTimesheetsToFlow’ as noted above in Step 4.

Step 9: Let’s make sure we can see our button

While we’re still on the command, let’s add a rule under Enable Rules.

What we’re doing here is making sure the button is visible when we select at least one record in the sub grid.

On the right, under Properties: Command,

  • Enable Rules > click Add Enable Rule
  • Add New Enable Rule

We will be brought to a rule under ENABLE RULES element.

  • Add Step
  • SelectionCountRule
  • AppliesTo: SelectedEntity
  • Minimum: 1 (feel free to adjust based on your needs)

Note: You can rename your buttons, commands and rules in RWB for easier reference. I’ve renamed the rule to <entity>.EnableRule0.atLeastOneIsSelected, for example.

Step 10: Publish the Button

Nice! Our button is now ready. On the top main menu of RWB, click Publish, and click OK to confirm.

Step 11: Test the Button

In your Model Driven App, navigate to the sub grid where you want to select the rows. In this example, we have a sub grid for Time Sheet entity, under Booking entity.

We have our button! Click it and you should see the ‘Flow triggered successfully!’ alert popping up. (This was included in the JavaScript. You can remove the alert from the JavaScript if required)






Popular Posts

Where to find the classic Advanced Find after Microsoft Update

If you used to go to Advanced Settings and see our trusty Advanced Find on the command bar, and suddenly it has disappeared after a Microsoft update, do not worry! Microsoft has again moved the old Advanced Find or I would even say hidden it, to drive us towards the Dataverse search. I encountered this in Dynamics 365 Customer Service and Field Service, so I'm not entirely sure it would affect Sales, Marketing and the rest. Highly likely all of them. Where to find the old / classic Advanced Find Click on the settings wheel and go to Advanced Settings     Click on the Dataverse search bar     and then click on ' Search for rows in a table using advanced filters '. This will bring up a side panel, with the 'Switch to Classic' button   Clicking this will bring you to the old / classic Advanced Find.   What's different now is that it opens up as another tab in the browser, unlike previously where it would be a pop-up. So you will be able to bookmark and save th...