Notifications
Announcements
Hi everyone, I am trying to trigger process workflow on custom button click using javascript on power pages, But the post request is giving me unauthorized error.
Please help if anyone is aware how we can achieve this.
What Javascript are you using to trigger the workflow?
@fm_skeller
I am using below javascript code
function triggerOnDemandWorkflow() {console.log("Inside Function");var entityId = document.getElementById('EntityFormView_EntityID');var entity = {"EntityId": entityId.value};console.log(entityId.value);var WorkflowId = "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX";fetch("https://oooooo.api.crm.dynamics.com/api/data/v9.2/workflows(" + WorkflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow",{body: "{\"EntityId\":\"" + entityId.value + "\"}",credentials: "same-origin",headers: {"Accept": "application/json","Content-Type": "application/json; charset=utf-8","OData-MaxVersion": "4.0","OData-Version": "4.0"},method: "POST"}).then(response => console.log("Success:", response)).catch(error => console.error("Error:", error));
}
and this is the error which I am getting on execution above function on button click
@Pragatij the way you are trying to call the workflow won't work. You cannot access the CRM webapi from the portal.From power pages, you can call a workflow like this:
var url = "/_services/execute-workflow/<your website id>"; var data = {}; var workflowReference = {}; workflowReference.LogicalName = "workflow"; workflowReference.Id = workflowid; data.workflow = workflowReference; var entityReference = {}; entityReference.LogicalName = entityName; entityReference.Id = id; data.entity = entityReference; var json = JSON.stringify(data); shell.ajaxSafePost({ type: "POST", contentType: "application/json", url: url, data: json }).done(function() { //workflow executed successfully }).fail(function(e) { //workflow failed for some reason })
Can it be a power automate instead? There is native support for calling those now: https://youtu.be/JS16v3xBLFU
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
In our never-ending quest to improve we are simplifying the forum hierarchy…
We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Fubar 70 Super User 2025 Season 2
Jerry-IN 55
sannavajjala87 31