«Указанный ключ отсутствует в словаре» при создании нового объекта CRM - PullRequest
0 голосов
/ 19 апреля 2020

Я столкнулся со следующей ошибкой при создании новой записи объекта.

Имя объекта и имя поля указаны правильно, но я не уверен, почему ошибка по-прежнему появляется.

Указанный ключ отсутствует в словаре.

enter image description here


Вызов CreateCase (строка)

foreach (DataRow row in caseTable.Rows)
{
    Entity caseEntity = helper.GetCasebyID((string)row[Excel.ID]);

    if (caseEntity == null)
    {
        caseEntity = CreateCase(row);
    }
}

CreateCase (строка):

private Entity CreateCase(DataRow row)
{
    Entity caseEntity = new Entity("ccc_case");

    if (!(string.IsNullOrEmpty(row[Excel.sourcename].ToString().Trim())))
    {
        caseEntity.Attributes[Case.sourcename] = row[Excel.sourcename];
    }
    //The given key was not present in the dictionary. 
    //at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
    caseEntity.Id = helper.GetOrganizationService().Create(caseEntity);
}

Вспомогательный класс:

public class Helper
{
    private OrganizationServiceProxy _proxy;
    private IOrganizationService _org;

    public IOrganizationService GetOrganizationService()
    {
        if (_org == null)
        {
            var credential = new ClientCredentials();
            credential.Windows.ClientCredential.Domain = ConfigurationManager.AppSettings["Domain"];
            credential.Windows.ClientCredential.UserName = ConfigurationManager.AppSettings["Username"];
            credential.Windows.ClientCredential.Password = Decrypt(ConfigurationManager.AppSettings["Password"]);
            Uri organizationUri = new Uri(ConfigurationManager.AppSettings["OrganizationUri"]);
            Uri homeRealmUri = null;
            _proxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credential, null);
            _proxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
            _org = new OrganizationService(_proxy);
        }
        return _org;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...