Start a sharepoint workflow from form button

Hello,

I came across this A post of one of your collegs.

Can somebody give me a sample code of how I can start a workflow from a form button?

Thank you!

Hello @Vasilii_Burca,

Yes, you can start a flow on button click. Please find a step by step instruction in the Send selected items to Flow on button click article.

If you want to start the flow for the current item, you can use this code in the button's onclick property:

//this URL needs to be updated
var url = 'FLOW_URL';

//current item ID
var itemIds = fd.itemId;

//send a request to start flow
fetch(url, {
    method: 'POST',
    body: JSON.stringify({ids: itemIds}),
    headers:{
    'Content-Type': 'application/json'
    }
})
.then(function(response) {
    if (response.status !== 202) {
    alert('Looks like there was a problem. Status Code: ' + response.status);
    } else {
    alert('Success');
    }
})

Hello,
I meen not Microsoft flow but sharepoint List Workflow.
Is this code apropriate for that to ?

Hello @Vasilii_Burca,

I'm sorry, I've missed that you've added a SharePoint 2019 tag.

If you are using SharePoint On-premises, you can design the button that triggers the Workflow, and you don't need the complex code.

For this, in Workflow settings >> Start Options, you need to check 'Start Workflow automatically when an item is changed(or created)
image

This will make the workflow ran each time the item is changed.

To run workflow steps only when the button is clicked, you need to do the following:

  1. Create a new Yes/No field in the list, call it StartFlow.
  2. Edit the workflow. Place all workflow steps under the condition that checks if the StartFlow equals Yes. And at the end of the steps add the action that sets the StartFlow field to equal No.
    image
  3. Finally, open the form in the designer. Add the custom button, and add this code to General >> Click. The code sets the field value and saves the form.
    image
fd.field('StartFlow').value = true;
return fd.save();
1 Like

Thank you that helped a lot !

1 Like

Hello,
this is exactly what I need, but it doesn't seem to work for me. I am using forms classic. Is this the same code?

Hello @David_E_Garza,

The approach would be the same, but the code for the button in Forms Designer for Classic UI is:

fd.field('StartFlow').value(true);
fd.save().click();

Please post questions regarding Forms Designer and Cross-Site Lookup to the forum.

1 Like

That worked. Thank you as always!

1 Like

I actually have an issue. It works when I have the field on display. However I wish to hide or make it read only. When I do this, something happens where it no longer works.
Here is my code:
fd.field('sendEmail').value(true);
alert("Email has been sent to Inventory Clerk");
fd.field('PercentComplete').value(50);
fd.save().click();

And here is my workflow:
plum1

Is there a way to hide it or make it read only and have the workflow still work?

@David_E_Garza,

If the field sendEmail is disabled on the form, you need to enable it before the submission in order to save changes:

fd.onsubmit(function () {
  fd.field('sendEmail').readonly(false);
  return true;
});

Please post questions regarding Forms Designer and Cross-Site Lookup to the forum.