Announcements
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>
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:
André Arnaud de Cal...
294,261
Super User 2025 Season 1
Martin Dráb
232,996
Most Valuable Professional
nmaenpaa
101,158
Moderator