Search code examples
c#odatawcf-data-servicesxrm

Required member 'LogicalName' missing for field 'Target'


I'm using the Microsoft XRM SDK to programmatically add an entity. However, each time I run a .Create() command I get the following error:

Required member 'LogicalName' missing for field 'Target'

First time using this service and similar resources in our company are scarce, so not sure what this error means or how to investigate/solve it.

Below is the class I created to handle the XRM communication. I instantiate each of the connection properties in the construtor. Then, in this case, call CreateAgency(AgentTransmission agt). The exception is being thrown in the CreateAgency() method on the .Create(account) method call.

class DynamicsCommunication
{
    private Uri OrganizationUri = new Uri("http://devhildy03/xRMDRMu01/XRMServices/2011/Organization.svc");
    private ClientCredentials credentials;
    private OrganizationServiceProxy servicePoxy;
    private Guid accountId;
    private Entity account;

    public DynamicsCommunication()
    {
        credentials = new ClientCredentials();
        credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        servicePoxy = new OrganizationServiceProxy(OrganizationUri, null, credentials, null);
        accountId = Guid.Empty;
    }

    public string UpdateDynamics(AgentTransmission agt)
    {
        switch (DeterminAction(agt))
        {
            case DynamicsAction.Create:
                return CreateAgency(agt);
            case DynamicsAction.Update:
                return UpdateAgency(agt);
            default:
                return string.Empty;
        }
    }

    private string CreateAgency(AgentTransmission agt)
    {
        try
        {
            //Exception is thrown after this command
            accountId = servicePoxy.Create(CreateAccount(agt));

            if (accountId != Guid.Empty)
            {
                return string.Empty;
            }
            else
            {
                return "error creating agency";
            }
        }
        catch (ODataException oEx)
        {
            string s = oEx.Message;
            throw;
        }
        catch (Exception ex)
        {
            string s = ex.Message;
            throw;
        }
    }

    private Entity CreateAccount(AgentTransmission agt)
    {
        account = new Entity();
        account.Attributes.Add("LogicalName", "something");
        account.Attributes.Add("name", agt.AgencyName);
        account.Attributes.Add("telephone1", agt.BusinessPhone.Replace("(","").Replace(")", "").Replace("-", ""));
        account.Attributes.Add("address1_line1", agt.MailingStreet1);
        account.Attributes.Add("address1_city", agt.MailingCity);
        account.Attributes.Add("address1_postalcode", agt.MailingZip);
        account.Attributes.Add("neu_address1stateprovince", 1); //1 for Mailing
        account.Attributes.Add("neu_channelid", LookupChannelId(agt.Channel));
        account.Attributes.Add("neu_appointementstatus", "279660000");
        account.Attributes.Add("customertypecode", LookupCustomerCode(agt.RelationshipType));
        account.Attributes.Add("neu_taxid", UnobfuscateRef(agt.ReferenceNumber));

        return account;
    }
}

Solution

  • Set the name of the CRM entity on the LogicalName property of the Entity object rather than adding it to the attribute collection

    account = new Entity("your_entity_name");
    

    or

    account = new Entity();
    account.LogicalName = "your_entity_name";