Search code examples
javascriptdynamics-crmdynamics-crm-365

Unable to set value to custom field Dynamics 365


Background

  • I have created a custom field in Dynamics 365 named new_statecode.

  • I am using an HTML webresource to get input from the user, that I later intend to pass to the custom field mentioned above.

    <input id="statecode" type="text" placeholder="">

  • I have written a Java script to get the value entered by the user and set the value in the custom field.

This is the code for the script.

document.getElementById("submitButton").addEventListener("click", function (event) {
    var strstatecode = document.getElementById("statecode").value;
    window.parent.Xrm.Page.data.entity.attributes.get("new_statecode").setValue(strstatecode);
});

The problem

This is the error I am getting in the console.

Unable to get property 'setValue' of undefined or null reference

I get a similar error when trying to use getValue property too.

Apparently this is a very common issue but none of the other solutions work for me.

What I have done so far

  • I have looked here, here, here and at tons of other posts.

  • I have checked the custom field name. There is no typo or spelling mistake.

  • I have tried using window.parent.Xrm.Page.getAttribute("new_statecode").setValue(strstatecode);

  • I have tried creating a new custom field and tried to set the value to it. That did not work either. It gives the same error.

Note1: I think it is worth mentioning that the code I have posted has been edited and shortened to match the mcve standards of SO. There are 6 other inputs (Street address, City, etc.) in my HTML webresource and I am successfully able to save the data to their respective fields in CRM. The only difference is that those are not custom fields.

Note2: I am using Dynamics 365 Version 1612 (8.2.1.410).

How do I fix this problem? Is there anything else I should look at? Maybe something I should have paid attention to while creating that custom field?

Forgive my lack of knowledge in CRM jargon.


Solution

  • An attribute/field needs to be present on a form to be reachable by Xrm.Page.getAttribute(attributeName).

    However, it can be hidden, or placed in a hidden section or tab.