我正在使用PowerShell中的CRM SDK,尝试查询链接到特定帐户名称的合同,如果我对帐户GUID进行查询,我会得到结果,但我想查询EntityReference对象中的名称,而不是GUID,例如(这很好):
$query = new-object Microsoft.Xrm.Sdk.Query.QueryExpression('new_contract')
$query.Criteria.AddCondition('new_account', [Microsoft.Xrm.Sdk.Query.ConditionOperator]::Equal, $AccountGuid)
以上结果如下:
Key Value
--- -----
createdby Microsoft.Xrm.Sdk.EntityReference
createdon 21/10/2014
modifiedby Microsoft.Xrm.Sdk.EntityReference
modifiedon 28/02/2016
modifiedonbehalfby Microsoft.Xrm.Sdk.EntityReference
new_account Microsoft.Xrm.Sdk.EntityReference
[...] [...]
new_account
属性为:
Id : dab2909d-6149-e411-93fc-005056af5481
LogicalName : account
Name : CustomerNameText
KeyAttributes : {}
RowVersion :
ExtensionData : System.Runtime.Serialization.ExtensionDataObject
我想为new_contract
创建new_account
名称LIKE CustomerNameText
我试过了:
$query.Criteria.AddCondition('new_account',([Microsoft.Xrm.Sdk.EntityReference].Attributes['new_account']).Name, [Microsoft.Xrm.Sdk.Query.ConditionOperator]::Like, $AccountName)
但它不起作用会收到以下错误:
Exception calling "RetrieveMultiple" with "1" argument(s): "Link entity with name or alias new_account is not found"
At line:2 char:5
+ $temp = $service.RetrieveMultiple($query);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FaultException`1
我希望我很清楚,但如果没有,请回答任何问题。
Powershell代码更可取,c#也可以
...谢谢
答案 0 :(得分:3)
您无法对QueryExpression
名称使用EntityReference
条件。使用EntityReference
,您只能使用Guid
。
如果要按名称查询,可以在QueryExpression中使用LinkEntity
。您必须使用LinkCriteria
var q = new QueryExpression();
q.EntityName = "new_contract";
q.ColumnSet = new ColumnSet();
q.ColumnSet.Columns.Add("new_contractid");
q.LinkEntities.Add(new LinkEntity("new_contract", "account", "new_account", "accountid", JoinOperator.Inner));
q.LinkEntities[0].Columns.AddColumns("accountid", "name");
q.LinkEntities[0].EntityAlias = "temp";
q.LinkEntities[0].LinkCriteria.AddCondition("name", ConditionOperator.Equal, "yourAccountName");
P.S 如果以上代码段不适合您。并且您希望按accountName
检索。要做到这一点,请使用帐户Guid
使用现有代码。但首先你需要检索accountGuid by name
。在那种情况下
public Guid getAccountGuidByName(string name)
{
QueryExpression accountQuery = new QueryExpression("account");
accountQuery.Criteria.AddCondition("name", ConditionOperator.Equal, "yourAccountName");
EntityCollection entCol = Service.RetrieveMultiple(accountQuery);
if (entCol.Entities.Count > 0)
{
return entCol.Entities[0].Id;
}
return Guid.Empty;
}
仅举例来说我不知道你怎么能在powershell中调用函数等但是我提供了c#代码,正如你说的那样好吧如果它在C#然后使用它如下:
$query = new-object Microsoft.Xrm.Sdk.Query.QueryExpression('new_contract')
$query.Criteria.AddCondition('new_account', [Microsoft.Xrm.Sdk.Query.ConditionOperator]::Equal, getAccountGuidByName("yourAccountName"));