Wednesday, July 15, 2015

Setting null value using Javascript.in MS CRM

I had an odd error where I was trying to null a lookup field in Javascript in CRM 2013.When the user changed a lookup field, I wanted to blank out a few fields to make them select those fields again.  The fields being nulled were dependant on selection of the first lookup field and the options and values they could pick changed.
if (Xrm.Page.getAttribute("field1").getValue() != null) {
    Xrm.Page.getAttribute("field1").setValue(null);
}
if (Xrm.Page.getAttribute("field2").getValue() != null) {
    Xrm.Page.getAttribute("field2").setValue(null);
}
field1 was correctly set to null but field2 refused to be set to null
I changed my code to set the required level to none, set field2 to null and then reset the required level to required.

//I had to set required level to none because assign null wasn't working, might be fixed in future roll ups
    if (Xrm.Page.getAttribute("field1").getValue() != null) {
            Xrm.Page.getAttribute("field1").setValue(null);
        }

        Xrm.Page.data.entity.attributes.get("field2").setRequiredLevel("none");
        if (Xrm.Page.getAttribute("field2").getValue() != null) {
            Xrm.Page.getAttribute("field2").setValue(null);
        }

        Xrm.Page.data.entity.attributes.get("field2").setRequiredLevel("required");

No comments:

Post a Comment

Get files of last hour in Azure Data Factory

  Case I have a Data Factory pipeline that should run each hour and collect all new files added to the data lake since the last run. What is...