2

我在动态 crm 表单中与系统用户有多对多关系,并且从以下代码段中,我试图获取在此关系中退出的用户列表。但这会引发异常,即附加信息:'SystemUser' 实体不包含 Name = 'salesorderid' 的属性。.

知道吗,我在其中缺少什么?

private void grabRelatedUsers(IOrganizationService service){
 QueryExpression sysUsersQuery = new QueryExpression()
            {
                EntityName = "systemuser",
                ColumnSet = new ColumnSet("systemuserid"),
                LinkEntities = {
                    new LinkEntity()  {
                    LinkFromEntityName = "systemuser",
                    LinkToEntityName = "new_salesorder_systemuser",
                    LinkFromAttributeName = "systemuserid",
                    LinkToAttributeName = "systemuserid",
                    JoinOperator = JoinOperator.Inner
                    },
                    new LinkEntity() {
                        LinkFromEntityName = "new_salesorder_systemuser",
                        LinkToEntityName = "salesorder",
                        LinkFromAttributeName = "salesorderid",
                        LinkToAttributeName = "salesorderid",
                        JoinOperator = JoinOperator.Inner,
                        LinkCriteria = new FilterExpression() {
                            Conditions = {
                                new ConditionExpression("salesorderid",ConditionOperator.Equal,orderId)
                            }
                        }
                    },
                    new LinkEntity().AddLink("systemuser","systemuserid","systemuserid")
                    },

            };

            EntityCollection subGridUsers = service.RetrieveMultiple(sysUsersQuery);
}
4

1 回答 1

1

您没有正确嵌套链接实体。查看为您的场景生成的 fetchXML:

<fetch version="1.0" output-format="xml-platform" mapping="logical">
  <entity name="systemuser">
    <attribute name="systemuserid" />
    <link-entity name="new_salesorder_systemuser" from="systemuserid" to="systemuserid" visible="false" intersect="true">
      <link-entity name="salesorder" from="salesorderid" to="salesorderid" alias="ab">
        <filter type="and">
          <condition attribute="salesorderid" operator="eq" value="XXXXXXXXXXXXXXX" />
        </filter>
      </link-entity>
    </link-entity>
  </entity>
</fetch>

如您所见,链接的实体必须是嵌套的,而且,因为这是开箱即用的 N:N 关系,您不能从 new_salesorder_systemuser 链接 salesorder,而是从 systemuser。所以 QueryExpression 应该是这样的:

QueryExpression sysUsersQuery = new QueryExpression()
{
    EntityName = "systemuser",
    ColumnSet = new ColumnSet("systemuserid"),
    LinkEntities = {
        new LinkEntity
        {
            LinkFromEntityName = "systemuser",
            LinkToEntityName = "new_salesorder_systemuser",
            LinkFromAttributeName = "systemuserid",
            LinkToAttributeName = "systemuserid",
            JoinOperator = JoinOperator.Inner,
            LinkEntities =
            {
                new LinkEntity()
                {
                    EntityAlias = "ab",
                    LinkFromEntityName = "systemuser",
                    LinkToEntityName = "salesorder",
                    LinkFromAttributeName = "salesorderid",
                    LinkToAttributeName = "salesorderid",
                    JoinOperator = JoinOperator.Inner,
                    LinkCriteria = new FilterExpression()
                    {
                        Conditions = {
                            new ConditionExpression("salesorderid",ConditionOperator.Equal, "sfsdf")
                        }
                    }
                }
            }
        }
    }
};

如果您在生成 QueryExpression 时遇到问题,只需使用 Advanced Find 进行查询,下载 fetchXml 并使用 SDKFetchXmlToQueryExpressionRequest消息将其转换为 QueryExpression。这里有一些示例: https ://msdn.microsoft.com/en-us/library/hh547457.aspx

于 2017-03-16T23:51:20.973 回答