locked
Early binding: setting null value not working? RRS feed

  • Question

  • I'm having an odd problem with Early Binding in CRM 2011. As far as I know, it should be possible to set a CRM attribute to null to clear the value in CRM. This isn't working using the properties in the class generated by the Early Binding tool. Using the following line, the attribute keeps its previous value after updating:

    entity.new_certainattribute = null;

    If I change that exact line to the following, it does work:

    entity["new_certainattribute"] = null;

    And I'm not sure why.  Any help would be appreciated.

    Monday, November 24, 2014 8:13 AM

All replies

  • I do not think setting null works in early binding. You have to mention the data type of the respective attribute. For instance if it is something like a string variable then you can put it as string.empty or "". Please check this and let me know.

    Also it would be great if you can let me know the attribute type.


    Regards, Vishnu Turlapati http://blogs.technet.com/b/tvishnun1/

    Monday, November 24, 2014 9:26 AM
  • Your code is ok, setting the attribute to null is the right way to remove the value from the field, also when using early bind.

    try to debug your code and check if the entity get the right value before the update and there aren't plugins that stop the update of that field.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, November 24, 2014 9:42 AM
  • The attribute type doesn't seem to matter, the same occurs for both a Nullable int and a Nullable datetime in my case. Since that's their type, I would assume setting them to null should work, but it doesn't. In the property in the generated class, you see this:

           
    [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("new_certainattribute")]
    public System.Nullable<int> new_certainattribute
    {
    	get
    	{
    		return this.GetAttributeValue<System.Nullable<int>>("new_certainattribute");
    	}
    	set
    	{
    		this.SetAttributeValue<System.Nullable<int>>("new_Certainattribute", "new_certainattribute", value);
    	}
    }

    The only thing I can think of at the moment is that it's not working because I'm not retrieving the record from CRM first. I just create a new instance of the entity and then update only the attributes that need to be changed and set the id. This always seemed to work since it did update the values, but perhaps it fails in this scenario. The documentation below does mention that you need retrieve the record, so I'm guessing that might be the cause of the problem:

    http://msdn.microsoft.com/en-us/library/gg328499.aspx


    Monday, November 24, 2014 9:52 AM