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

Notifications

Announcements

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 70 Super User 2025 Season 2

#2
Jerry-IN Profile Picture

Jerry-IN 55

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard