2015-01-19 9 views
0

이 코드는 계정 개체에서 company라고 내가 다른 사용자 지정 엔터티의 레코드를 검색 할맞춤 엔터티에서 데이터를 검색하는 방법은 무엇입니까?

계정 개체의 관계 1:N에 따라 연락처 레코드를 검색하는 데 도움이 그러나 나는 사용자 정의 데이터 호출하는 방법을 모른다 - 특정 회사의 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

+1

이것은 CRM4 또는 CRM2011을 의미합니까? – Alex

+0

이것은 CRM 4를위한 것입니다 – tollamie

답변

0

UPDATE : 이 솔루션이 아니라 CRM 4.0 CRM 2,013분의 2,011입니다 : 내가 그래서이 latebinding 내 예 (당신을 위해 일 것이다 earlybinding 사용한 적이

, 너무).

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입니다. 당신의 방법에

이미 매개 변수 "서비스"와 "컨텍스트"그냥이 사용할 수 있도록이 있습니다

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

내 게시물을 확인하십시오, 내 코드의 이미지를 추가했습니다 ... 볼 수 있듯이, 그것은 creatquery를 허용하지 않습니다 – tollamie

+0

당신의 사용을 볼 수 없지만 Microsoft를 사용하여 추가해야합니다 .Xrm.Sdk.Client; –

+0

이 Microsoft.Xrm.Sdk.Client를 추가하려고하면; 그는 .xRM을 알지 못합니다. 왜냐하면 저는 microsoft.xrm.client 참조를 추가 한 후에 버전 crm v 4.0에 있기 때문에 작동하지만 여전히 작동하지 않습니다! 오, 죄송합니다. – tollamie

1

나는이 오래 알고 있지만, createquery의 문제는 오타로 인해 -> [ "cs_accountid") 둥근 것이 아닌 대괄호로 끝나야합니다.