0
votes

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)
            {
            }
        }
    }
1
You should at the very least: 1-Give us the full error message 2-Tell us on which line you get the error and 3-What data you were using to cause it to break.DavidG
can you show what is the data type of objAccountModel.AccountID? kindly make a breakpoint and hover your cursor in that lineJeric Cruz
@DavidG Yes, sorry. I didn't notice I did not pointed which part of the code I'm getting an error. I've fixed it now, hope that works for you.Serene
@JericCruz objAccountModel.AccountID says it's System.Guid.Serene

1 Answers

3
votes

You are trying to assign GUID in EntityReference.

Change this line

contactEntity["parentcustomerid"] = accountId;

like below:

contactEntity["parentcustomerid"] = new Microsoft.Xrm.Sdk.EntityReference { Id = accountId, LogicalName = "account" };