将帐户关联到活动CRM

时间:2015-09-21 10:13:11

标签: c# dynamics-crm-2015

我正在编写从数据库中提取的代码并将信息填充为活动,我已成功地将数据库中的字段添加到描述和主题字段,但我似乎无法将其链接到到一个帐户? 这是我尝试过的;

foreach (var phoneNumber in phoneNumbers)
{
    var potentialMatches = _xrm.AccountSet.Where(account => account.Address1_Telephone2.Equals(phoneNumbers)).ToList();

    if (potentialMatches.Count > 0)
    {
        var accountMatch = potentialMatches.First();

        var actualCRMAccount = (Account) _xrm.Retrieve("Account", accountMatch.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

        if (actualCRMAccount != null)
        {
            //Is this correct way?
            new ActivityParty()
            {
                PartyId = new EntityReference(Account.EntityLogicalName, actualCRMAccount.Id)
            };
            activityParties.Add(new ActivityParty() {Id = actualCRMAccount.Id});
        }
    }
}
//RegardingObjectId not working
//RegardingObjectId = new EntityReference(Account.EntityLogicalName, recordRow.Cells[0].Value.ToString);
newMsg.To = activityParties;
newMsg.Description = recordRow.Cells[0].Value.ToString();
newMsg.Subject = recordRow.Cells[2].Value.ToString();

_xrm.Create(newMsg);

编辑1: 当我现在运行它时我得到了这个警告

  

类型&System; Service.ServiceModel.FaultException`1'的未处理异常发生在Microsoft.Xrm.Sdk.dll中   附加信息:名称为'帐户'的实体在MetadataCache中找不到。

这是抛出警告的代码段

var actualCRMAccount = (Account)_xrm.Retrieve("Account", accountMatch.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

为什么会这样?

编辑2: 我用帐户替换了帐号,见下文。

  var actualCRMAccount = (Account)_xrm.Retrieve("account", accountMatch.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

编辑3: 我现在正试图为潜在客户做这件事。但是当我添加这段代码时。

newMsg.RegardingObjectId = activityParties;

我收到此错误。

  

无法隐式转换类型' System.Collections.Generic.List'到' Microsoft.Xrm.Client.CrmEntityReference'

如何为RegardingObjectId分配值。

感谢大家的帮助。

2 个答案:

答案 0 :(得分:2)

问题是您是通过以下方式添加帐户:     新的ActivityParty(){Id = actualCRMAccount.Id}

要定义新的ActivityParty,您必须设置PartyId属性,该属性是所选Type的实体引用。

new ActivityParty() { PartyId = new EntityReference(Account.EntityLogicalName,actualCRMAccount.Id)    }

应该有用。

答案 1 :(得分:1)

以下行中有一个拼写错误:

var potentialMatches = _xrm.AccountSet.Where(account => account.Address1_Telephone2.Equals(phoneNumbers)).ToList();

.Equals(phoneNumbers)应该是:.Equals(phoneNumber)

然后在检索帐户记录后用以下代码替换代码:

var actualCRMAccount = (Account)_xrm.Retrieve("Account", accountMatch.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

if (actualCRMAccount != null)
{
    activityParties.Add(new ActivityParty { PartyId = actualCRMAccount.ToEntityReference() });
}