"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу

Wednesday 18 March 2015

JS Development in Dynamics CRM- Tips

    This post is regarding a javascript development tip which is very useful in Dyamics CRM. It was not my idea but my colleagues' (Irina and Alex). And it was an eye opener for me. As we all know we write js code on form load as well as on change event of different fields in Dynamics CRM. At a later point have you ever struggled to see on which fields we have js code ?  I have. Their suggestion was to use attach events by using the addonchange js function in Dynamics CRM. They also mentioned that this would make the js code more transparent. addonChange function is nothing but a js function to be called when the attribute changes. Please read the comments below. Thanks to Irina and Alex for this recommendation.


//Form Load
//onLoad function should be added to the Form load event of Form editor as usual
function onLoad() {

    FillData();// FillData Function call on Form load
    attachEvents();
}


//Function to attach events to different fields
function attachEvents() {

    //Attach Onchange event of lookup1(new_City) and lookup2(new_Profession)
    //You could attach as many fields as you prefer
    //Please note that the function name is passed as a parameter to addonchange function
    //Its the function we would like to call during onchange event of these fields.
    Xrm.Page.getAttribute("new_City").addOnChange(FillData);
    Xrm.Page.getAttribute("new_Profession").addOnChange(FillData);
    //No need to add FillData function call on the onchange event of each field on
    //the Form editor instead here we attach it using addOnChange function.
}

//Fill data based on lookup1 - new_City and lookup2 - new_Profession
function FillData() {

    //Js code to fill in the field based new_City and new_Profession
     //For instance you could fill another field using these 2 field values
}