dbollig
dbollig

Reputation: 81

Saving parent account to contact in .Net Dynamics CRM application

I am learning crm development in a .net application.

I am trying to associate a contact with an account. But I am not finding accountid in the documentation for the contact entity.

How does this work when trying to associate these records?

Here is my contact controller code.

    [HttpPost]
    public IActionResult Create(ContactEntityModels model)
    {

        newContact.firstname = model.firstname;

        var contact = new Entity("contact");
        {
            contact["firstname"] = newContact.firstname;
            contact["parentcustomerid_account"] = newContact.ParentAccount;
        }

        _crmContext.ServiceContext.AddObject(contact);

        _crmContext.ServiceContext.SaveChanges();

        return RedirectToAction("Contacts", "Admin");
    }

Upvotes: 2

Views: 117

Answers (1)

You have to use the EntityReference object to assign like below:

    var contact = new Entity("contact");
    {
        contact["firstname"] = newContact.firstname;
        contact["parentcustomerid"] = new EntityReference("account", <GUID>);
    }

Upvotes: 1

Related Questions