2

I am struggling using the CrmServiceClient to update attribute values to null.

My code is working fine for non null values but fails with an exception:

Object reference not set to an instance of an object. at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AddValueToPropertyList(KeyValuePair2 Field, AttributeCollection PropertyList) at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.UpdateEntity(String entityName, String keyFieldName, Guid id, Dictionary2 fieldList, String applyToSolution, Boolean enabledDuplicateDetection, Guid batchId)

whenever I try to set a null value.

So far I have tried the following:

// create Crm service client object
CrmServiceClient svc = new CrmServiceClient(
new System.Net.NetworkCredential("username", "password", "domain"),
                "crmhost",
                "443",
                "crmorganization",
                false,
                true
                );

// check the connection to CRM was successful
if (!svc.IsReady)
{
    throw new Exception("Could not connect to CRM Organization.", svc.LastCrmException);
}

// create a Dictionary to store the attributes to be added to the entity
Dictionary<string, CrmDataTypeWrapper> attributes = new Dictionary<string, CrmDataTypeWrapper>();

//this doesn't work
attributes.Add("new_somedatefield", null);

// and this doesn't work
attributes.Add("new_somedatefield", new CrmDataTypeWrapper(null, CrmFieldType.CrmDateTime));

// this also doesn't work
DateTime? nullDate = new DateTime();
nullDate = null;
attributes.Add("new_somedatefield", new CrmDataTypeWrapper(nullDate, CrmFieldType.CrmDateTime));

svc.UpdateEntity("new_entityname", "new_entitynameid", (Guid)data[metaData.PrimaryIdAttribute], attributes));

There's nothing specifically mentioned in the documentation about setting null values.

Has anybody successfully managed to achieve this?

EDIT - To clarify I need to target Dynamics CRM 2015, the Update method on the CrmServiceClient is not available until 2016.

Andy Darby
  • 23
  • 7
  • Why can't you use latest Xrm.Tooling? It can call CRM 2015 successfully, there is no reason for using outdated libs outside plugins – Pawel Gradecki May 24 '18 at 12:51
  • 1
    Yes, this seems to have worked. Using the suggestion from @Arun I can now successfully set null values. I assumed I'd need to use the Xrm.Tooling matching our version of CRM. I'll run some more tests and scenarios before putting this into production. Still begs the question how it was supposed to be achieved with the version I was using though! – Andy Darby May 25 '18 at 15:08

1 Answers1

1

This should work.

Entity ent = new Entity("entityname");
ent.Attributes["datefieldname"] = null;
ent.id = Id;
service.Update(ent);
  • This isn't how the CrmServiceClient is used. There is no Update method that accepts an Entity object. You need to build a Dictionary of as the list of attributes, with the string being the name of the CRM attribute and the CrmDataTypeWrapper object containing the value to set the attribute to. I cannot work out how to set a NULL value using the CrmDataTypeWrapper object without generating an exception as above. – Andy Darby May 23 '18 at 08:58
  • 1
    [Update](https://msdn.microsoft.com/en-us/library/microsoft.xrm.tooling.connector.crmserviceclient.update.aspx) vs [UpdateEntity](https://msdn.microsoft.com/en-us/library/microsoft.xrm.tooling.connector.crmserviceclient.updateentity.aspx) – Arun Vinoth-Precog Tech - MVP May 23 '18 at 10:33
  • 1
    Looks like the Update method only applies from CRM 2016 onwards, we are using CRM 2015. I've updated the question to clarify. – Andy Darby May 23 '18 at 10:36
  • 1
    Taking @Pawel's advice I have updated to use the latest version of Xrm.Tooling, which means I can then use the suggestion from Arun. Still not sure how it was supposed to be achieved using the older version I was originally using though. – Andy Darby May 25 '18 at 15:11