如何从自定义实体中检索数据?

时间:2015-01-19 23:50:41

标签: c# dynamics-crm dynamics-crm-4

此代码有助于根据帐户实体

中的1:N关系检索联系人记录

我想从帐户实体中检索另一个名为company的自定义实体的记录,但我不知道如何调用自定义数据实体以在RetrieveContact方法中使用它 - contact是标准实体,company是自定义实体

PS:公司实体的帐户查询称为cs_accountid

BusinessEntityCollection bec = RetrieveContact(Service, context, ((Key)(Account.Properties["accountid"])).Value.ToString(), "contact", "parentcustomerid");
                            if (bec.BusinessEntities.Count > 0)
                            {
                                foreach (contact c in bec.BusinessEntities)
                                {
                                    c.parentcustomerid = new Customer();
                                    c.parentcustomerid.type = "account";
                                    c.parentcustomerid.Value = k.Value;
                                    Service.Update(c);
                                }
                            }

private BusinessEntityCollection RetrieveContact(ICrmService service, IPluginExecutionContext context, string AccountID, string FromEntityName, string FromAttributeName)
        {

            // Create the ConditionExpression.
            ConditionExpression condition = new ConditionExpression();
            condition.AttributeName = "accountid";
            condition.Operator = ConditionOperator.Equal;
            condition.Values = new string[] { AccountID };

            // Create the Link entities
            LinkEntity le = new LinkEntity();
            le.LinkFromEntityName = FromEntityName;
            le.LinkFromAttributeName = FromAttributeName;
            le.LinkToEntityName = "account";
            le.LinkToAttributeName = "accountid";

            // Create the FilterExpression.
            FilterExpression filter = new FilterExpression();

            // Set the properties of the filter.
            filter.FilterOperator = LogicalOperator.And;
            filter.AddCondition(condition);
            le.LinkCriteria = filter;

            // Create the QueryExpression object.
            QueryExpression query = new QueryExpression();

            // Set the properties of the QueryExpression object.
            query.EntityName = FromEntityName;// EntityName.contact.ToString();
            query.ColumnSet = new AllColumns();// cols;
            query.LinkEntities.Add(le);
            //query.AddOrder("lastname", OrderType.Ascending);

            BusinessEntityCollection bec = service.RetrieveMultiple(query);
            return bec;
        }

private DynamicEntity RetournRecord(string entityname, Guid recordid, TargetRetrieveDynamic target, ICrmService Service)
        {
            target.EntityId = recordid;
            target.EntityName = entityname;
            RetrieveRequest retrieve = new RetrieveRequest();
            retrieve.Target = target;
            retrieve.ColumnSet = new AllColumns();
            retrieve.ReturnDynamicEntities = true;
            // Create a response reference and execute the retrieve request.
            RetrieveResponse response1 = (RetrieveResponse)Service.Execute(retrieve);
            return (DynamicEntity)response1.BusinessEntity;
        }

感谢您的帮助和时间!

enter image description here

2 个答案:

答案 0 :(得分:1)

我知道这是旧的,但是createquery的问题是由于拼写错误 - > [" cs_accountid")它应该以方括号而不是圆括号结束。

答案 1 :(得分:0)

<强>更新 此解决方案适用于CRM 2011/2013,而不适用于CRM 4.0:

我从未使用过早期绑定,所以这是我后期绑定的例子(也适用于你)。

using Microsoft.Xrm.Sdk.Client;

...

var service = OrganizationServiceFactory.CreateOrganizationService(PluginExecutionContext.UserId);
using (var context = new OrganizationServiceContext(service))
{
     List<Entity> myCompanyRecords = context.CreateQuery("cs_company").Where(o => ((EntityReference)o["cs_accountid").Id.Equals(id)).ToList();
}

“id”是您的帐户实体记录的Guid。

在您的方法中,您已经拥有参数“service”和“context”,因此您可以使用它:

List<Entity> myCompanyRecords = context.CreateQuery("cs_company").Where(o => ((EntityReference)o["cs_accountid").Id.Equals(id)).ToList();