Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

CRM Button Web resource

(0) ShareShare
ReportReport
Posted on by 30

I have a question, could somebody tell me how to trigger a button from web resource on form in order to display for example: entityName on every user click? I set setClientContext function on form load and it's display alert with entity name, but how to trigger this function on click instead of load?

<script>
function setClientContext(xrm, formContext){

globalThis.Xrm = xrm;
globalThis.formContext = formContext;

var entityName = formContext.data.entity.getEntityName();

Xrm.Utility.alertDialog("Hello!! " + entityName);
}

</script>

<button onclick="setClientContext(xrm, formContext)">Click Me!</button>

  • Verified answer
    Community Member Profile Picture
    on at
    RE: CRM Button Web resource

    Hi q1938484,

    In fact, the function in web resource wouldn't trigger when the form is onload beside you add onload function on web resource or add onload event on your form.

    In your Case, It seems that you add onload event on your current form in which you pass Xrm and formContent to your web resource. The onload event's code should be something like this:

    function form_onload(executionContext) {
        var formContext = executionContext.getFormContext();
        var wrControl = formContext.getControl();
        if (wrControl) {
            wrControl.getContentWindow().then(
                function (contentWindow) {
                    contentWindow.setClientContext(Xrm, formContext);
                }
            )
        }
    }

    You could check the onload function on the current form.

    And you needn't modify it, you just need to change your html like this:

    <html><head>
    <meta charset="utf-8">
    <title>test</title>
    <meta><meta><meta><meta><meta></head>
    <body style="overflow-wrap: break-word;">
    <div>
    <button onclick="clickMe()">Click Me!</button>
    <div>

    <script type="text/javascript">
    function setClientContext(xrm, formContext){
    globalThis.Xrm = xrm;
    globalThis.formContext = formContext;

    }

    function clickMe(){
    var entityName = formContext.data.entity.getEntityName();
    Xrm.Utility.alertDialog("Hello!! " entityName);
    }
    </script>

    </div></div></body></html>

    And it would be triggered when you click the button:

    pastedimage1637803566884v1.png

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,996 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans