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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Run function on button click and not onload of webresource

(0) ShareShare
ReportReport
Posted on by 65

I have below html webresource  whcih update the selected value from optionset  on field in CRM form but it is running on load and onchange. I want it to work on click on the button only. Once I have chosen the option and click on the button it should get updated in CRM. Any suggestions.

<html><head>
<title>Metadata Demonstration</title>
<script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script type="text/javascript">

function startSample() {
var select = document.getElementById("combobox");
select.onchange = function () {
var index = this.selectedIndex;
var selected = this.options;
var selectedValue= selected[index].text;
window.parent.opener.Xrm.Page.data.entity.attributes.get("new_deletion").setValue(selectedValue);
};
}

</script>
<meta></head>
<body onload="startSample()" onfocusout="parent.setEmailRange();" style="overflow-wrap: break-word;">
<select id="combobox">
<option value="Test1">Testing1</option>
<option value="Test2">Testing2</option>
<option value="Test3">Testing3</option>
</select>
<button onclick="startSample()">Click me</button>

</body></html>

I have the same question (0)
  • Suggested answer
    Carlo Velasco Profile Picture
    787 on at

    Hi

    Please refer to the following post.

    community.dynamics.com/.../how-to-add-a-button-to-a-form

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Try below code and see if it works.

    <html><head>

    <title>Metadata Demonstration</title>

    <script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>

    <script type="text/javascript">

    function startSample() {

    var select = document.getElementById("combobox");

    select.onchange = function () {

    var index = this.selectedIndex;

    var selected = this.options;

    var selectedValue= selected[index].text;

    window.parent.opener.Xrm.Page.data.entity.attributes.get("new_deletion").setValue(selectedValue);

    };

    }

    function startSample_ButtonClick() {

    var select = document.getElementById("combobox");

    var index = select.selectedIndex;

    var selected = select.options;

    var selectedValue= selected[index].text;

    //alert(selectedValue);

    window.parent.opener.Xrm.Page.data.entity.attributes.get("new_deletion").setValue(selectedValue);

    }

    </script>

    <meta></head>

    <body onload="startSample()" onfocusout="parent.setEmailRange();" style="overflow-wrap: break-word;">

    <select id="combobox">

    <option value="Test1">Testing1</option>

    <option value="Test2">Testing2</option>

    <option value="Test3">Testing3</option>

    </select>

    <button onclick="startSample_ButtonClick()">Click me</button>

    </body></html>

    If found helpful, Please mark my answer verified.

  • cloflyMao Profile Picture
    25,210 on at

    Hi Ravinder,

    You can use this Syntax to achieve it.

    Xrm.Page.data.entity.save();

    Since all of your changes occur in the front end, you need to add following code to the js code to save changes.

    Add this code to the js:

    window.parent.Xrm.Page.data.entity.save();

    Like this:

    3527.image001.png

    Only in this way, you can save your changed records after clicking button.

    Regards,

    Clofly

  • Suggested answer
    Ravinder Singh Jamwal Profile Picture
    65 on at

    Hey Bipin Thanks. Seems like you forgot to remove the initial function. It works though.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Glad that it worked. Please mark my answer verified if I were helpful.

    If found helpful, Please mark my answer verified.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans