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 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

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

    Hi,

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

  • Ramakanta Profile Picture
    2,715 on at

    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

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    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
    2,715 on at

    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 ...

  • Ramakanta Profile Picture
    2,715 on at

    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");
    }

      

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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
    2,715 on at

    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

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    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
    2,715 on at

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans