Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Web resource button to trigger a Power Automate Flow

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"

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

    Hi Sam,

    In the form_load function, you are passing two parameters to setClientApiContext function of the web resource button page.

    contentWindow.setClientApiContext(Xrm, formContext);

    It may causes confusion to setClientApiContext function of the web resource, because there is only one parameter:

    function setClientApiContext(formContext) {

        window._formContext = formContext;

    }

    Please remove Xrm and only keep formContext to test again.

    Alternatively, if the error is still not resolved by above step, you could try following approach:

    1. Change the form_onload to:

    function form_onload(executionContext) {
        var formContext = executionContext.getFormContext();
        window.top._formContext = formContext;
    }

    Instead of passing parameters to web resource, we set formContext as a window level property.

    2. In the button page, remove setClientApiContext function. In PostFlow function, extract the formContext from window object directly.

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

    pastedimage1607478766310v2.png

    Regards,

    Clofly

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

    Hi Clofly,

    It looks like that one is not enabled, so I've removed it, but I'm still getting an error for "Cannot read property'data' of undefined"

  • cloflyMao Profile Picture
    cloflyMao 25,198 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

  • Sam Hef Profile Picture
    Sam Hef 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
    cloflyMao 25,198 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

  • cloflyMao Profile Picture
    cloflyMao 25,198 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

  • Sam Hef Profile Picture
    Sam Hef 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
    cloflyMao 25,198 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
    Sam Hef 45 on at
    RE: Web resource button to trigger a Power Automate Flow

    13311.Error.PNG

  • Sam Hef Profile Picture
    Sam Hef 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?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans