2015-09-21 64 views
1

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

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.ServiceModel.FaultException`1' 发生在Microsoft.Xrm.Sdk.dll。
附加信息:在MetadataCache中找不到名称='Account'的实体。

这是它抛出警告

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

为什么会变成这样的一段代码?

编辑2: 我用Account替换Account,见下文。

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

感谢大家的帮助。

+0

的:.Equals(phoneNumber)

然后帐户记录的检索与此更换后的代码上面的代码不工作,行'newMsg.To = activityParties',不会将客户名称分配给活动? – AndroidAL

+0

我看到你改变了发布的代码。位还是有问题: new ActivityParty() { PartyId = new EntityReference(Account。EntityLogicalName,actualCRMAccount.Id) }; activityParties.Add(new ActivityParty(){Id = actualCRMAccount.Id}); 应改为: activityParties.Add(新ActivityParty() { PartyId =新的EntityReference(Account.EntityLogicalName,actualCRMAccount.Id) }); 这应该工作。如果没有,那么检索到的帐户可能不存在。 –

+0

嗨,最近有什么问题? – AndroidAL

回答

2

的问题是,经由you're添加帐户: 新ActivityParty(){ID = actualCRMAccount.Id}

要定义一个新ActivityParty必须设置PartyId属性这对于一个实体引用选择类型。

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

应该工作。

+0

嗨,感谢您的答复,所以我应该添加您的代码在if语句(actualCRmAccount!= null) – AndroidAL

+0

这似乎没有为我做错了什么工作? – AndroidAL

1

有一个在下面的行一个错字:

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

.Equals(phoneNumbers)应该是:

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() }); 
} 
+0

,我已经添加了你的代码,它仍然没有在窗体中填充TO,字段? – AndroidAL

+0

如果您检索的帐户不为空,这应该可以正常工作。你检查过了吗? –

+0

您的代码中存在另一个错误。我修改了我的答案。 –