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

Notifications

Announcements

No record found.

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>

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    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

Responsible AI policies

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

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 45 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 36

#3
Satyam Prakash Profile Picture

Satyam Prakash 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans