locked
This is lookup field OnChange event issue. RRS feed

  • Question

  • Hi,

    I'm using the form that have a lot of attributes.

    and I also have two lookup fields(A,B) and a text field(C) in the form.
    It was attached OnChange Event by javascript.

    Like this:

     crmForm.all.A.attachEvent("onchange", [FunctionName1]);
     crmForm.all.B.attachEvent("onchange", [FunctionName2]);

     

    When someone open the form, the form load .js file.
    and then, call the function of attaching event from .js file.
    I must stress that I don't use the form field OnChange Event. I'm just using attachEvent.
    (When I use the form field OnChange Event, everything is fine.)

    and I run the FireOnChange() in the Form OnLoad Event.
    Like this:
    OnLoad = function()
    {
    crmForm.all.A.FireOnChange();
    }

    ----------------------------------------------
      A: lookup field
      B: lookup field
      C: text field
    ----------------------------------------------

    When I change a value of B in the lookup view, after I run a FireOnChange() of A in the Form OnLoad Event.
    In that case, OnChange Event of B is not fired.

    In two of belowing cases, OnChange Event is fine.
    1.  When I run a FireOnChange() of A, after I change a value of B
    (or run a FireOnChange() of B, after I change a value of A. What's field dosen't matter.)
    2.  When I change a value of A or B after I run a FireOnChange() of C.
    (I think, It is fine when the field type is not lookup.)

    In the only one case, OnChange Event is not fired.
    1.  When I change a value of lookup field, after I run a FireOnChange() of lookup field.

    I also tried it after I remove other code.
    Is it CRM bug?

    I have found why this is happend for last week.
    Please, let me know it.
    I really want to know why.


    Thanks.
    JE.Song

     

    • Edited by JE.Song Friday, March 5, 2010 5:49 AM
    Wednesday, March 3, 2010 12:29 AM

Answers

  • User the keyword

    debugger;

    just before your above script and see if you can go through your code line by line. Step into the events and triggers to see if you can find why it isn't being triggered,


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Friday, March 5, 2010 10:22 AM
    Moderator

All replies

  • What mechanism are you using to "load" the external .js file?

    Dave Berry
    Wednesday, March 3, 2010 12:44 AM
    Moderator
  • Hi, Dave.

    You can refer to this link(http://httpcode.com/blogs/PermaLink,guid,b908badc-b3cb-4c5e-867f-245a62408fbe.aspx).
    It similar to my code.

    Thanks^_^
    JE.Song
    Wednesday, March 3, 2010 12:58 AM
  • Have you checked to see which versions of your code have been cached by the browser?  When you include external .js scripts the way you have, the browser will cache them locally, and your changes will not be immediately available.  Be sure to clear your browser's cache between updates.
    Dave Berry
    Wednesday, March 3, 2010 1:04 AM
    Moderator
  • Yes, I have tried it, after clean the browser's cache.
    and I have checked whether it is changed by using 'alert'.

    JE.Song

    Wednesday, March 3, 2010 1:11 AM
  • Ok, then my next suggestion is to not use FireOnChange, as I'm pretty sure that's a CRM-specific function that points to the function handler established by the customization's "OnChange" code.  You may want to try the fireEvent(); function instead:  http://snipplr.com/view/15300/fire-an-event-manually-on-ie--5/

    Dave Berry
    Wednesday, March 3, 2010 1:22 AM
    Moderator
  • Oh, and there is a behavior regarding the lookup field that I know from some of the more advanced programming I've done with it:  The element referenced by the path crmForm.all.field is actually the IMG element of the Lookup button on the field.

    As far as I'm aware, if that image element is replaced, your events may not be attached to its replacement.  This may happen in a situation where you use the automatic search function of typing directly in the field (when the image changes to the "search" image of rotating green dots, and then changes back), instead of clicking on the button to perform changes to the Lookup field.  I'm not certain if this is the behavior the field actually makes, but testing it would be simple:

    If your code works when you click on the Lookup button to edit the contents of the Lookup field, but does not work anytime after you make edits directly within the field, then I'm correct.
    Dave Berry
    Wednesday, March 3, 2010 1:32 AM
    Moderator
  • Hi, Dave.

    I have tried the fireEvent(); function as you told me.
    When I used it, next OnChange event hasn't any problems.
    It is obviously a great way. but, It's just not for me.
    because, I should dynamically control the onchange events.
    so, I already attached an OnChange event in a function of .js file.
    but, fireEvent() need to specify the function that will be fired on changed.
    Like this:
     document.getElementById('FormFieldID').fireEvent('onchage', this.function_onchange());

    Maybe, there is a way to get an OnChange event object dynamically from form field element.
    then, It may apply to this case. Is there any kind of ways like that?

    anyway, I have also tried your test case.
    I clicked on the lookup button to change a value, after I make edit directly within the field.
    (I have entered the text in the lookup field. and then, entered 'tab' key.)
    That was fine. It fired an OnChange event in that case.
    So, I think, It's not about changing image element.

    anyway, thank you for your response.
    and please let me know, if you'll have any idea about it.


    Thanks.
    JE.Song
    Wednesday, March 3, 2010 6:13 AM
  • Incorrect.  See http://msdn.microsoft.com/en-us/library/ms536423%28VS.85%29.aspx.  All you need is:

    crmForm.all.field.fireEvent('onchange');

    Which isn't significantly more difficult to execute or write than the alternative.  The only other recommendation I have is to use a "global" function pointer in your OnLoad code, and reference it from your field-specific OnChange code:

    OnLoad:
    ----------------------------
    var someFunc = function() {
      alert("Yar, there be change here!");
    }
    ----------------------------

    OnChange:
    ----------------------------
    someFunc();
    ----------------------------

    This way, you can keep all the code in the OnLoad script, without having to manage or update code in several spots.
    Dave Berry
    Wednesday, March 3, 2010 5:18 PM
    Moderator
  • You can also use fieldName_onchnage0()


    Modify this

    crmForm.all.yourField.FireOnChange();

    with

    yourField_onchange0();

    MSCRM Bing'd - http://bingsoft.wordpress.com
    Wednesday, March 3, 2010 5:27 PM
    Moderator
  • Hi, Dave.

    I have already tried fireEvent('onchange');.
    but it dosen't work anything.
    Of course, I have already attached OnChange event programmacally before I run a fireEvent('onchange'). but it wasn't fired.

    and I also thank you for your tip.
    but I think, someFunc eventually needs to specify the function that should be fired on changed.
    Like this:
     someFunction = function(){
        this.customization_function_onchange();
     }
    and I have already the customization function for each form field.

    I really want to control OnChange events for each form field dynamically.
    I think, it's not proper for me.


    Thanks.
    JE.Song
    Thursday, March 4, 2010 2:13 AM
  • Hi, Rhett.

    First, thank you for your response.

    I have tried FieldSchema_onchange0();
    but it dosen't work. and there was no error.

    Is it right way which I have tried?
    Is there anything I should specify? Like Object Name or Form Object.



    Thanks.
    JE.Song
    Thursday, March 4, 2010 2:26 AM
  • Is the field readonly or disabled?
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, March 4, 2010 9:38 AM
    Moderator
  • Absolutely not. 

    And I have tried many test case and I found some strange behaviors on the lookup field.

    Assume that there are two of lookup schemas.(A, B)
    A have a value, but B is empty.

    And they have been attached OnChange Events programatically.
    Like this:
     crmForm.all.A.attachEvent("onchange", [FunctionName1]);
     crmForm.all.B.attachEvent("onchange", [FunctionName2]);


    In following cases, each OnChange event of lookup field dosen't work.
    1: When I try to change a value on the B, after I run a A.FireOnChange(); function.
    2: When I try to change a value on the A, after I run a B.FireOnChange(); function.

    In following cases, each OnChange event of lookup field is OK.
    1: When I try to change a value on the B, after I run a B.FireOnChange();
    2: When I try to change a value on the A, after I run a A.FireOnChange();
    3: When I try to change a value on the B, after I run a A.FireOnChange(); and a B.FireOnChange();.
    4: When I try to change a value on the A, after I run a B.FireOnChange(); and a A.FireOnChange();.


    I'm really wondering why this is happened.


    Thanks.
    JE.Song

    Friday, March 5, 2010 1:43 AM
  • User the keyword

    debugger;

    just before your above script and see if you can go through your code line by line. Step into the events and triggers to see if you can find why it isn't being triggered,


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Friday, March 5, 2010 10:22 AM
    Moderator