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.
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.
Can it be a power automate instead? There is native support for calling those now: https://youtu.be/JS16v3xBLFU
@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
})
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
What Javascript are you using to trigger the workflow?