CRM 2011 field On Change Event Javascript getting Value using XRM.Page.getAttribuites(fieldName).getValue()

Proposed Answer CRM 2011 field On Change Event Javascript getting Value using XRM.Page.getAttribuites(fieldName).getValue()

  • 18 Juli 2011 18:58
     
     

    Hi I am experiencing a strange behaviour when I try to get a field value Xrm.Page.getAttribute(totalAmountFieldName).getValue(); on change. It gives the previous value not the current changed value.

    but If i use  window.crmForm.all.totalAmountFieldName.DataValue; this gives the correct change value.

    Ex :

    Total Amount = 100

    Changed to 200 and the Onchange event fires I get 100 from Xrm.Page.getAttribute(totalAmountFieldName).getValue(); But window.crmForm.all.totalAmountFieldName.DataValue gives the correct value 200.

    Why is it behaving different? Can some one help.


    Thanks, Future MVP

Semua Balasan

  • 19 Juli 2011 18:43
     
     
    That's strange. Do you make the change through UI or JavaScript code?
    Daniel Cai | http://danielcai.blogspot.com
  • 19 Juli 2011 21:07
     
     

    you are right that is really strange. The Change happens through UI and I have capture the new Value and perform some caluculations based of it in the javascript


    Thanks, Future MVP
  • 19 Juli 2011 23:50
    Moderator
     
     Saran Jawaban Memiliki Kode

    Do you have another field on the form that holds the value "100"?  The reason I ask, is because getAttribute() accepts one of 4 argument types:

    1. None
    2. Integer
    3. String
    4. Function (as a delegate)

    So, you're supposed to call it like this:

    Xrm.Page.getAttribute("totalAmountFieldName").getValue();
    

    ... with the quotes surrounding the field name.  Without the quotes, it's difficult to tell how the getAttribute() method would treat totalAmountFieldName.  On one hand, it may not be defined, and possibly treated as a "0" value, in turn returning the first attribute available to the form.  Perhaps you simply forgot the quotes when you typed up your original post.  Either way, make sure your code has them and report back here whether it makes a difference.

    On the other hand, assuming you've made a typographical error in your post that doesn't reflect the actual code, the situation sounds suspiciously like either a race-condition, or some JS encapsulation behavior.  In that case, I would ask how you registered the code to the OnChange event: was it in the attribute properties from the form editor, or is it attached by code through the addOnChange() method?


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    • Disarankan sebagai Jawaban oleh Jim Glass Jr 24 Juli 2011 20:59
    •  
  • 20 Juli 2011 4:04
     
     
    Good catch, Dave. Hope that's the cause. 
    Daniel Cai | http://danielcai.blogspot.com
  • 20 Juli 2011 10:50
    Moderator
     
     

    I've seen similar behaviour before for a optionset field, where if you try to get the value during the onchange of the same field it will not get the latest as the underlying object hasn't updated yet.

    If you check the value in a nother field onchange after this step you will get the correct value.

    I haven't seen this behaviour with a number field before though.

    What rollup are you on? Not sure this is the problem but curious.


    MS CRM Bing'd - http://bingsoft.wordpress.com
    Useful Tools  CRM 4 to CRM 2011 JavaScript Converter Tool
    CRM 2011 OData Query Designer
    CRM Forum Guidance on how to Help Us Help You
  • 20 Juli 2011 13:36
     
     

    Dave thanks for the detailed explanation on how getAttribuite() accepts the argument types. I actually do have the quotes around the field ..  here is wat my code looks like

    Var totalAmountFieldName=

    "sl_totalamount";

    var newAmountOnChange =  Xrm.Page.getAttribute(totalAmountFieldName).getValue();

     

    To answer the second question

    Code is registered through form editor.  Hope this helps in invstigating my issue. 

    This not the only attribuite that is having issue. Even the LookUP's are beaving the same way.


    Thanks, Future MVP
  • 20 Juli 2011 13:55
     
     

    Hey Clinton, I am glad I am not the only one experiencing this issue. I am having the issue on LookUp's , Money and Decimal attributes so far.

    As far as the roll up goes nto sure but I am CRM 2011 Build Version 5.0.9688.594

    Just off topic thanks for your "OData Query Designer" tool . I am using it and is really powerful and easy to use.


    Thanks, Future MVP
  • 20 Juli 2011 14:16
    Moderator
     
     

    No worries, my tools are out there so others can benefit, I'm glad you like it. There will be some additional features coming soon as well as a new tool.

    With regards your issue, I noticed the same behaviour your experiencing ages ago but haven't come across it lately. You are on the standard build so if you're able, go and get Rollup 2 and see if that sorts it out. http://support.microsoft.com/kb/2466086 

     


    MS CRM Bing'd - http://bingsoft.wordpress.com
    Useful Tools  CRM 4 to CRM 2011 JavaScript Converter Tool
    CRM 2011 OData Query Designer
    CRM Forum Guidance on how to Help Us Help You
  • 20 Juli 2011 15:47
     
     
    Alright I will give it a try and let you know how it goes. Thanks
    Thanks, Future MVP
  • 20 Juli 2011 17:44
    Moderator
     
     
    Sounds like you don't have any Update Rollups applied, based on your version number.  I second Rhett's recommendation that you might want to give UR 2 a try, and see if the behavior is different.  In my experience, Microsoft doesn't report every bug they fix in an Update Rollup--which is different from a Fruit Rollup in that they're less tasty.
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
  • 23 Juli 2011 3:27
     
      Memiliki Kode

    Future, can you see if using the sourceObject you get the same old value?

    I just used the following code to quickly check this on a text and whole number field and in both cases I was getting the new value I typed. In one case it went from empty to a value, the other I was just adding to the value.

    function sbj_onchange(obj) {
      var attrib = obj.getEventSource();
      alert(attrib.getValue());
      alert(Xrm.Page.ui.getCurrentControl().getAttribute().getValue());
    }
    


    Make sure you add the checkbox for the execution context in the event property dialog.

    Hope this helps,
    Rich Dickinson
    Program Manager - Dynamics CRM

  • 25 Juni 2012 12:40
     
     

    Hi,

    This is happening to me as well and I'm at rollup 6.

    I wrote a function for a JScript command in the ribbon. When the user presses a button, the jscript is fired, but the Xrm.Page.getAttribute("field").getValue() shows the previous value of the field.

    Thanks,

    Florin