Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Programmatically register On-change Event handler (for a form) in Dynamics CRM?

(0) ShareShare
ReportReport
Posted on by 2,715

I have added a script as a Form Library. It contains code(funtion) that needs to be executed on field change event.I don't want to register it manually,all i want to do using Programmatically i.e. by updating the formxml, is any one having the sample code(C#)/link which describes  for the same

*This post is locked for comments

  • Ramakanta Profile Picture
    Ramakanta 2,715 on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    Here is the link crmdynamicsblog.wordpress.com/.../add-form-library-on-crm-form-using-c-or-javascript

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    Hi RK,

    I am not sure about self invoking functions. From where you got this term?

    Are you referring to to attaching the method to field on change directly?

  • Ramakanta Profile Picture
    Ramakanta 2,715 on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    Thanks Ravi,It works as expected.I also find out that we can  achieve it  by using Self-Invoking  function.Can you please suggest ,what will be the best option in my scenario

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    Hi RK,

    Try this code, paste it as-is in your web resource. This is same as Alex suggested above.

    ===================

    function RegisterEvents()

    {

    debugger;

    if (typeof Xrm == 'undefined' || Xrm == null) setTimeout(function () { RegisterEvents(); }, 200);

    else

    {

    Xrm.Page.getAttribute("subject").addOnChange(test1);

    // Xrm.Page.getControl("subject").getAttribute().addOnChange(test1);

    }

    }

    function test1()

    {

    alert("calling");

    }

    RegisterEvents();

    ============================

    Hope this helps

  • Ramakanta Profile Picture
    Ramakanta 2,715 on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    comm.jpg

    Hi Alex,

           I had added the FormLibrary ,now i am trying to register a function which is present inside the library on OnChange of an attribute,i had followed your suggestion but unfortunately my onchange event is not triggering,i am trying in  Lead Form,below is the code that i have used,is there anything i am missing?

    function test()
    {
    debugger;
    RegisterEvents();
    }
    function RegisterEvents()
    {
    debugger;
    if (typeof Xrm == 'undefined' || Xrm == null) setTimeout(function () { RegisterEvents(); }, 200);
    else
    {
    Xrm.Page.getAttribute("subject").addOnChange(test1);
    // Xrm.Page.getControl("subject").getAttribute().addOnChange(test1);
    }
    }
    function test1()
    {
    alert("calling");
    }

      

  • Ramakanta Profile Picture
    Ramakanta 2,715 on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    Thanks Alex for quick response,My requirement is to Add JS Library into the FORM(which i have already done) and now i am looking to register my function on OnChange Event.Now i will try your suggestion ...

  • Verified answer
    ashlega Profile Picture
    ashlega 34,477 on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    If you are ok at least adding that web resource to the form, you can do something like this in your script:

    RegisterEvents();

    function RegisterEvents()

    {

       if(typeof Xrm == 'undefined' || Xrm == null) setTimeout(function(){ RegisterEvents(); }, 200);

       else{

          Xrm.Page.getAttribute(arg).addOnChange(...);

       }

    }

    If you don't want to even add that web resource to the form manually, you'll have to update form Xml I believe:

    https://community.dynamics.com/crm/b/journeyintocrm/archive/2016/01/06/hardcore-crm-using-c-to-modify-an-entity-forms

  • Ramakanta Profile Picture
    Ramakanta 2,715 on at
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    Thanks Guillaume  for quick response,i don't want to register OnLoad event in order to add the OnChange event ,my intention is to register the function on OnChange of my target field

  • Suggested answer
    RE: Programmatically register On-change Event handler (for a form) in Dynamics CRM?

    Hi,

    you could simply register it in your Javascript file: msdn.microsoft.com/.../gg334409.aspx

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February 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... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans