Search code examples
javascriptdynamics-crm-2011dynamics-crmcrmxrm

Could Xrm.Page.getAttribute() return null? Crm 2011


I'm having a weird behavior here,

I have a field "new_field", this field is in the form, the tab where it is, is hidden, the type is string and usually doesn't have value.

Sometimes Xrm.Page.getAttribute("new_field") brings me the field with value or not. Sometimes it gets null, this only occurs when there is no value on the field.

What could cause the field returns null?


Solution

  • Unfortunately, Xrm.Page.getAttribute("new_field") is not very robust and it can, indeed, return null sometimes. So, as a good practice, you could check for nulls like this:

    var value = Xrm.Page.getAttribute("new_field") ? Xrm.Page.getAttribute("new_field").getValue() : null;
    

    We even put helper methods like that in a separate .js file we reuse everywhere.

    Hope this helps!