Skip to main content

Registering Form Event Handlers In JavaScript

Something I have started looking at recently is registering form event handlers within the code. I personally think it allows anyone to review the form to quickly seeing all event handlers in one place without having to flick through both the form and the JavaScript file. This is done using

formContext.getAttribute(‘my_fieldname’).addOnChange(MyOnchangeFucntion)

Documentation

I would usually call separate register handlers function on form load to register all my handlers. Example below

// OnLoad function called from the from event handler
function OnLoad (executionContext){
    //do other on load stuff
    //rigster all handlers
    RegisterHandlers(executionContext);
}
function RegisterHandlers(executionContext){
    var formContext = executionContext.getFormContext();
    formContext.getAttribute('my_fieldname1').addOnChange(my_fieldname1_Onchange);
    formContext.getAttribute('my_fieldname2').addOnChange(my_fieldname2_Onchange);
    formContext.data.entity.addOnSave(myFunction);
}
function my_fieldname1_Onchange(executionContext){
    //do on change stuff
}
function my_fieldname2_Onchange(executionContext){
    //do on change stuff
}
function OnSave(executionContext){
    //do on save stuff
}

At a glance this allows you to see which fields have on change functions registered on them and make changes in one place

Charles Osei

Dynamics 365 Consultant @ Hitachi MCSE Business Applications Computer Science ā€“ University of Lincoln 2017 Nottingham D365/CRM user group Chapter Leader TDG ā€“ Community leader Twitter @charlied3v LinedIn https://www.linkedin.com/in/charlesosei/

Leave a Reply