web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Not able to trigger Po...
Power Pages
Unanswered

Not able to trigger PowerApps process workflow through javascript in power pages

(0) ShareShare
ReportReport
Posted on by 44

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.

Categories:
I have the same question (0)
  • fm_skeller Profile Picture
    277 Moderator on at

    What Javascript are you using to trigger the workflow?

  • Pragatij Profile Picture
    44 on at

    @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_0-1706867545576.png

     

  • fm_skeller Profile Picture
    277 Moderator on at

    @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
    }) 
  • Peragin Profile Picture
    160 on at

    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.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
Valantis Profile Picture

Valantis 59

#2
11manish Profile Picture

11manish 51

#3
omkarsupreme Profile Picture

omkarsupreme 27

Last 30 days Overall leaderboard