locked
Adding Contacts to new Account in C# RRS feed

  • Question

  • I am trying to add contacts to a newly created account, but seem to be having some problems linking the contact to the account.

    After reading account data into a SQL Datarow, I create an account with code which returns the GUID for the new account:
    account newAccount = new account();
    
    if (!dr1.IsNameNull())
       newAccount.name = dr1.Name.ToString();
    
    ReturnGUID = service.Create(newAccount);	
    


    Using that new GUID, I then try to create a contact for that same account.

    contact newContact = new contact();
    if (!cr1.IsFirstNameNull())
       newContact.firstname = cr1.FirstName.ToString();
    if (!cr1.IsLastNameNull())
       newContact.lastname = cr1.LastName.ToString();
    newContact.parentcustomerid.Value = AccountGUID;
    

    ReturnGUID = service.Create(newContact);


    When it gets to the newContact.parentcustomerid.Value = AccountGUID; line I get an error about this being the wrong type.  I looked up the SDK documentation, and it appears that the 'parentcustomerid' value is an object type of customer?

    Any sample code on how to add a new contact to a newly created Account?
    Monday, November 9, 2009 5:06 PM

Answers

  • Hi, Chris.

    Try this:

    newContact.parentcustomerid = new Customer("account", AccountGUID);

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com

    if instead you are using the ASDL, it would look like this:

    newContact.parentcustomerid = new Customer();
    newContact.parentcustomerid.type = EntityName.account.ToString();
    newContact.parentcustomerid.Value = AccountGUID;
    only the SDK has constructors that take arguments
    Monday, November 9, 2009 5:54 PM

All replies

  • Hi, Chris.

    Try this:

    newContact.parentcustomerid = new Customer("account", AccountGUID);

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    • Proposed as answer by mardukes Monday, November 9, 2009 5:54 PM
    Monday, November 9, 2009 5:14 PM
    Moderator
  • try using following

    newContact.parentcustomerid =CrmTypes.CreateCustomer(EntityName.account.ToString(), AccountGUID);
    Monday, November 9, 2009 5:40 PM
  • Hi, Chris.

    Try this:

    newContact.parentcustomerid = new Customer("account", AccountGUID);

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com

    if instead you are using the ASDL, it would look like this:

    newContact.parentcustomerid = new Customer();
    newContact.parentcustomerid.type = EntityName.account.ToString();
    newContact.parentcustomerid.Value = AccountGUID;
    only the SDK has constructors that take arguments
    Monday, November 9, 2009 5:54 PM