1

Dear smart developers out there,

I encounter a problem when I want to create a contact belonging to an organization in Microsoft Dynamics CRM 2013 via web services

client = new OrganizationServiceClient("CustomBinding_IOrganizationService"); 
var newContactProperties = new Dictionary<string, object> { 
                { "lastname", "TestContact"}, 
                { "firstname", "A"},
                { "fullname", "A TestContact"}

        };

/* organizationType is a CRM.CRMWebServices.OptionSetValue
 * with ExtensionData null, PropertyChanged null and a valid Value
 *
 * orgReference is a CRM.CRMWebServices.EntityReference
 * with a valid Id
 */

newContactProperties.Add("parentcustomeridtype", organizationType);
newContactProperties.Add("parentcustomerid", orgReference);

var entity = new Entity();
entity.LogicalName = "contact";
entity.Attributes = new AttributeCollection();
entity.Attributes.AddRange(newContactProperties);

client.Create(entity);

This gives me error 'Attribute parentcustomeridtype must not be NULL if attribute parentcustomerid is not NULL'

I am puzzled why this happens and how I can solve this problem. Please help me if you can.

Thank you, AllWorkNoPlay

AllWorkNoPlay
  • 454
  • 1
  • 4
  • 20
  • Is the value of the organizationType set properly? Like the following: OptionSetValue organizationType = new OptionSetValue(1); This should be 1 if the parentcustomer is an account, or 2 if it is a contact. – Bojan Borisovski Dec 09 '14 at 09:18
  • you don't need to set fullname and parentcustomeridtype properties, make sure that orgReference contains the right logical name of the parent record – Guido Preite Dec 09 '14 at 16:14
  • Even with your useful tips I do not manage to get rid of this error message. I now try to accomplish what I want using 'early bound' objects, with http://xrmearlyboundgenerator.codeplex.com. Creating accounts and contacts works, now I am trying to use the AttachLink method of OrganizationServiceContext ... to be continued – AllWorkNoPlay Dec 11 '14 at 17:51

2 Answers2

3

You don't need to set "parentcustomeridtype" attribute separately. It's a system field, will be set by platform and in parentcustomerid exists for legacy reason, when it was Customer type in earlier versions of Dynamics CRM. You need to specify only EntityReference in lookup field.
newContactProperties.Add("parentcustomerid", new EntityReference("account", new Guid("{accountid guid}")));
Also it's not clear what type you use in "orgReference" field. For contact valid entity types should be "account" or "contact".

  • Thank you for the reaction. Whether I set parentcustomeridtype or not does not matter, always I get this error. OrgReference contains a valid EntityReference to an account. – AllWorkNoPlay Dec 11 '14 at 17:50
0

thank you for the answers, I did not manage to get it right using web services this way.

I tried using Early Bound access with success:

  1. Generated proxy objects using https://xrmearlyboundgenerator.codeplex.com/
  2. Added line [assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()] to assemblyInfo to have Intellisense available (even for customized fields)
  3. Now I manage to create a contact and assign it to an organization (something like this):

        var contact = new Contact()
                       {
                           FirstName = "Bob",
                           LastName = "Dobalina",
                           Address1_Line1 = "123 Strasse",
                           Address1_City = "Berlin",
                           Address1_PostalCode = "32254",
                           Telephone1 = "425-555-5678",
                           EMailAddress1 = "bob.dobalina@germany.de"
                       };
    
        var account = new Account()
        {
            Name = "Siemens Germany",
        };
    
    
        context.AddObject(contact);
        context.AddObject(account);
    
        context.AddLink(account, "contact_customer_accounts", contact);
    
        context.SaveChanges();
    }
    
AllWorkNoPlay
  • 454
  • 1
  • 4
  • 20