I have a code that creates or updates an account. This block of code calls another method that saves the accountid on the contact record once it is chosen as the primary contact of the account.
However, when I try to run this, I am receiving this error:
An exception of type 'System.ServiceModel.FaultException`1' occurred in Microsoft.Xrm.Sdk.dll but was not handled in user code Additional information: System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #06B9DDED
It happends around the Update I do on the UpdateAccountNameContactEntity method. I've read a couple of sites, saying it might be the Guid I'm using, but I'm very new to this and I don't know how should I fix it.
I'm using ASP.NET MVC and Dynamics 365 right now. I have used this as my guide to create this.
Here is the code.
public void SaveAccount(AccountEntityModels objAccountModel)
{
using (OrganizationService service = new OrganizationService("CRM"))
{
Entity AccountEntity = new Entity("account");
if (objAccountModel.AccountID != Guid.Empty)
{
AccountEntity["accountid"] = objAccountModel.AccountID;
}
AccountEntity["name"] = objAccountModel.AccountName;
AccountEntity["telephone1"] = objAccountModel.Phone;
AccountEntity["fax"] = objAccountModel.Fax;
AccountEntity["websiteurl"] = objAccountModel.Website;
AccountEntity["primarycontactid"] = new Microsoft.Xrm.Sdk.EntityReference { Id = objAccountModel.PrimaryContact.Id, LogicalName = "account" };
if (objAccountModel.AccountID == Guid.Empty)
{
objAccountModel.AccountID = service.Create(AccountEntity);
UpdateAccountNameContactEntity(objAccountModel.AccountID, objAccountModel.PrimaryContact.Id);
}
else
{
service.Update(AccountEntity);
UpdateAccountNameContactEntity(objAccountModel.AccountID, objAccountModel.PrimaryContact.Id);
}
}
}
public void UpdateAccountNameContactEntity(Guid accountId, Guid contactId)
{
using (OrganizationService service = new OrganizationService("CRM"))
{
try
{
Entity contactEntity = new Entity("contact");
contactEntity["contactid"] = contactId;
contactEntity["parentcustomerid"] = accountId;
service.Update(contactEntity); //THIS IS WHERE I GET THE ERROR
}
catch (Exception ex)
{
}
}
}
objAccountModel.AccountID
? kindly make a breakpoint and hover your cursor in that line – Jeric Cruz