如果我有记录的GUID,但我不知道它是帐户还是联系人,我该如何检索此记录并识别其类型?
例如,我可以像这样检索指定的类型:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
但是,如果我不知道它是什么类型的记录,如何检索它并识别其类型?
这样的事情:
var myEntity = organizationService.Retrieve(
"????",
myEntityGuid,
new ColumnSet(true));
答案 0 :(得分:2)
如果您从Nuget引用DLaB.Xrm,您可以这样写:
bool isAccount = service.GetEntitiesById<Account>(customerId).Count == 1;
如果您想真正获得实际值,可以这样做。
var customerId = System.Guid.NewGuid();
var entity = service.GetEntitiesById<Account>(customerId).FirstOrDefault() ??
service.GetEntitiesById<Contact>(customerId).FirstOrDefault() as Entity;
if (entity != null)
{
var account = entity as Account; // will be null if the Guid was for a contact
var contact = entity as Contact; // will be null if the Guid was for an account
}
答案 1 :(得分:1)
您无法单独使用GUID进行搜索。 您需要GUID和实体逻辑名称。
每当使用代码,c#或javascript检索实体引用时,对象将包含实体逻辑名称
更新以尝试查找具有给定ID的帐户:
Guid Id = // Your GUID
IOrganizationService serviceProxy = // Create your serivce proxy
var accountQuery = new QueryExpression
{
EntityName = "account",
TopCount = 1,
Criteria =
{
Conditions =
{
new ConditionExpression("accountid", ConditionOperator.Equal, Id)
}
}
}
var response = serviceProxy.RerieveMultiple(accountQuery);
if(null == response.Entities.FirstOrDefault())
{
//No Account Record Found
}
答案 2 :(得分:1)
如果您只需要区分帐户和联系人,并希望确保记录确实存在,您还可以使用他们的CustomerAddress,LEFT OUTER JOIN
两者,帐户和联系人:
var query = new QueryExpression
{
EntityName = "customeraddress",
ColumnSet = new ColumnSet("customeraddressid"),
TopCount = 1,
Criteria = new FilterExpression
{
Conditions =
{
// limit to Address 1
new ConditionExpression("addressnumber", ConditionOperator.Equal, 1),
// filter by "anonymous" GUID
new ConditionExpression("parentid", ConditionOperator.Equal, myEntityGuid),
},
},
LinkEntities =
{
new LinkEntity
{
EntityAlias = "acc",
Columns = new ColumnSet("name"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "accountid",
LinkToEntityName = "account",
JoinOperator = JoinOperator.LeftOuter
},
new LinkEntity
{
EntityAlias = "con",
Columns = new ColumnSet("fullname"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "contactid",
LinkToEntityName = "contact",
JoinOperator = JoinOperator.LeftOuter
},
},
};
...允许您一次性检索帐户或联系人:
var customer = service.RetrieveMultiple(query).Entities.FirstOrDefault();
...但要求通过AliasedValue
s:
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
...这可以使阅读很多属性有点混乱。
答案 3 :(得分:1)
我知道这是一个古老的问题,但是我想我会补充一些内容,以防将来有人遇到类似我的问题而对此感到困惑。在这种情况下,它可能与OP要求的不完全相同。
我必须确定一个实体是另一个类型,所以我可以使用一种方法,而不必为每个实体编写独立的方法。去吧:
resources: ["pods","deployments","namespaces","services"]
并将其传递给以下方法,即可识别类型。
var reference = new EntityReference
{
Id = Guid.NewGuid(),
LogicalName = "account" //Or some other logical name - "contact" or so.
}