Search code examples
javascriptdynamics-crmcrmdynamics-crm-2015dynamics-crm-2016

Can I pass a value to js function from crm field


I have two fields that use the same function (called by OnChange event), I wonder if there is an option to pass the field's value to javascript function (for validation) without using XRM.

Write a function with the Xrm.Page.getAttribute("field").getValue(); - is the only option that I have?

Do I have to write two different functions to two different fields that calls the same function ?


Solution

  • From CRM form control perspective, you can have a defined library which contains below generic function.

    function generic_onchange(execContext){
        var myControl = execContext.getEventSource().getName();
        var myControlValue = execContext.getEventSource().getValue();
    }
    

    When you configure the onchange event for your both controls with the above function, select the checkbox "Pass execution context as first parameter".

    Update:
    When addOnChange is being used to attach function to event, execution context is passed by Default as the first Parameter.

    Reference

    The function will be added to the bottom of the event handler pipeline. The execution context is automatically set to be the first parameter passed to the event handler.