Trigger Power Automate Workflow for selected item via Button in Form

Hi there,

I have a form configured on a sharepoint list item. I have a button inside the form that I would like to trigger a power automate workflow. I have followed the same method referenced in your post here Start a sharepoint workflow from form button - #2 by mnikitina. I have managed to successfully achieve this for a list or library control button, but not on a button control configured in a form on a SP list item. Steps I followed below.

  1. Created power automate workflow: "When an http request is received"
  2. Paste the body JSON schema:
    {
    "type": "object",
    "properties": {
    "ids": {
    "type": "array",
    "items": {
    "type": "integer"
    }
    }
    }
    }

This was found in the article. Add start flow button to the toolbar of List or Library — SharePoint forms

  1. I then get item using the ids in an apply to each. However see attached image. I am always getting this error. This didnt happen on the library or content control button workflows.

Any guidance would be much appreciated.

Hello @mhannaway,

The JSON schema doesn't match the schema from the screenshot. The output of the HTTP request is a string, not an array, that is why are getting the error.

How do you trigger the flow? Do you trigger the flow for the items selected in a List or Library control?
Please share the flow settings, make the screenshot of the steps.

Hi @mnikitina,

Apologies let me explain a little more. I am triggering the flow via a button control in a Sharepoint Form. The form opens after editing a SP List Item. I want to be able to trigger a flow after selecting the button. See screenshots below.

image

When I select the button, it actually triggers the flow so I believe I have the code in the Click section correct. But something must be wrong in my flow. Flow steps are below. What I am trying to do in the flow is trigger a get item to get the item I am editing and do a bunch of actions after that. Based on the article, once I receive a HTTP request, I can then get item, which will auto configure it into an apply to each. This was the case when I applied this to my list or library control but in this example, its not working in relation to a button.

Is my JSON schema incorrect? This is the JSON I used which I pasted into the sample payload to generate schema.

{
"type": "object",
"properties": {
"ids": {
"type": "array",
"items": {
"type": "integer"
}
}
}
}

@mhannaway,

If you want to trigger the flow for one item, you need to adjust the JSON schema in the «When a HTTP request is received» action:

{
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    }
  }
}

And update the code:

var url = 'https://prod-08.southeastasia.logic.azure.com:443/workflows/b9d325ca06be4da1b4ebdbc84'

            fetch(url, {
                method: 'POST',
                //send the current item id
                body: JSON.stringify({id: fd.itemId}),
                headers:{
                'Content-Type': 'application/json'
                }
            })

Hi @mnikitina

Thank you. I followed your steps but still am running into an issue. See below configuration.

My on click code for the button. Is this correct?:
image

Strangely, Im still running into this error in the flow. I pasted your schema above into the sample payload to generate schema and this was the schema it returned. Im not sure why but if I dont do an apply to each I cannot get the item ID. No dynamic content pops up. How do I retrieve the item ID after HTTP request is received?


This is the schema pasted into the "use sample payload to generate schema".

{

"type": "object",

"properties": {

    "type": {

        "type": "string"

    },

    "properties": {

        "type": "object",

        "properties": {

            "id": {

                "type": "object",

                "properties": {

                    "type": {

                        "type": "string"

                    }

                }

            }

        }

    }

}

}

Apologies the schema above is what was returned after pasting the JSON schema you provided you in your previous comment.

@mhannaway,

You need to specify the flow URL:
image

And use the variable you've declared:

var itemIds = fd.itemId;
fetch(url, {
    method: 'POST',
    body: JSON.stringify({id: itemIds}),
    headers:{
    'Content-Type': 'application/json'
    }
})

And you need to updated the JSON schema and run the flow on button click, not from Test flow with the recently used trigger.

{
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    }
  }
}

Hi @mnikitina

I'm sorry I have tried that. Below is a screenshot of my click code.

My flow is as follows:

I pasted the JSON Schema you provided into the "When a HTTP Request is recieved" (use sample payload to generate schema) above and ran the flow by clicking the button. I still am getting this error. The request body JSON schema returns despite pasting your code in:
{

"type": "object",

"properties": {

    "type": {

        "type": "string"

    },

    "properties": {

        "type": "object",

        "properties": {

            "id": {

                "type": "object",

                "properties": {

                    "type": {

                        "type": "string"

                    }

                }

            }

        }

    }

}

}

@mhannaway,

It should return the new schema even if the flow runs with errors.

You should also place the next action outside of the «Apply to each» step.

That's correct it has returned the new schema and that's what I pasted above.

When I place the get item action outside of an apply to each. For some reason I don't get any dynamic content and can't identify an ID for the item.

@mhannaway,

This the code that triggers the flow on my form:

var url = 'https://prod-08.southeastasia.logic.azure.com:443/workflows/b9d325ca06be4dfdfdgfb737bf/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=BR08JfAaO8BMMIjj3zLWQkxmhMOXGJWvmO_LKZP7F-c'
//send a request to start flow
var item = fd.itemId;
fetch(url, {
    method: 'POST',
    body: JSON.stringify({id: item}),
    headers:{
    'Content-Type': 'application/json'
    }
})

These are my flow steps:

And this is the flow outcome:

Try creating a new flow from scratch with the exact same step and setting and test how it works.

Thank you @mnikitina This has worked as I started from scratch.

Appreciate your help.

2 Likes

Hello, I got an issue for this one. It is throwing error 401. Any idea how to get pass this?

Thank you in advance.

image