Warm tip: This article is reproduced from serverfault.com, please click

其他-Dynamics CRM 2013工作流C#从链接实体中提取字段

(其他 - Dynamics CRM 2013 Workflow C# Extract Field from Linked Entity)

发布于 2020-11-30 03:53:20

我正在尝试从Dynamics CRM 2013工作流中的相关CRM实体获取字段。以下代码返回映射的实体,但未引用该实体中的匹配记录。

如何更新它,以便它对两个匹配的实体Guid运行以下查询?我需要使用EntityReference吗?

   QueryExpression FindAddressLots = new QueryExpression(context.PrimaryEntityName);
                    FindAddressLots.LinkEntities.Add
                      (new LinkEntity(context.PrimaryEntityName, "appdetails_data", "applicationdetailsid", "appdetails_dataid", JoinOperator.Inner));
        
             FindAddressLots.LinkEntities[0].Columns.AddColumns("appdetails_dataid", "suburb", "postcode");
    FindAddressLots.LinkEntities[0].EntityAlias = "lotdata";
        
    EntityCollection ec = service.RetrieveMultiple(FindAddressLots);
        
                    foreach (Entity act in ec.Entities)
                    {
        
                      if (act.Attributes.Contains("lotdata.suburb"))
                        {
                          FindAddressLots = act.GetAttributeValue<AliasedValue>("lotdata.suburb").Value.ToString();
                         }
                    }
                }

有什么建议?这将不胜感激。谢谢。

Questioner
Michael
Viewed
0
Guido Preite 2020-12-02 18:20:28

首先让我们澄清一件事:我们不知道字段的类型suburb,在我的代码中,我将假定它是一个字符串,但可以是其他字符串。

可以按以下方式重写查询代码(链接属性,列和别名相同):

QueryExpression queryAddressLots = new QueryExpression(context.PrimaryEntityName);
LinkEntity linkDetails = queryAddressLots.AddLink("appdetails_data", "applicationdetailsid", "appdetails_dataid", JoinOperator.Inner);
linkDetails.Columns.AddColumns("appdetails_dataid", "suburb", "postcode");
linkDetails.EntityAlias = "lotdata";
EntityCollection collAddressLots = service.RetrieveMultiple(queryAddressLots);

根据代码,没有逻辑来检索特定记录(但通常存在),否则它将返回所有Address Lots与明细实体连接的实体(我之前也写过注释,没有人关心真实的实体名称,但是它们应该是确切的说,当我阅读一个名为applicationdetailsidpeople的字段时,可以假设存在一个称为的实体,applicationdetails而该实体不存在

第二部分是如何获取值,可以将其重写为(也在这里,变量名,称为actec不应使用的名称):

foreach (Entity addressLot in collAddressLots.Entities)
{
    AliasedValue aliasSuburb = addressLot.GetAttributeValue<AliasedValue>("lotdata.suburb");
    if (aliasSuburb != null && aliasSuburb.Value != null)
    {
        string suburb = (string)aliasSuburb.Value;
    }
}

请注意,我对字符串进行了强制转换,因为正如我在假定字段是字符串之前写的那样,如果该字段是a,Whole Number我可能会做类似Convert.ToInt32(aliasSuburb.Value)

现在,问题提到了it isn't referencing the matching record in the entity,我认为它是指这样的事实(正如我之前写的那样):没有提供ID,并且返回了所有记录。这可以通过在QueryExpression定义之后添加条件来轻松解决,例如:

queryAddressLots.Criteria.AddCondition("applicationdetailsid", ConditionOperator.Equal, context.PrimaryEntityId);

当然,我们不能保证这是否可行,因为我们不知道字段名称,希望作者知道。老实说,我们可以查询这个appdetails_data实体并添加一个条件以匹配applicationdetailsid(对我来说,它看起来像是N:1关系,而不是1:N关系),suburb而无需使用a来查询字段,LinkEntity但我不能100%肯定。

希望对你有所帮助,下次,请不要隐藏实体和字段名称,只需更改前缀即可。