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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Web resource button to trigger a Power Automate Flow

(0) ShareShare
ReportReport
Posted on by 45

Hi I have created a button with an HTML resource and I need it to post the entity GUID to a Power Automate flow with a "When a HTTP request is received" trigger.

I'm using postman to do testing, which is working, but the code that I use from it is not, can someone please help?

Code:




    
    



    


Dynamics 365 Sales Professional - "crm6"

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Web resource button to trigger a Power Automate Flow

    Hello,

    What error do you get when you call this Flow from your code?

  • Suggested answer
    cloflyMao Profile Picture
    25,210 on at
    RE: Web resource button to trigger a Power Automate Flow

    Hi Sam,

    1. As following tutorial said:

    https://carldesouza.com/how-to-use-formcontext-in-a-web-resource-in-dynamics-365-power-apps/

    To use formContext in web resource, firstly we need to pass it from Form to our Web Resouce:

    (Run form_onload function at OnLoad event of form.)

    function form_onload(executionContext) {
        var formContext = executionContext.getFormContext();
        var wrControl = formContext.getControl("WebResource_MyWebResource");
        if (wrControl) {
            wrControl.getContentWindow().then(
                function (contentWindow) {
                    contentWindow.setClientApiContext(formContext);
                }
            )
        }
    }

    Secondly, in the web resource, we need to add an extra function setClientApiContext to set formContext as global variable on the web resource page.

    function setClientApiContext(formContext) {
        window._formContext = formContext;
    }

    2. Now in PostFlow function, the formContext can be used directly. 

    However, we should use formContext.data.entity.getId() to get entity GUID instead, because formContext.getAttribute() only works for attribute that has been added to Form.

    var quote = _formContext.data.entity.getId().replace(/\{|\}/gi, "").toLowerCase();
     

    Full code in script tag of your web resource:

    function setClientApiContext(formContext) {
        window._formContext = formContext;
    }
    
    function PostFlow() {
        var quote = _formContext.data.entity.getId().replace(/\{|\}/gi, "").toLowerCase();
        // Same to original
    }

    setClientApiContext  function will be automatically loaded.

    Regards,

    Clofly

  • Sam Hef Profile Picture
    45 on at
    RE: Web resource button to trigger a Power Automate Flow

    Hi Clofly,

    Thank you for the assistance, I've tried doing as you recommend but it doesn't seem to be making any difference and I'm not getting any errors. Is there anything else I might be able to try?

  • Sam Hef Profile Picture
    45 on at
    RE: Web resource button to trigger a Power Automate Flow

    13311.Error.PNG

  • cloflyMao Profile Picture
    25,210 on at
    RE: Web resource button to trigger a Power Automate Flow

    Hi Sam,

    1. Please check whether executionContext has been passed to  form_onload function.

    (If you are using ExecutionContext, then the line 1 should be changed to var formContext = ExecutionContext.getFormContext();. But it is recommended to use executionContext instead.)

    pastedimage1607044578328v1.png

    2. Run debugger; at beginning of PostFlow function to check whether the formContext has been passed to your function successfully.

    pastedimage1607045004087v2.png

    3. Because you are just running console.log to print message in console, thus you will see no difference even if the HTTP request trigger ran successfully.

    You could run alert in the second then block to confirm the success.

    fetch(url, requestOptions)
        .then(response => response.text())
        .then(result => alert("Ok"))
        .catch(error => console.log('error', error));

    w1.JPG

    Or check result in flow directly.

    Regards,

    Clofly

  • Sam Hef Profile Picture
    45 on at
    RE: Web resource button to trigger a Power Automate Flow

    Regarding your first question, that was set.

    Running the debugger I get this:

    new_CalculateTotalsButton:8 Uncaught TypeError: Cannot read property 'data' of undefined

       at PostFlow (new_CalculateTotalsButton:8)

       at HTMLButtonElement.onclick (new_CalculateTotalsButton:57)

    on this line:

    var quote = _formContext.data.entity.getId().replace(/\{|\}/gi, "").toLowerCase();

  • cloflyMao Profile Picture
    25,210 on at
    RE: Web resource button to trigger a Power Automate Flow

    Hi Sam,

    It means that formContext is still not passed from form_onload function.

    Please check whether you are using contentWindow.setClientApiContext(formContext); in the form_onload function.

    getContentWindow (Client API reference) in model-driven apps - Power Apps | Microsoft Docs

    Regards,

    Clofly

  • cloflyMao Profile Picture
    25,210 on at
    RE: Web resource button to trigger a Power Automate Flow

    setClientApiContext can be changed to other custom function name, but we need to keep names in both form_onload function and web resource are same.

    (We use getContentWindow() method to access the WebResource and then invoke it’s corresponding method.)

    Regards,

    Clofly

  • Sam Hef Profile Picture
    45 on at
    RE: Web resource button to trigger a Power Automate Flow

    Hi Clofly,

    I Indeed am using contentWindow.setClientApiContext(formContext); in the form_onload function. I realise where I went wrong, I had a script error so I disabled the script to try resolve the issue but forgot to turn it back on and assumed that whatever I had done had fixed the issue, but it turns out I didn't. So I'll just try do a complete post of everything I have so hopefully you can tell me what I'm doing wrong.

    Web Resource HTML:

    
    
    
        
        
        
    
    
    
        
    
    
    

    Web Resource Dynamics Information:
    2273.Button.PNG

    Form Context (Display Name):

    Form-Context.PNG

    Form Context Handler Properties:
    Form-Context-Handler-Properties.PNG

    Form Context Function:

    function form_onload(executionContext) {
        // var formContext = primaryControl.id;
        var formContext = executionContext.getFormContext();
        var wrControl = formContext.getControl("WebResource_calculate_Totals");
        if (wrControl) {
            wrControl.getContentWindow().then(
                function (contentWindow) {
                    contentWindow.setClientApiContext(Xrm, formContext);
                }
            )
        }
    }

    Script Error:

    6574.SCript-Error.PNG

  • cloflyMao Profile Picture
    25,210 on at
    RE: Web resource button to trigger a Power Automate Flow

    Hi Sam,

    At OnLoad event, I can see that you are running two form_onload functions that are from two libraries: new_quote.js and new_form_context: the first ranked function may has impact on the second one due to they have same name.

    Please check whether there is ExecutionContext in form_onload of the new_quote.js and disable the function to run new_form_context only.

    Regards,

    Clofly

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 130

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 118 Super User 2025 Season 2

#3
Erin Lubben Profile Picture

Erin Lubben 57

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans